LESSON 10 – READING ANALOG PINS

To add to this lesson (or another lesson):  Need to add a button pressed task using pull-up or pull-down resistor.

 

ANALOG PINS

 

Analog pins are simply pins that have a voltage that can range anywhere between 0 and 5 volts.  This is unlike digital pins that are either at 0V (low) or 5V (high) but nothing in between.

 

PIN NUMBERS

 

Analog pins are numbered with an ‘A’ in front of them.  So analog pin #1 is called A1.

 

INPUT PIN

 

Just like for digital pins, analog pins need to be set to INPUT using pinMode.

 

Here is an example of making analog pin #2 (or simply, A2) an input pin:

 

     pinMode(A2, INPUT);

 

READING A VALUE

 

We use the analogRead() function to read the voltage on a pin.

 

Here is an example of reading pin A2’s voltage:

 

     int value = analogRead(A2);

 

In the example above, we are storing the returned value of analogRead(A2) in the variable value.

 

PERMILLAGE

 

Permillage is similar to the term percentage but out of 1000.  However, in computing, instead of 1000, it is out of 1024.

 

So, one half is equivalent to 512 permille.  The symbol for permillage is similar to percentage’s symbol.  It is o/oo.

 

ANALOG PIN VALUES

 

An analog pin’s voltage is read as a number between 0 and 1023.  It is a permillage of 5V.  In other words, 1023 is 5V.  0 is 0V.  512 is 2.5V.  And so on…

 

CALCULATING THE VOLTAGE

 

We can convert the result from analogRead() into voltage by simply dividing by 1023 and multiplying by 5.

 

EXAMPLE 1

 

If the reading is 745.  Then the voltage is:

 

          745/1023 * 5 = 3.64 volts

 

EXAMPLE 2

 

We can also make our program calculate the voltage:

 

     int voltPermillage = analogRead(A1);

     int voltage = voltPermillage / 1023 * 5;

 

 

TRY THIS…

 

PRACTICE 10-1

 

CIRCUIT

 

Create a series circuit that the 5V pin on the Arduino to a 1000 ohm resistor, then to another 1000 ohm resistor and then to one of the GROUND pins on the arduino.

 

Then insert a wire that connect pin A0 to between the two resistors.

 

ANALYSIS

 

Remembering our days from the Electricity unit, we know that the voltage drop over both resistors should be the same since they are both the same resistance.  So, we expect the voltage reading to be half of 1023.  So a reading in the area of 511 would be expected.

 

PROGRAM

 

Inside setup, prepare pin A0 for INPUT.

 

Inside loop, read A0 and output its value to screen.  Add a delay of 100 milliseconds between readings.

 

TESTING

 

Test your program to make sure you have a reading similar to expectations.

 

ALTERATIONS

 

Replace one of the resistors with an LDR.  You should see a difference in reading as you expose the LDR to more/less light.