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?

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()
  • Thank you. That fixed the issue. – sisko May 30 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 at 1:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.