Arduino Workshop 03 - Output: Actuators
Arduino Workshop 03 - Output: Actuators
Remember the problems we ran into when we tried to drive more than 3 LEDs from one
Arduino output pin? Connecting them in series required more than 5V (max. voltage
output per Arduino pin) if we wanted to drive more than 3 LEDs and connecting them in
parallel required more than 40mA of current (40mA is the maximum current output of
an Arduino pin) when trying to connect more than 2 LEDs (review series and parallel
circuits in the Arduino workshop 01 if this does not sound familiar to you).
Transistors help us overcome the voltage and current limitations of Arduino pins.
Transistors are polarized electronic components that amplify amount of electrons going
through a circuit. In a transistor, a weak current (i.e. few electrons) controls a larger
current (lots of electrons).
The following picture from my old electronics junior project book illustrates this:
Here is a simple circuit that shows how to use a 2222 transistors to control 4 LEDs
connected in parallel. Important: The NPN 2222 transistor has a current rating of
200mA. If each LED has an operating current of 20mA (general purpose LED, clear LEDs
usually require more current) you can drive a maximum of 10 LEDs in parallel this way.
Other transistors, such as the TIP120 that we’ll use for the motor control example in
section 2) can control much more current than the 2222.
You can rewrite the blink example (make sure to define pin 9 instead of pin 13 as the
LED output pin) to quickly test your circuit. In the Arduino software:
File > Examples > 01. Basics > Blink
The resistor at the transistor’s base (R1) can be any value between 1 - 10kΩ, the
resistors for the LEDs (R2 – R5) are 220Ω:
R2 R3 R4 R5
R1
The Arduino’s output pins can only supply (sink or source) a very weak current of a
maximum of 40mA per pin. This means that you cannot connect a larger load (such as a
motor, larger number of LEDs, relay, etc.) directly to the output pin. You need an
amplifier that is connected to the pin. For smaller loads a regular NPN2222 switching
transistor works (up to 200mA current, see example in section above) for larger loads
you should consider a power transistor such as the TIP120 (collector current up to 5A,
or pulsed up to 8A)
A very simple Arduino controlled TIP120 circuit would look like this:
You can get the +9V voltage directly from the Vin pin on your Arduino (as long as you
use a (V power supply of course). If you need a +5V voltage, use one of your 7805
voltage regulators to convert the output from your 9V power supply to +5V. Please look
at the section “power supply decoupling” on p. 254 in “Physical Computing” if you
intend to driver larger loads (multiple LEDs, high brightness LEDs, motors, relays, etc…)
The SN754410 is a handy IC that allows you to control the speed and direction of a DC
motor with only one PWM output and two digital outputs from your Arduino board
Please read pp.255 -260 in “Physical computing for how to use the SN754410 motor
driver IC with a microcontroller.
In the following example we will control a DC motor with a potentiometer. The first half
of the 270° degree turning angle of the potentiometer will make the motor run forward.
The motor’s speed will decrease the more the knob is turned toward the
potentiometer’s center position. The second half of the turning angle will make the
motor run in reverse, with increasing speed toward the 270° mark. The percentages in
the following picture are related to the motor’s speed.
Regular DC motors vs. gear motors
In most of your application when a rotary movement is necessary you will need force
(torque) over speed. In this case, use a gear motor instead of a regular DC motor. The
gearbox attached to a motors output shaft amplifies its torque and slows down its
speed. If you are using a regular DC motor and adjust its speed with the Arduino’s PWM
output you will just slow down its speed and reduce its torque! You might consider
hacking a servo motor for an inexpensive continuously rotating gear motor if you need
one and only have a servo available (see:
http://www.seattlerobotics.org/guide/servohack.html). Solarbotics has inexpensive gear
motors if you need to purchase some genuine gear motors:
https://solarbotics.com/catalog/motors-servos/gear-motors/
The following is from the Seattle Robotics Society’s intro to servo motors. It describes
timing, the most important aspect of controlling a servo motor’s 180 degree movement
with a microcontroller:
How do you communicate the angle at which the servo should turn? The control wire is
used to communicate the angle. The angle is determined by the duration of a pulse that
is applied to the control wire. This is called Pulse Coded Modulation. The servo expects
to see a pulse every 20 milliseconds (.02 seconds). The length of the pulse will
determine how far the motor turns. A 1.5 millisecond pulse, for example, will make the
motor turn to the 90 degree position (often called the neutral position). If the pulse is
shorter than 1.5 ms, then the motor will turn the shaft to closer to 0 degrees. If the
pulse is longer than 1.5ms, the shaft turns closer to 180 degrees.
As you can see in the picture, the duration of the pulse dictates the angle of the output
shaft (shown as the green circle with the arrow). Note that the times here are illustrative,
and the actual timings depend on the motor manufacturer. The principle, however, is
the same. You can start with a range from 1000-2000ms and then work your way
toward 500ms to 2500ms.
A simple example
In this simple example we connect a potentiometer to one of the Arduino’s analog
inputs. The turning of the potentiometer’s knob should result in a proportional turning
of the servos output shaft.
Arduino A0 pin
Arduino digital
input pin 9
Arduino code: The Arduino code makes use of a built-in library for servo control. In
Arduino open: File > Examples > Servo > Knob
If you notice an unsteady or jerky behavior of the servo motor’s output, put a 0.1µF
capacitor between the Arduino’s analog pin A0 and GND. This will smooth out any
possible voltage spikes from the potentiometer.
(4) Robot control: Arduino and iRobot Create
Let’s start by first taking a look at the anatomy of the iRobot Create:
More information about the iRobot Create can be found in the robot’s manual:
http://www.irobot.com/filelibrary/pdfs/hrd/create/Create%20Manual_Final.pdf
Sensors
The iRobot Create comes with some basic sensors mounted on its platform, they are:
Furthermore the iRobot Create allows access to sensor monitoring some of its internal
functions:
• distance the robot has traveled since the distance was last requested (in
millimeters)
• angle the robot has turned since the last time the angle was requested (in
degrees)
• charging state
• battery voltage (in mV)
• current (flowing into the robot’s battery, in mA)
• battery temperature (in degrees Celsius)
• battery charge (in mAh)
• battery capacity (in mAh)
• velocity information (left, right, in mm/s)
Actuators
The iRobot Create Open interface allows access to the following on-board actuators
• wheel motors (speed left, speed right)
• LEDs (play, advance, bi-color power LED)
• speaker (define musical notes)
Using the Arduino board as a controller, other sensors and actuators can be added
easily to the Arduino board’s I/O pins (e.g. photocells, IR proximity sensors, ultrasonic
range finders, servo motors, etc.)
Connecting an Arduino UNO board to the iRobot Create:
pin 21 (GND)
b) Reading basic sensor values
We are making use of Arduino’s softSerial library, so we can work with two independent
serial ports on one Arduino board. The reason for this is simple: in order to see if the
Arduino can read OI sensor values we need to be able to send requests to OI that return
values (serial call and response) and at the same time communicate these readings back
to the Arduino serial monitor so we can see what values we get. In a later step we can
then trigger driving commands based on the sensor readings. For now however we are
just interested in reading sensor values.
Why don’t we connect the Arduino UNO’s hardware serial port (RX/TX)?
“The serial port output TXD from the Roomba/Create is too weak to drive the RX
serial port (hardware serial port) input of an Arduino properly. This is because of
the USB-Serial converter on the Arduino: it also tries to drive the RX serial port
input via a pullup resistor, but the Roomba does not have enough drive to pull the
RX down below about 2.5 volts, which is insufficient to be reliably detected as a
TTL serial input of 0.”
Winkler, AD32600 Physical Computing, Arduino workshop 03, p. 11
(from: http://projectsfromtech.blogspot.com/2013/11/irobot-create-arduino-
control.html).
Notes on the Arduino Mega: the Arduino Mega has 4 serial ports, use Serial1(TX1/RX1),
Serial2(TX2/RX2) or Serial13(TX3/RX3) instead of Serial0. You can still use Serial0 to
communicate back to a serial monitor via USB or XBee for debugging if needed.
We would like to connect a potentiometer to Arduino’s analog pin A0 for speed control
and map the potentiometer’s reading like this: the robot should be at speed 0 when the
potentiometer is in the middle position, it should be at the maximum speed backwards
when we turn the potentiometer all the way to the left and it should be at its maximum
forward speed when the potentiometer is turned all the way to the right. The following
drawing shows the hardware setup for this example and the full Arduino code example
can be downloaded here:
http://web.ics.purdue.edu/~fwinkler/AD32600_F14/create_go.zip.
(3) stop()
(4) spinLeft(speed)
(5) spinRight(speed)
The final code example has all of these functions implemented and makes use of them
in the bump and go behavior:
http://web.ics.purdue.edu/~fwinkler/AD32600_F14/bump_and_go.zip
Outlook
This is just the beginning of what you can do with Arduino and the iRobot Create
platform. Feel free to combine Arduino examples from previous and future workshops
with the iRobot Create to develop exciting robot-based applications. For example, you
can think about drawing a line in Processing and having the iRobot Create execute this
line by driving along it in the real world. In the next workshop we will take a closer look
at how to integrate Processing and Arduino, so that something like the idea mentioned
above could be soon realized by you!
Further reading:
Kurt, Tod E. Hacking Roomba. Indianapolis, IN: Wiley,2007.