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…