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)</p>

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
  • 287
  • 2
  • 4
  • 9

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
  • 7,102
  • 1
  • 11
  • 16