Primeiramente gostaria de salientar que não sou o autor principal do conteúdo deste artigo. A minha intenção é de mostrar o que outros autores tem realizado (referenciando links para o conteúdo original) neste blog até como um guia pessoal para uso próprio e ainda compartilhar minhas anotações e pequenas modificações que realizo no conteúdo, desejando que possa ajudar mais alguém em algum lugar.
Pin No: 16 pins
In this tutorial I will show you how to connect some motors
to your Raspberry Pi. Doing so will allow your Raspberry Pi to interact in the
real world, making it possible to build a robot, turn on a fan on a hot day or
even drop a treat for your cat or dog while your away.
What we plan to do is safely connect one or two motors to the Raspberry Pi with as few components as possible. Once we have the electronics put together on the breadboard, I will show you how to control them easily using Python to first make the motor spin, and then add some control to change the motor direction so we can go backwards.
This guide will require a careful eye to catch any mistakes, and a bit of courage, especially if you are new to the GPIO connectors. I would like to stress I am not responsible for any damage caused to your Raspberry Pi and/or components.
IMPORTANT: Do not connect a motor, no matter how small directly to the Raspberry Pi, it will damage your Raspberry Pi.
The main processor can only supply enough power to light a LED, roughly 20mA. A motor will want at least 400mA of current to start turning.
To get a motor running, you will need:
If you haven’t looked closely at your Raspberry Pi before, now might be the best time to have a good look. There are 26 pins grouped in two rows of 13, and these collectively are called the General Purpose Input Output header or GPIO for short. These are a mix of four power pins, five ground pins and 17 data pins.
Figure 1. The Layout of the GPIOs with the pin numbering. Pin 1 is the top left labelled 3V3
Some of these data pins have extra functions such as an i2c bus, SPI bus and UART serial connectors, all of which can connect to other hardware to allow the Raspberry Pi to talk to items such as an Arduino, an Analogue to Digital Convertor (ADC) or add-on boards such as a PiGlow or PiFace.
TIP: When working with the GPIO pins, always do this while the Pi is unplugged, as any accident by connecting (or shorting) 2 pins together can cause damage to the Raspberry Pi.
It is important to do this while the power to the Raspberry Pi is off, or disconnected, as you want to avoid shorting any connectors by mistake.
The first thing you need to do is connect up the power and ground wires. As with most electronics projects, everything that connects together will require a common ground. This is shown with the black wires.
Figure 2: Connect the power and ground wires
The ground on the Raspberry Pi is physical pin 6. Referring to Figure one this is worked out by starting at the top left with pin 3V3, counting left to right so 5V is pin 2, GPIO 2 (labelled 2) is pin 3 and so on.
Reading pin numbers on Integrated Circuit (IC) chips is easily done by having the notch or dot to the left then starting from bottom left gives us pin 1.Figure 3. Pin 1 is at the bottom left
Now add three wires from the GPIO pins to the L293D.
Figure 4. Add the three GPIO wires to control the motor
Add the motor:
It is extremely important that you double-check every connection before adding the batteries. Only when you are happy that everything is in place, connect the battery wires to the power rails of the breadboard.
One of the great features of the L293D is it that it can handle two motors independently and each motor can run at different speeds or directions. Using this one IC makes it possible to create a two-wheeled robot capable of turning, going forwards and going backwards easily.
Adding a second motor involves just three additional wires and another motor.
It is important to check and double-check any wiring before adding any power source to your project as some of the wiring can get a bit fiddly it is easy to miss a connection and send 5V in to the 3.3V of the Raspberry Pi.
Always check your wiring and then check it again!
If you haven’t set up an SD card for your Pi before, it will be worth understanding how to create one by reading the How to Flash an SD Card for Raspberry Pi tutorial, first.
With a freshly created Raspbian SD card in place connect the Raspberry Pi as usual and boot up.
Add the batteries, ensuring that you pay attention to the correct rails (the long strips along the top and bottom, if you have any), as you only want the black wire to connect to the ground, and the red wire to positive or source of the chip only.
The next job is to tell the Raspberry Pi that a motor, or two, has been connected. To do this I am using a language called Python. It comes installed on Raspbian which is a bonus. If you are using another Operating System such as Arch or PiDora, double-check if RPi.GPIO is available.
In order to get the motors to work double-click LXTerminal on your desktop to bring up a terminal window. This is where you will be writing Python code using a program called Nano. Nano is a text editor, similar to Notepad or TextEdit but for the command prompt, I will teach your some commands as we go along if you are new to it.
To turn the motor on for two seconds use the following code:
The first two lines tell Python what is needed in the program.
The first line will want to access a module called RPi.GPIO. This module handles all the hard work involved around turning the GPIO pins on and off on the Raspberry Pi.
The second line brings in sleep from the module time to make it possible to pause the script giving it time to perform a certain action, in this case leaving a motor on for a few seconds.
The function setmode tells RPi.GPIO to use the board numbering on the Raspberry Pi. The numbers 16, 18 and 22 we will use to tell Python they are the pins associated with the motors.
When using the L293D you can give it a direction, by turning one side on to turn in one direction, called pin A and vice versa is pin B. To turn the motor on use a pin called Enable, labelled E in the test script–this is pin 22. I'll cover this a bit more later.
Finally, tell the Raspberry Pi these are all outputs which is done with GPIO.OUT.
With the script set up, the Raspberry Pi ready to turn the motors. It will turn on some pins, wait two seconds then turn them off again, shown in the remainder of the script.
Save and exit by pressing CTRL-X, along the bottom a message asks you to confirm the changes. Press Y and Enter to confirm. Now you are back at the command prompt to run the script and see the motor spin to life.
If the motor didn’t turn, double check your wiring or batteries. Debugging and finding out why something doesn’t work can be annoying, but is a useful step in learning something new!
It’s brilliant to have a motor spin, but even better to make it turn backwards, so I'll show you how to do that.
Nothing needs to be done to the wiring, this is purely Python now. This is achieved by creating a new script, calling it motorback.py. To create the script in Nano, enter the command:
Enter the following code:
CTRL-X then Y followed by Enter to save.
The script is very similar to the previous one, but if you notice for backwards we made Motor1A low and Motor1B high.
High and low are programming names for on and off.
To stop the motor you'll turn off, low, Motor1E.
Enable is the switch to turn the motor on and off, regardless of what A and B are doing.
If you are finding this confusing, look at a Truth Table to see what is happening.Truth table
There are only two states which allow the motor to turn, when Enable is on or high, and either A or B must be high, but not both.
For two motors look at the following script. All that is different is a couple more lines to set up the second motor.
After testing the IR sensor modules, next you need to connect and test the L293D module and the motors. Power the L293D module by connecting the + and – pins of the board to the 9V battery. Also, connect the “-” of the board to the GND of raspberry pi. You can refer to the connection diagram here for completing the connections:
Next, you have to provide the inputs to the board. Four output GPIO pins from the raspberry pi control the direction of rotation of the two motors. The two terminals of the motors are then connected to the 4 output terminals of the board. The motors then, based on the command from the raspberry pi are powered by the 9V battery. The logic for controlling the motors from the raspberry pi is as given below:
Here, HIGH means 5V signal or digital 1 and LOW is 0V signal or digital 0. Eg: GPIO.output(5,1), this command sends a HIGH signal (digital 1) to pin no 5 on the raspberry pi. Thus each motor’s direction can be controlled by writing HIGH/LOW signals through two GPIO pins from the raspberry pi.
Next, you need to wire a push button to the raspberry pi as shown in the above connection diagram. This button is used to activate and de activate the robot. After wiring the robot, you need to attach the wheels. Use double side tape to fix the parts together on the robot chassis. After which, you will have a setup that almost looks like this:
After connecting the motors you need to check them. Use the code below to check the motors and the L293D. Make sure you have powered the driver board (L293D) and given the connections as per the above diagram. After that, copy the code below and save it as a python file: motor.py on your raspberry pi. Then run it using the command: sudo python motor.py. You will notice that both the motors rotate in one direction first and then after a second rotates in the opposite direction. This process repeats until you interrupt it.