Despite numerous attempts to get the DC motor running, it will not work. I have checked the wiring several time… the motor.py program is running, but the motor not.
I use RPI 4 Model B and Bullseye (updated - Ver 11) - python3 ver.3.9.2
The Blinking LED and RGB LED does work…
and I use Thonny for testing.
Furthermore the Python code for 1.3.1 on the project page is the the wrong one… it refers to the temperature sensor… - but I use the one from the Directory…
My Pin Setup is:
MotorPin1 = 27
MotorPin2 = 17
MotorEnable = 22
defsetup()
GPIO.setmode(GPIO.BCM)
… etc
Can it be, that L239D does not work properly ? the Power Supply Module does deliver 5 V on the breadboard 's +/- Pins - I used a new 9 V battery (LED is on)
So how to check, whether or not the L239D does work properly.?
This hint / error message I get in Python3:
%Run 1.3.1_Motor.py
/home/pi/davinci-kit-for-raspberry-pi/python/1.3.1_Motor.py:15: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(MotorPin1, GPIO.OUT)
/home/pi/davinci-kit-for-raspberry-pi/python/1.3.1_Motor.py:16: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(MotorPin2, GPIO.OUT)
/home/pi/davinci-kit-for-raspberry-pi/python/1.3.1_Motor.py:17: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(MotorEnable, GPIO.OUT, initial=GPIO.LOW)
Clockwise
Stop
Counterclockwise
Stop
Clockwise
and here is the code I use:
#!/usr/bin/env python3
import RPi.GPIO as GPIO
import time
# Set up pins
MotorPin1 = 27
MotorPin2 = 21
MotorEnable = 22
def setup():
# Set the GPIO modes to BCM Numbering
GPIO.setmode(GPIO.BCM)
# Set pins to output
GPIO.setup(MotorPin1, GPIO.OUT)
GPIO.setup(MotorPin2, GPIO.OUT)
GPIO.setup(MotorEnable, GPIO.OUT, initial=GPIO.LOW)
# Define a motor function to spin the motor
# direction should be
# 1(clockwise), 0(stop), -1(counterclockwise)
def motor(direction):
# Clockwise
if direction == 1:
# Set direction
GPIO.output(MotorPin1, GPIO.HIGH)
GPIO.output(MotorPin2, GPIO.LOW)
# Enable the motor
GPIO.output(MotorEnable, GPIO.HIGH)
print ("Clockwise")
# Counterclockwise
if direction == -1:
# Set direction
GPIO.output(MotorPin1, GPIO.LOW)
GPIO.output(MotorPin2, GPIO.HIGH)
# Enable the motor
GPIO.output(MotorEnable, GPIO.HIGH)
print ("Counterclockwise")
# Stop
if direction == 0:
# Disable the motor
GPIO.output(MotorEnable, GPIO.LOW)
print ("Stop")
def main():
# Define a dictionary to make the script more readable
# CW as clockwise, CCW as counterclockwise, STOP as stop
directions = {'CW': 1, 'CCW': -1, 'STOP': 0}
while True:
# Clockwise
motor(directions['CW'])
time.sleep(5)
# Stop
motor(directions['STOP'])
time.sleep(5)
# Anticlockwise
motor(directions['CCW'])
time.sleep(5)
# Stop
motor(directions['STOP'])
time.sleep(5)
def destroy():
# Stop the motor
GPIO.output(MotorEnable, GPIO.LOW)
# Release resource
GPIO.cleanup()
# If run this script directly, do:
if __name__ == '__main__':
setup()
try:
main()
# When 'Ctrl+C' is pressed, the program
# destroy() will be executed.
except KeyboardInterrupt:
destroy()
any help is appreciated…
Thanks
Bernhard
from Lüneburg/Germany