RPi – Motor Control with an H-Bridge

In this episode of Cooking with Team 279, we explore how to control a small DC motor with a Raspberry Pi.

To control a DC motor, we will use the following basic elements:

  1. A simple PWM circuit to allow us to specify speed the motor should turn
  2. A switchable circuit to control the direction of spin
  3. A separate power supply for the motor itself

 

Source Code

If you need a refresher on using PWM with the Raspberry Pi, see the RGB LED Fader episode of Cooking with Team 279

Attached are two scripts to show the basics of controlling the DC motor using the circuit shown below.

  1. Hellowmotor.py
  2. motor.py

Hellomotor.py is a very basic script that simply prompts for the speed of the motor (0 to 100), sets the speed, and then prompts again.  The script allows enabling/disabling the motor and reversing the motor by manually editing variables within the script. The key points to note are:

  1. Use of the enable pin to allow the motor to be used
  2. How PWM is used to control speed
  3. How the two control pins are used to set the direction/speed

 

motor.py creates a GUI via tkinker to do the same things, but all on the fly, so it’s a bit more advanced, but allows nicer control of the motor.

(It’s also our first example of creating a GUI with checkboxs!)

 

Download Link: motor.tar.gz

 

 

Powering a DC Motor

As a best practice, always power a motor via a separate power supply than from the Raspberry Pi.

This is done for the following reasons.

  1. The power draw of most motors is higher than the Raspberry Pi can deliver, and will cause the Raspberry Pi to burn out
  2. Most motors will have a surge of power when first turned on that will exceed the power a Pi could deliver, even if the running power was within limits
  3. Motors will dump energy back into the circuit when being powered off that can overload and damage the Pi

 

Initial Power Draw and Back EMF

A DC motor is an example of an induction coil. An induction coil becomes charged with energy, and can store energy in a similar manner to a capacitor or a battery.  However, an inductor stores energy via a magnetic field, whereas a capacitor stores energy in an electric field.

When a motor is energized, that magnetic field does not yet exist, and additional current can be drawn briefly while the field is created and the motor reaches it’s steady state. This spike in power draw can be very high depending on the motor size.

Conversely, when power is removed from an electronic circuit which contains inductors, the magnetic fields will collapse and return their energy to the circuit.  The amount of power dumped can be high, and can damage micro controllers such as the Raspberry Pi.

Protecting a micro controller like the PI from these draws and back EMF requires a separate power supply for the motors, and some sort of buffer circuit to separate the electrical switching used for control and the power to drive the motor.

 

H-Bridge

A DC motor has two connections.  Hooking those leads up to a power source will turn the motor in a particular direction. If you want to reverse the direction of the motor, you have to reverse the two motor connections between the positive and negative supply leads.  You can build a circuit to allow you to do that reversing electronically rather than having to physically reverse the connections yourself,  which is called an H-Bridge.

H-Bridges can be used with more than motors, but we’ll stick with a high level overview for motors only in this article.

See the H-Bridge Wikipedia article for a good overview

There are various types of integrated circuits that implement H-Bridges that we can easily use in our circuits.   A given h-bridge  must be matched to the motor and power needed, so there are many possible devices to choose from.  In the low power range, two popular choices are:

The SN754410 has a higher power rating, but both are suitable for many small toy DC type motors.  The pin-outs on both ICs are the same, so the SN754410 can be substituted anywhere a L293D is shown (but check power draw on the motors if replacing a SN754410 with a L293D).

Both chips allow control of two motors, 4-36V for the motors, and both have diodes to protect the circuit against back EMF from the motor.  However, the L293D only allows up to 600mA current per motor, whereas the SN754410 allows up to 1A current draw per motor.

In our example circuit, we will use the SN754410.

 

Both of these chips have diodes in them to protect against Back EMF.  If you use other chips (such as the plain L293), you may need to add diodes to protect the Pi.

 

H-Bridge Connections

To use the SN754410/L293D, you must connect the following to the chip:

  1. A GPIO pin to enable/disable the motor
  2. Two GPIO pins to control the speed and direction of the motor
  3. The motor leads
  4. Power supply for the motor (4V to 36V)
  5. 5V power supply for the internal logic circuit
  6. Ground

If you want to control two motors, you of course do the above twice (except for the power supplies).

L293D/SN754410 Pin Out looks like this:

l293d-pinout

 

 

Pin Label Connect to Description
1 1,2EN GPIO 26 Used to enable/disable the motor connected to 1Y, 2Y
2 1A GPIO 5 one of two inputs to control speed and direction
3 1Y Motor Lead one of two motor output connections for the first motor
4 GND Heat Sink and Ground Connect all 4 ground pins to ground even if only controlling 1 motor
5 GND Heat Sink and Ground Connect all 4 ground pins to ground even if only controlling 1 motor
6 2Y Motor Lead one of two motor output connections for the first motor
7 2A GPIO 6 one of two inputs to control speed and direction
8 Vcc2 +4.5V to +36V power supply for motor Motor power supply
9 – 15 All Control pins for 2nd motor
16 Vcc1 +5V for logic power supply for internal logic circuit

 

Example Motor and Power

There are many small DC motors that can be used for learning.  In this example circuit, we used the DC motor in a microservo body that Adafruit sells.  This motor has a nice low power draw, and a slow enough rotation with it’s gearing to use for misc tasks.

While building this circuit myself, I paired it with a 5V power supply, and a barrel plug to drive the motor and connect to pin 8 on the H-bridge, although the DC motor used has a low enough draw that we could have used a microusb power supply instead.

Regardless of what motor you use, make sure you chose a power supply to match the motor, and the motor has <1A current draw when using the SN754410 H-bridge.

 

Circuit Diagram

Here is the example circuit completed:

sn754410-h-bridge_bb

sn754410-h-bridge_schem