úterý 8. března 2016

RoboCar: Arduino Motor Shield L293D with Raspberry Pi B+ (part 2)

Last time I wrote about the Motors Shield and I tried to describe how the L293D modul and the 74HC595 module work. And now is time for programming. But we have to wire it up first. You can connect PINs according this image. (image update - 05.09.2016 - fixed wiring of blue and brow wire on motor shield, thanks to Maxime)

And when you have it connected we can start talking about using python to run the motors.


  1. Raspbery Pi with installed OS (I have Raspbian)
  2. Installed Git, Python3 and RPi.GPI - sudo aptitude install python3 python3-rpi.gpio
  3. Connectected Motor Shield to Raspberry Pi

Controlling motors from Python

I've wrote Python class for controlling motors using this Motor Shield. You can download it from my github.

git clone https://github.com/lipoja/AMSpi.git

Using AMSpi class

from AMSpi import AMSpi

# For BOARD pin numbering use AMSpi(True) otherwise BCM is used
with AMSpi() as amspi:
    # Set PINs for controlling shift register (GPIO numbering)
    amspi.set_74HC595_pins(21, 20, 16)
    # Set PINs for controlling all 4 motors (GPIO numbering)
    amspi.set_L293D_pins(5, 6, 13, 19)
    # Run motors
    amspi.run_dc_motors([amspi.DC_Motor_1, amspi.DC_Motor_2, amspi.DC_Motor_3, amspi.DC_Motor_4])

When you are creating object you can specify what pin numbering you will use.
  • AMSpi() - for GPIO.BCM
  • AMSpi(True) - for GPIO.BOARD
In this example I am using BCM. BCM is the GPIO number. BOARD is the number in the circle (on the image above). 

Then you have to set the pins for shift register. It is done by set_74HC595_pins method.
amspi.set_74HC595_pins(DIR_LATCH=21, DIR_CLK=20, DIR_SER=16)

After that you must specify used pins for enabling the motors. This is done by set_L293D_pins method.
amspi.set_L293D_pins(5, 6, 13, 19)

You can choose which motor do you want to use. If you have only two motors you can set it like this: 
amspi.set_L293D_pins(PWM2A=13, PWM2B=19)

Now the exciting part. Spinning the wheel :-]. You can start one or multiple motors at time. Using run_dc_motors method and specifying which motor you want to run.
For this you can use class variable DC_Motor_X where X is number of the motor.
(DC_Motor_1, DC_Motor_2, DC_Motor_3, DC_Motor_4)

Running all 4 motors:
amspi.run_dc_motors([amspi.DC_Motor_1, amspi.DC_Motor_2, amspi.DC_Motor_3, amspi.DC_Motor_4])

Running only two motors:
amspi.run_dc_motors([amspi.DC_Motor_1, amspi.DC_Motor_2])

If you want to run only one motor you can use same method:

Or you can use this method for running only one specific motor:

Now the direction of the motor. How do you switch it. Easily. 
Just add False as second parameter of method:
amspi.run_dc_motors([amspi.DC_Motor_1, amspi.DC_Motor_2, amspi.DC_Motor_3, amspi.DC_Motor_4], clockwise=False)

OK, now you car is driving super fast forward but how do you stop it? Piece of cake. Just call:
amspi.stop_dc_motors([amspi.DC_Motor_1, amspi.DC_Motor_2, amspi.DC_Motor_3, amspi.DC_Motor_4])

The metod stop_dc_motors works same as run_dc_motors. 
Just specify the motors which you want to stop.
On the git repository is commented example.py file. 
You can look at it and see what it can do.

OK, that was easy, right? If you have questions just ask me. I will try to answer them. 

neděle 6. března 2016

RoboCar: Arduino Motor Shield L293D with Raspberry Pi B+ (part 1)

Target: Running 4 DC motors with Raspberry Pi B+ and Motor Driver Shield L293D for Arduino.
How to do it: I have no idea. I am lots. ... OK, do not freak out. Take a deep breath and think.

Arduino Motor Shield - How does it work?

How does the L293D module work? And why is there the 74HC595 thing? Hmm. I have to figure it out. 
With this shield you can run 4 DC motors. It is done by two L293D with help of 74HC595.

Motor Shield L293D for Arduino

L293D module - Motor controller

Let's start with the L293D. This module can control two motors. It tells the motor to run/stop and change the direction of the motor (clockwise/counterclockwise). I am lazy and I did not want to draw so I borrowed this image.

L293D Module - Connection of motors
Image source: http://www.noveldevices.co.uk/ar-project-motor-control
What do we need to spin the wheel? We should connect the motor and then we have to do two things.

1. Set the direction of the motor

This is done by setting HIGH or LOW value to PIN 2 and PIN 7 of the L293D. Direction can be set according table below.

Input 1 Input 2
LOW LOW no spin
LOW HIGH clockwise
HIGH LOW counterclockwise
HIGH HIGH no spin

Same can be applied to the second motor (circuit) on Input 3 and Input 4 (PIN 11 and PIN 15).

2. Enable the motor

Or we can say: Tell the motor to run. This is done by setting HIGH value to the PIN 1 (Enable 1,2) or PIN 9 for second motor.

74HC595 module - Serial Shift Register

Why do we need this shift register? I had no idea. But then I had a detailed  look to the shield scheme. And then I understood. It is for sawing wires. With this I can set for all motors its direction using only 3 data wires instead of 8. That's cool.

74HC595 -  Serial Shift Register
Image source: http://www.protostack.com/blog/2010/05/introduction-to-74hc595-shift-register-controlling-16-leds/

We need to connect from Raspberry Pi GPIO pins to SER (PIN 14), RCLK (PIN 12), SRCLK (PIN 11). Each output pin from the shift register (Qx) is connected to the Output PIN of L293D. (as you can see on scheme below) By passing right number to the shift register we can set the direction of each motor. Here are the combinations of number, motor and direction.

number motor direction
4 M1 clockwise
8 M1 counterclockwise
2 M2 clockwise
16 M2 counterclockwise
1 M3 clockwise
64 M3 counterclockwise
32 M4 clockwise
128 M4 counterclockwise

And from this scheme you can easily connect the Raspberry Pi GPIO to the correct pin of the motor shield (top left).

How to do it practically and how to use the python for running the motor will be described in next post.

středa 2. března 2016

RoboCar: Shopping fever

Few months ago I had an idea to build my first "robocar" using Raspberry Pi B+. I saw lot of Arduino robots but I don't wanted to use Arduino because I want my car to be "smart". So I ordered from dx.com these main items:

KEYES L293D Motor Control Shield for Arduino
ZL-4 Smart Car Chassis Kit for Arduino
Sensor Distance Measuring Module - Blue
Ultrasonic Smart Car Mounted Holder
DS18B20 Temperature Sensor - Black + Silver
PIR Motion Sensor

Then few female/female cables for connecting those things to Raspberry Pi GPIO. I already had Raspberry Pi B+ with WiFi module at home. OK, maybe you are asking why I did not bought the Motor HAT for Raspberry Pi but for Arduino. That is easy. They don't have it on dx.com and I think that it will not be so difficult to connect those modules to Raspi. But I do not have any skills in this area. I've never used Arduino or Raspi for driving motors. So wish me luck and let's go to connecting those items together. We will start with the motors ;-]