0

I am learning how to build a basic robot using a raspberry pi and an L298N h-bridge.

I have followed this tutorial successfully i:e the single DC motor I have connected does work in response to the code. My problem is when I use the code, below, the motor does not work:

import sys
import time
import RPi.GPIO as GPIO

GPIO.setwarnings(False)

Forward = 23
Backward = 24
Enable = 25

GPIO.setmode(GPIO.BCM)
GPIO.setup(Forward, GPIO.OUT)
GPIO.setup(Backward, GPIO.OUT)
GPIO.setup(Enable, GPIO.OUT)

p=GPIO.PWM(Enable, 1000)
p.start(25)

def forward(t):
    GPIO.output(Forward, GPIO.HIGH)
    GPIO.output(Backward, GPIO.LOW)
    print("Moving Forward")

def reverse(t):
        GPIO.output(Forward, GPIO.LOW)
    GPIO.output(Backward, GPIO.HIGH)
        print("Moving Backward")

while(1):
        forward(3)
        # reverse(3)
        GPIO.cleanup()

When I run the script, I get the following error:

Traceback (most recent call last): File "move2.py", line 43, in forward(3) File "move2.py", line 27, in forward GPIO.output(Forward, GPIO.HIGH)

RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)

I had set the pin mode to BCM earlier in the code so I'm not sure why the error is occurring.

I'm not sure what I'm doing wrong.

A little help, please?

sisko
  • 277
  • 2
  • 4
  • 9
  • But have you tried the tutorial's complete demo program? If the demo program works, then you can try to modify it bit by bit. Your program uses PWM which is a bit complicated for newbies. . Perhaps you can try simpler programs - https://raspberrypi.stackexchange.com/questions/96515/why-dont-my-motors-rotate/96517#96517 https://raspberrypi.stackexchange.com/questions/95999/why-isn-t-my-raspberry-pi-motor-spinning/96016#96016 – tlfong01 May 30 '19 at 14:09

1 Answers1

2

Its because you are calling GPIO.cleanup() in your while loop:

while(1):
    forward(3)
    # reverse(3)
    GPIO.cleanup()

Comment out the cleanup:

while(1):
    forward(3)
    # reverse(3)
    #GPIO.cleanup()
CoderMike
  • 6,381
  • 1
  • 9
  • 15
  • Thank you. That fixed the issue. – sisko May 30 '19 at 16:54
  • How neat! I stared at the OP's program for a couple of minutes and found everything OK. :) I also read the demo program in the the OP's recommended tutorial and found it good. – tlfong01 May 31 '19 at 01:01