Thursday, July 18, 2013

Getting it all together -- Raspberry Pi talking to the Arduino UNO talking to the L298N dual H-bridge makin’ wheels turn





All setup for some code and connection testing

My project manager (Kylie) is back home now so the project engineer (me) is trying to get a step or two ahead of her…  She’s expecting progress by the time we get together again!

Power:  I’m using a rechargeable LiPo USB battery to power the +5V for the RPi and Arduino and a separate set of 8 rechargeable NiMH AA’s (9.6V) for the motor power to the L298N.

Motors and motor drivers:  The chassis we’re using has 4 DC motors (3-12V) and I’m using a SainSmart L298N dual H-bridge module to drive each pair.  At the moment I only have one H-bridge, so for testing I’m driving the two rear motors.  My plan is to connect the second H-bridge to the front motors in a way that the code will drive the left and right wheels as pairs for maneuvering (if anyone knows why that’s a good or bad idea please let me know…). 

Arduino to L298N:  The connections from the Arduino to the L298N are pretty straightforward.  See the pics at the side of this page for reference.  If you want to vary the motor speed you have to remove the jumpers the ENA and ENB pins in order to provide a PWM signal from the Arduino. Then you have to provide a digital output pin for each of the four inputs on the L298N (IN1 & IN2 control motor A and IN3 & IN4 control motor B).  Finally, don’t forget to connect GND – GND.  Of course the NiMH battery pack is connected to the motor power block with + to VCC and – to GND.  You can leave the 5V empty since the board has an onboard regulator to provide the logic circuits the 5V from the motor power inputs. 

So, my pin connections are:

Arduino (digital)           L298N            Comment

pin 2                               IN1                  motor A direction
pin 3                               ENA                motor A speed; needs to be a PWM output pin
pin 4                               IN2                  motor A “brake”

pin 5                               IN3                  motor B direction
pin 6                               ENB                motor B speed; needs to be a PWM output pin
pin 7                               IN4                  motor B “brake”

In the Arduino sketch, you control the motor direction by setting IN1 (or IN3 for motor B) to HIGH or LOW and then “releasing the brake” by setting IN2 (or IN4 for motor B) to the opposite.
i.e.:
IN1 = HIGH  using digitalWrite
IN2 = LOW   using digitalWrite
ENA = 127   using analogWrite to set PWM
Will spin the motor in one direction at 50% (PWM values go from 0 – 255).  Reversing IN1 and IN2 will reverse direction. 

Hope that was clear enough…  I’ll post the code as soon as I get it initially tested.

BTW, here is a good place to make some acknowledgements:
1.    Username cbrittian1 provided a super Instructable (see link) that I followed pretty closely to get up and running and I’ve shamelessly “stolen” his code as the core for my project.  That’s the beauty of open source…  hopefully I’ll contribute some modifications and improvements along the way.

2.    At work we have an excellent robotics team, and Scott in particular has been a key “consultant”, turning me on to the Raspberry Pi in the first place, explaining the electronics details, and  pointing out a few things that should have been more obvious to me but weren’t.

3.    And of course there are many, many other great discussions and contributors out there that I’ve leveraged for this project.  For example, this YouTube video  is excellent for setting up your USB WiFi dongle.  It gives a link to a script as well.

RPi talking to the Arduino:  I plan to use the I2C for communications to / from the RPi and Arduino.  That’s the way cbrittian1 did it.  I’ve written and tested some very basic USB serial code but I didn’t want to rewrite his code, so I2C it is, at least for now.  Plus, by powering the Arduino from the 5V GPIO from the RPi, the vehicle won’t have to carry around the USB cable and I can still use it for code development and uploading.  I’ve ordered a couple 4-channel I2C-safe Bi-directional Logic Level Converters but haven’t received them yet.  Even though there are a few articles out there saying if you have the RPi set up as MASTER that it’s OK to direct connect the SDA / SCL pins because they have pull up resistors, I don’t want to take that chance yet so, now I wait…


13 comments:

  1. How many modules should i buy for 4WD? Four modules. |I noticed u had one module.

    ReplyDelete
  2. Hi!
    If you are asking about the L298N H-Bridge modules... Each L298N module is a Dual H-Bridge so each module has independent control for two motors. The picture was a test set up using one L298N Dual H-Bridge module to drive two motors. In my final set up I used two L298N Dual H-Bridge modules so I could independently drive four motors, but I'd probably recommend using only one and tying the left and right side motors together on the outputs of a single L298N Dual H-Bridge module. That would save weight and power consumption.
    Good luck!

    ReplyDelete
  3. Does motor on chassis has to be tightened? I just bought 4WD and also bought 2-L298N modules.

    ReplyDelete
  4. I think my chassis did have a motor or two that needed to be tightened. Good luck. Let me know if you run into trouble.

    ReplyDelete
  5. One last question. Does its matter which one is negative and which one is positive terminal on motor?

    ReplyDelete
  6. Not really. The polarity determines direction of a DC motor spin. For simplicity I wired the left side opposite of the right side so that when the outputs of the L298N module are all set HIGH the motors would be going in the same direction (since they are mounted in opposite directions).

    ReplyDelete
  7. It is ok to used 6V - 9V battery instead of 12V?

    ReplyDelete
  8. I did not use a 12v battery. Power: I’m using a rechargeable LiPo USB battery to power the +5V for the RPi and Arduino and a separate set of 8 rechargeable NiMH AA’s (9.6V) for the motor power to the L298N. See the schematic in the next post.

    ReplyDelete
  9. Hi Bruce! Thank you for all the hard work you did on this project! I got couple of questions on Arduino and Python source code location. I looked through "Instructables" as well, however I did not see path to code location. There are very detailed Raspberry PI setup, links to other sources, but I did not find source code. Did I miss something or overlooked :). Thanks again! Leo

    ReplyDelete
    Replies
    1. Thanks Leo! It's been a while since I've had time to work on this project. You've reminded me that I really want to get back to it and to work on improving things a bit. I need to lighten the robot, but add another ultrasonic sensor and improve the obstacle avoidance and iPad control. BUT I put 2 new links on this blog (under the little airplane picture at the top) to make it easier to get the code as it exists today. The other 2 links are to the great site "Let's Make Robots" where I have posted some additional info and the code is also available from there. Good Luck! --Bruce

      Delete
  10. Thank you very much, Bruce! I'll check those links. Looks like Arduino code covers the most - sensors and motor, but for Python you interact with SMBus (which is very important) and probably got more modules (video, remote control, etc) in the "Let's Make Robots" link.
    Thank you and all the best!
    --Leo

    ReplyDelete
  11. Hey Bruce, I really like your project and realised i have most of the parts at home. Was wanting to try it out but you link for Lets make robots I think is down (Either that or i am missing something really oblivious) would u be able to post the like here please???

    ReplyDelete
    Replies
    1. I think I just fixed the link to "This project on Let's Make Robots". Not sure what happened. Hope it all works out for you. Let me know if I can help.

      Delete