Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

Under Development

Analog Input

The real world is not digital. Considering temperature fluctuation as an example, it changes within some range of values and generally does not make abrupt changes over time. We often measure environmental parameters like temperature, light intensity, or whatever using analog sensors. These resulting signals are stored as sequential digital data.

Analog Signals

An example would be to measure and record room temperature every minute. One could watch a thermometer and write down the readings. The sequence of data would look like (in Celsius): 20.3, 20, 20.5, 21, 20.8 ...

We face a number of issues here. First of all Arduino cannot understand temperature as such, it needs to be translated into an electrical value. Second that electrical value has to be translated into a number that can be processed within Arduino.

As said, microprocessors cannot handle temperature values as humans do. We need to translate that to something the microchip can read. In order to do so, we can use sensors that will transform, in this case, temperature into a voltage value between 0 and 5 volts. This values are different from the HIGH and LOW that characterize digital signals, because they can take any value between 0 and 5 volts. 0.3 volts, 3.27 volts, 4.99 volts are possible values.

This is what we call an analog signal. It differs from the digital ones in being able of taking many more than just two values. The amount of possibilities depends only in the capabilities of the processor/microcontroller you are working with in each case. As we will see later, Arduino can only distinguish 1024 different levels between 0 and 5 volts.

Bringing Analog Signals into Arduino

Each sensor can translate a range of physical world values into electrical values. Let's imagine that our temperature sensor reads between 0C and 100C. Typically it should then assign 0 volts to 0C and 5 volts to 100C. Thanks to this we can easily translate levels of voltage into temperature and viceversa.

The next issue has to do with the finite resolution of digital technology. Arduino has the possibility of reading values from the real world, which have been translated into electrical values between 0 and 5 volts. Try now to answer this question for a second: how many voltage values are there between 0 and 5 volts? The answer is simple: endless. Imagine two voltage values in the range we are working with, that are as close as possible, e.g. 3.4 and 3.41 volts. It is possible to have endless values inbetween those two: 3.401, 3.403, 3.402539 ...

This means that we would need a processor with the ability to represent endless numbers, but Arduino cannot. Therefore, we speak about different levels. In particular Arduino divides the range of 0 to 5 volts into 1024 different voltage levels or intervals. 0 volts is in the interval 0, and 5 volts in the interval 1023. In this way, 2.5 volts would be in the interval 511 as well as 2.52 volts or 2.5103.

This operation of translating an analog voltage value into different levels is what we call Analog to Digital Conversion. One small hardware part inside the microprocessor that comes with the Arduino I/O board is dedicated to translate analog voltages into these values, it is the Analog to Digital Converter also called ADC.

Example

/*

  Analog Input
 Demonstrates analog input by reading an analog sensor on analog pin 0 and
 turning on and off a light emitting diode(LED)  connected to digital pin 13. 
 The amount of time the LED will be on and off depends on
 the value obtained by analogRead(). 

 The circuit:
 * Potentiometer attached to analog input 0
 * center pin of the potentiometer to the analog pin
 * one side pin (either one) to ground
 * the other side pin to +5V
 * LED anode (long leg) attached to digital output 13
 * LED cathode (short leg) attached to ground

 * Note: because most Arduinos have a built-in LED attached 
 to pin 13 on the board, the LED is optional.

 Created by David Cuartielles
 modified 30 Aug 2011
 By Tom Igoe

 This example code is in the public domain.

 http://arduino.cc/en/Tutorial/AnalogInput

 */

int sensorPin = A0; // select the input pin for the potentiometer int ledPin = 13; // select the pin for the LED int sensorValue = 0; // variable to store the value coming from the sensor

void setup() {

  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  

}

void loop() {

  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);    
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);  
  // stop the program for <sensorValue> milliseconds:
  delay(sensorValue);          
  // turn the ledPin off:        
  digitalWrite(ledPin, LOW);   
  // stop the program for for <sensorValue> milliseconds:
  delay(sensorValue);                  

}

Why 1024 values?

Numeric Conversion

Binary

Decimal

0

0

1

1

10

2

11

3

100

4

101

5

110

6

111

7

1000

8

1001

9

1010

10

...

1111111110

1022

1111111111

1023

You could ask then, why 1024 values? This is a choice by the microprocessor's manufacturer, who decided to create a 10-bit ADC. Numbers inside microprocessors are represented as sequences of digital values. But this needs a more in depth explanation.

Humans represent numbers in the decimal scale. We have a series fo ten differen symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. And we can represent any amount using those. When we count, we start in the same order we wrote them and, once we reach the last number, we start to make combinations of symbols. After number 9 comes 10 which is a combination of two symbols. After 453 comes 454 which just takes the next number ...

Within digital machines, there are only two symbols: 1 which represents HIGH or 5 volts, and 0 which represents LOW or 0 volts. This means that after number 1, we get numer 10. But 10 for the microcontroller is only 2 for a human!! We call this way of counting Binary and it is used by most of the state-of-the-art computers and microprocessors. The table on the right will help you to understand this better.

Each one of the 1's and 0's in the binary numbers is what we call a bit. A group of four is a nibble and a group of eight is a byte. Other groupings are called words and they can be of many different lengths. Arduino's ADC represents the data readings in words of 10 bits. As you see in the table, with 10 bits the maximum number we can represent is 1023.

The use of the same type of symbols to represent numbers as in the human language, can be misunderstood. This is why when writing binary numbers we usually write all the zeroes on the left hand side. E.g. in a 10 bits number, the decimal 3 would be the binary 0000000011.

Examples