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.

4 komentáře:

  1. hi Jan, thanks for sharing your experience.
    can you explain why did you used power supply from raspberry?
    if i remove power supply jumper (light green on your picture) and remove power supply connection from raspberry, will it work?

    OdpovědětVymazat
  2. _MOTORS = {
    DC_Motor_1: {_PIN_: None, _DIRECTION_: [4, 8, 4 | 8], _IS_RUNNING_: False,
    _RUNNING_DIRECTION_: None, _PWM_FREQUENCY_: 10, _PWM_DUTY_CYCLE_: 100, _PWM_: None},
    DC_Motor_2: {_PIN_: None, _DIRECTION_: [2, 16, 2 | 16], _IS_RUNNING_: False,
    _RUNNING_DIRECTION_: None, _PWM_FREQUENCY_: 10, _PWM_DUTY_CYCLE_: 100, _PWM_: None},
    DC_Motor_3: {_PIN_: None, _DIRECTION_: [32, 128, 32 | 128], _IS_RUNNING_: False,
    _RUNNING_DIRECTION_: None, _PWM_FREQUENCY_: 10, _PWM_DUTY_CYCLE_: 100, _PWM_: None},
    DC_Motor_4: {_PIN_: None, _DIRECTION_: [1, 64, 1 | 64], _IS_RUNNING_: False,
    _RUNNING_DIRECTION_: None, _PWM_FREQUENCY_: 10, _PWM_DUTY_CYCLE_: 100, _PWM_: None}
    what does the numbers in direction mean please

    OdpovědětVymazat
  3. Thank you.
    https://github.com/mahirgul/wifi-nodemcu-robot-esp8266

    OdpovědětVymazat