■ BY JOE PARDUE SMILEY’S WORKSHOP
The Arduino Classroom
Arduino 101 — Chapter 6:
Analog Input
Last time, we were introduced to the concepts of analog versus digital signals where we
discovered that digital signals are made from two discrete voltages (in our case, +5V and
0V) and analog signals can take any voltage (in our case, on a continuum from +5V to
0V). We learned how to generate analog signals using PWM and also about the Arduino
serial monitor and how to use that to communicate between a PC and the Arduino. In
this chapter, we will learn about inputting analog signals using the ADC (
analog-to-digital converter) built into the Arduino. We will also get an introduction to some basic
electrical concepts which will help us to understand what it is we are inputting. First,
though, let's learn some more serial communication techniques.
Sending Commands and Data
The computer on the Arduino board is an ATmega328
microcontroller and — as the name indicates — it is
intended to control things. Sometimes a microcontroller is
entirely embedded into a system such as a car engine
where it might do something like sensing oxygen and
actuating an air intake valve. Other times, it is used in a
system where it requires human user input — like in a
microwave oven where it senses button presses and uses
that human input to set the cooking time.
from the PC serial monitor to the Arduino so that it could
use that number to set the angle of a servo. We saw that if
we received a string of characters that represent a number
(such as “123”) that we could use the following statement
to convert the string to an integer:
int n = readString.toInt(); //convert readString
into a number
In our last lab, we saw how to use a PC to send data to
the Arduino to set the angle on a servomotor, but there
could be many different things we’d like to make a
servomotor do. For instance, if the servomotor is controlling
a camera-pointing system on a robot, we might want to
have the camera sweep across an area beginning at a
certain position and ending at another position. Plus, we
might want the sweep to occur at a certain speed.
The readString.toInt() function is very useful when we
send it the characters for a number. However, if we send it
non-numeric characters (like ‘a’ or ‘!’), it doesn’t know what
to do with them and the function returns 0. [You should
remember this if you want to receive a 0 as a valid number,
since you might actually be receiving a 0 as an error
indication.]
Here, we have three different actions to control on the
servo, and each action is associated with a specific number.
We might want to have the sweep speed be something
between the slowest speed at 0 and the fastest speed at
Let’s look at the situation where we want to send a
command to the Arduino that contains a number. When
the only thing we were sending to the Arduino was a
number representing the angle, that was no problem. So,
what if we also want to send a number representing the
velocity we want the pointer to move, or a number that
represents the angle we want the motion to start from, or
another number for the angle where the motion ends?
100. Then, we might want to make the servo start at 45°,
move to 135°, and do that at a medium speed of 50. If we
want to control this action from a PC, then we will need
four commands: one to set the start position; one to set the
end position; one to set the velocity; and one to tell it to
do the sweep.
We’ve learned how to send numbers; now, let’s learn
how to send commands along with those numbers.
Here we have three separate numbers, so we’ll need
some way to link them to a command for the action
associated with the number. For velocity, let’s use ‘v’ to
precede the number: vxxx where xxx is 0 to 100 (0 is
stopped and 100 is the maximum speed). For the start
angle, we will use ‘s’ as sxxx where xxx is 0 to 180. For the
end angle, we will use ‘e’ as exxx where xxx is also 0 to 180.
In the last chapter, we looked at how to send a number
This means that the Arduino will get the string v123, and
must be able to separate the v as the velocity command
58 June 2014