简体   繁体   中英

Raspberry Pi connected to Motor Controller- Getting error in code

*Hi, I am getting this code error:
*blink.py:25: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.*
GPIO.setup(ENA1,GPIO.OUT)
Traceback (most recent call last):
File "/home/pi/my_python_programs/blink.py", line 27, in <module>
GPIO.output(IN1,GPIO.LOW)
RuntimeError: The GPIO channel has not been set up as an OUTPUT*

It is in reference to this code I made to control a motor controller from the raspberry pi. Here is my code. I am wondering what the error is:

#ImportedLibraries
import RPi.GPIO as GPIO
from time import sleep
#Motor1
PWR= 17
ENA1 = 33
IN1 = 31
IN2 = 29
GND = 39
#Motor2
PWR = 1
ENA2 = 32
IN3 = 18
IN4 = 16
GND = 34
#SetMode
GPIO.setmode(GPIO.BOARD)
#Motor1
GPIO.setup(ENA1,GPIO.OUT)
PWM1=GPIO.PWM(ENA1,100)
GPIO.output(IN1,GPIO.LOW)
GPIO.output(IN2,GPIO.LOW)
#Motor2
GPIO.setup(ENA2,GPIO.OUT)
PWM2=GPIO.PWM(ENA2,100)
GPIO.output(IN3,GPIO.LOW)
GPIO.output(IN4,GPIO.LOW)
PWM1.start(10)

It seems that the setup is not been doing very well, it fails in line 25.

If you want to control a motor you will need a H bridge, like L293D or L298, and then use the gpiozero library, look for it in the net.

Try to use this code:

from gpiozero import Motor
from time import sleep

motor = Motor(forward=4, backward=14)

while True:
    motor.forward()
    sleep(5)
    motor.backward()
    sleep(5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM