Delicious Bookmark this on Delicious      HOME > > ARDUINO LANGUAGE COURSE  t.o.c.

Introduction to the Arduino analog to digital converter

This is one of a collection of pages which, together, attempt to show you "everything" about the Arduino's programming language.

There is a page for you with more information about the project in general, and the way these pages are organized, if you want that.

Please visit my page about power browsing notes sometime.

This page, and the software it references, ©TK Boyd, 1/2010.


The Arduino comes with built in analog to digital converters. Even a little Arduino has six, and bigger ones have more. And they convert the analog signal to a number on a 1024 step scale, i.e. the resolution isn't too shabby.

Let's start with "digital" and "analog". The former is a "world" where everything only comes in two flavors. Traditionally, we speak of "zero" and "one" as the two flavors, but we could also speak of up/ down or left/right or on/off. Digital is Good because things can be made more reliable when there are only two possibilities. Digital is Bad because it is so unlike the Real World. All sorts of clever wheezes become necessary to make digital computers helpful in the real world.

Analog is the alternative. Instead of just two states, we have many. A simple desk lamp that can only be "on" or "off" is a "digital" device. A light on a dimmer, which can be "off", "dim", "medium", "quite bright", "fully bright"... and everything in between... is an "analog" device. My hi-fi can be off, can play music softly, at a medium volume, loud, is an analog device.

Don't let the use of "digital" in "digital clock" fool you. That's not the sort of digital which is the complement of "analog".

Temperatures are an analog quantity that you may want to work with via your Arduino.

The Arduino's analog to digital converters take a voltage which can be zero, 1 volt, 2 volts... 5 volts, and in between (but not, with simple answers, higher), and convert the analog voltage into a digital form, inside the Arduino, from where you can use it in programs. More on that in a moment.

There are many, many sensors available which can turn SOMETHING into a voltage. There are analog temperature sensors, for instance. When you hook them up properly, one wire from their circuit has a low voltage in it when the sensor is cold, and higher and higher voltages as the temperature of the sensor is increased. (The Dallas 1-Wire temperature sensors, by the way, do NOT work like this. They are a story for another day. Very nice devices!) Other sensors produce a range of voltages when subjected to a range of light levels, mechanical stress, humidity, etc, etc.

By the way: When you connect the output of the sensor, or any other source of voltage, to the analog input of the Arduino....

a) Be certain the voltage will not be outside zero to the voltage of your Arduino, typically 5v.

b) Don't worry about significant current flowing into (or out of) the Arduino's analog input. It is a "high impedance" input.

In this tutorial, we are not going to use a sensor; we're just going to use a potentiometer... a thing like a radio's volume control. Also known as (roughly speaking) a variable resistor. It is a "stand in" for a sensor. It "looks" like one to the Arduino, but we can turn it "up" and "down" more easily than we can make a sensor change the voltage it is outputting.

So: Hooking up our potentiometer: Potentiometers come with three wires. One is the "wiper". The other two connect to the ends of a long "resistor" inside the device. A 5k pot or higher will be suitable for our needs. Don't worry if it is "log" or "lin". It doesn't need to handle high currents.

Later we will connect the wiper to the Arduino's "A0" analog input. Connect one of the other wires to the Arduino's ground, and the remaining wire to the Arduino's 5v.

