I use this simple circuit to control a DC motor with an L293D, and a 9g servo motor with a Raspberry PI Zero W:
I use the simple following code to test the circuit:
from time import sleep
import RPi.GPIO as GPIO
ServoPin = 32
MotorPin1 = 3
MotorPin2 = 5
MotorEnable = 33
GPIO.setmode(GPIO.BOARD)
GPIO.setup(ServoPin, GPIO.OUT)
GPIO.setup(MotorPin1, GPIO.OUT)
GPIO.setup(MotorPin2, GPIO.OUT)
GPIO.setup(MotorEnable, GPIO.OUT)
servoPWM = GPIO.PWM(servoPIN, 50) # 50 Hz
servoPWM.start(2.5) # Duty cycle (0.0 <= dc <= 100.0)
motorPWM = GPIO.PWM(MotorEnable, 100)
motorPWM.start(0)
GPIO.output(MotorPin1, GPIO.LOW)
GPIO.output(MotorPin2, GPIO.HIGH)
sleep(0.5)
servoPWM.ChangeDutyCycle(5)
motorPWM.ChangeDutyCycle(25)
sleep(0.5)
servoPWM.ChangeDutyCycle(10)
motorPWM.ChangeDutyCycle(50)
sleep(0.5)
GPIO.output(MotorPin1, GPIO.HIGH)
GPIO.output(MotorPin2, GPIO.LOW)
servoPWM.ChangeDutyCycle(2.5)
motorPWM.ChangeDutyCycle(80)
sleep(0.5)
The raspberry crashes (reboot) often, mostly when controlling both the motor and the servo at the same time. Any idea why?
I followed those instructions for the DC motor wiring.