Driving a DC Motor with Raspberry Pi
Using a Dual H Bridge DC Stepper Motor Drive
Introduction: (Skip if you want to just get things connected) The curiosity rover I am building requires the use of larger than usual motors. This means I can’t use small 1-inch motors that you find in most RC cars. This also means that the power requirements for each motor is far more than a motor shield can handle.
- You have a properly sized motor-to-power-supply ratio
- Basic understanding electronics and polarity
The specs for each of these motor drives:
Dual H bridge drive Chip: L298N Logical voltage: 5V Drive voltage: 5V-35V Logical current: 0mA-36mA Drive current: 2A(MAX single bridge) Storage temperature: -20 to +135 Max power: 25W
The motor driver I’m using is the L298N Dual H Bridge Driver driver.
For reference I have labeled the pins on this board.
The jumper that connects motor voltage to logic allows you to power the chip with the motor voltage. If you’re using 7-volts or less to power your motors you don’t to wire+5 volt terminal, just leave the jumper in place.
The enable pin jumpers put each respective motor in a “ready to run” state. Whenever voltage is applied to a motor’s enable pin, the motor will run. Use this option if you don’t want to control the speed of the motors.
I’m using a 6-volt battery, so I leave the jumper in place to power both the motor and logic from the same source. Connect the positive power source to the left power terminal, and the negative to the middle ground terminal.
In DC circuits, red typically indicates positive and black for negative
Connect the wires to the power source too. I use terminal blocks as I tend to have many wires.
Connect wires to Motor A IN1 and Motor a IN2
Connect those wires to the GPIO pins on the Pi. The physical pin numbers are 16 and 18.
I’ll be referencing the pins according to their physical numbering. In other places the pins might be referred to by their software number (e.g. physical pin 16 is GPIO23)
Then connect the motor to the Motor A terminals. It does not matter which of the Motor A terminals red or black are connected to, the beauty of the H Bridge is that it will reverse motor direction depending on which IN pin has power.
That’s it for wiring and hardware! Now we have to get the software connected.
If you haven’t used python on your pi yet, you should install it now by running this command in your terminal
sudo apt-get install python-dev
Then run this command to install GPIO itself:
sudo apt-get install python-rpi.gpi
You will probably be prompted to confirm that you want to install, type
Once Rpi GPIO is installed you can import it into an IDLE script or a python script.
import RPi.GPIO as GPIO
I will also import python’s
time class so I can use a timer.
Here’s thje code for our initial stup.
# We're using the physical board pin numbers GPIO.setmode(GPIO.BOARD) # define which pins we are driving the L298N with DirectionOnePin = 16 DirectionTwoPin = 18 # Define how we are using these pins GPIO.setup(DirectionOnePin, GPIO.OUT) GPIO.setup(DirectionTwoPin, GPIO.OUT)
GPIO.setup(..., GPIO.OUT) doesn’t run the motors, it only configures the GPIO interface to use those pins as output pins.
To run the motors add these lines to your script
print "Motor A Direction one" GPIO.output(DirectionOnePin, GPIO.HIGH) time.sleep(2) GPIO.output(DirectionOnePin, GPIO.LOW) print "Motor A Direction two" GPIO.output(DirectionTwoPin, GPIO.HIGH) time.sleep(2) GPIO.output(DirectionTwoPin, GPIO.LOW) # This command clears the configuration from the GPIO interface GPIO.cleanup()
That’s it! Now you should be able to run the script via the command `sudo python myscript.py`, with `myscript` being whatever you named the file. If you wrote the program in IDLE you can click the run button.
In the next tutorial I’ll explain how to use PWM To control the speed of a motor connected to this driver.