In my early computing days, I spent many hours struggling with analog to digital converters. (ADCs). In those days, they were complex external extra electronics. The Arduino's internal ADCs are just fine for a lot of the stuff we want to do. The following little program will read the first ADC, the one looking at the A0 input. The only trouble is that we won't see what the Arduino sees!! (We'll solve that shortly.)

28 Dec 09, 15:38

Expects an analog input to "A0"

int intAnValue=0;
const byte AnInputPin=0;

void setup()

void loop()

The only really interesting line in all of that is....


... and this is its story:

intAnValue is a variable. It is of data type "int", which means it can hold whole numbers from -32,768 to 32,767. We created the integer, and gave it an initial value (zero) with the....

int intAnValue=0;

... line early in the sourcecode. Because this variable declaration is outside any of the program's functions, it is a "global" variable, one shared by all parts of the program. (Global variables are generally to be avoided, but in a little program like this, there is no harm in creating a few.) (By the way, "variable declaration" means "the declaration of a variable", not a declaration that somehow varies. English has so many ambiguities!)

Variables, I hope you remember, are places we put things. If we said....


... we would be storing 52 in the variable named "intAnValue".

However, we have said....


... so all of the material to the right of the equals sign must "boil down" to a number... and it does!

The word "analogRead" is a keyword. It says, "go look at one of the analog inputs. Which input is determined by what is in the parentheses after the keyword. In our little program, we have AnInputPin, but earlier we put zero in that, so, the analogRead process is going to look at the first analog input, the one called A0.

If the pin is connected to 0 volts, intAnValue will be filled with zero. If the pin is connected to 5 volts, intAnValue will be filled with 1023. In between voltages will give rise to in between numbers.

Fine. If you say so. How can we see if that's true?

Look at the following. It is a combination of the material we presented a moment ago, plus some stuff from our experiments with LEDs. Remember that whatever is in the "loop" function happens over and over again.

28 Dec 09

Expects an analog input to "A0"
Also needs 3 LEDs, merely to display crudely result of analog read.*/

int intAnValue=0;
const byte AnInputPin=0;

const byte LED0=11;//LED for LS bit connected here
const byte LED1=12;//LED connected here
const byte LED2=13;//LED for MS bit connected here

void setup()


void loop()
if (intAnValue>299) setLEDs(0,0,1);
if (intAnValue>500) setLEDs(0,1,0);
if (intAnValue>700) setLEDs(1,0,0);
delay(500);//To slow down how rapidly readings are taken.


void setLEDs(byte bL2, byte bL1, byte bL0)
   if (bL0==0) digitalWrite(LED0,LOW);
               else  digitalWrite(LED0,HIGH);
   if (bL1==0) digitalWrite(LED1,LOW);
               else  digitalWrite(LED1,HIGH);
   if (bL2==0) digitalWrite(LED2,LOW);
               else  digitalWrite(LED2,HIGH);

Within "loop", the computer, via the analogRead keyword, looks at the voltage on the A0 pin, and puts a number in the variable intAnValue.

Lets say that the number on this occasion is 550.

Due to.... setLEDs(0,0,0); ... very briefly, all the LEDs will be off.

Then, almost immediately, the program does....

if (intAnValue>299) setLEDs(0,0,1);

... and as the number in intAnValue is 550, the right hand LED, very briefly, goes on.

Then, almost immediately, the program does....

if (intAnValue>500) setLEDs(0,1,0);

... and as the number in intAnValue is 550, the middle LED goes on, and the right hand LED goes off.

Then, almost immediately, the program looks at...

if (intAnValue>700)...

... and as it is NOT, the state of the LEDs is not changed.

And then the program does delay(500); That gives our eyes time to register the fact that the middle LED (only) is on.

And thus the first pass through "loop" finishes. The program then goes back and does it again. If the potentiometer has not been changed to a new setting, the voltage at A0 is unchanged, and the display on the LEDs is unchanged.

Change the setting on the potentiometer, and the voltage on A0 changes, and which LEDs are on changes.

Ta da!

You're becoming a stronger and stronger Arduino programmer day by day!

   Search this site or the web      powered by FreeFind

Site search Web search
Site Map    What's New    Search

The search engine is not intelligent. It merely seeks the words you specify. It will not do anything sensible with "What does the 'could not compile' error mean?" It will just return references to pages with "what", "does", "could", "not".... etc.

SPELL your search term properly. When I review search logs, it is amazing how many people ask the engine to search for something meaningless.

Why does this site cause a script to run? I have my web-traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try it, check out their site. And if there are Google ads on the page, they are run with scripts, too.

Click here to return to Arduino COURSE table of contents.
Click here to go to the author's home page.

Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.

Here is how you can contact this page's editor. This page, and the software it references, ©TK Boyd, 1/2010.

Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at