 12 – ARDUINO AND ANALOG PINS

Objectives

• Introduce potentiometers.
• Understand Analog-to-Digital Converters.
• Learn how to use Arduino analog pins

Bill of materials Arduino Uno or equivalent. We can use any other Arduino board in this chapter. We will also need a PC computer with the Arduino IDE properly installed and set up A solderless Breadboard. A LED diode. A 10 KΩ potentiometer A 30 Ohm resistor Some jumper wires

Potentiometers

So far we have ever used fixed resistors that have resistance values that are essentially constant. But sometimes it is useful to have a variable signal to control our circuit. Think of the control to adjust the volume of an stereo or of the dial that tunes a radio station.

A potentiometer is simply a device that provides a variable resistance, by turning its shaft.

There are potentiometers in so many sizes, shapes and colours as you can imagine, but at the end they are nothing but a fixed resistor (10 kΩ in our case) and a mechanism to allow the wiper (the sliding contact) to move over the resistive material when the shaft is rotated, so we can read a portion of that value. That’s why a potentiometer provides always three terminals in a row. The two outermost terminals behave as a fixed resistor, that has the same resistance value as the full scale of the potentiometer (10 KΩ in our case), and the central terminal changes the resistance depending on the rotation of the shaft.

We are going to assemble a protoype using the following circuit schematic diagram. Here the potentiometer is labelled Pot1. The idea is to connect +5V and GND to the outermost terminals of the potentiometer (it doesn’t matter to which of them because resistors have no voltage polarity), and then the central terminal to the positive terminal of a LED diode. The negative terminal of the LED diode is wired to ground via a limiter resistor.

This way, when we rotate the shaft of the potentiometer we modify the voltage applied to the input of the LED diode, that will take values from 0 to +5V (although it seems to be strange is very easy), and so we will have a current intensity regulator for the LED diode.

• Applying Ohm’s law and using a 10 KΩ resistor the current intensity in the circuit is: 5 V / 10.000 Ω = 0,5 mA. It is very little to be able to turn on the LED diode, which requires a current intensity of about 20 mA. So for most of the rotation of the potentiometer the LED diode will be off.
• Important: Remember not to remove the R1 resistor. Although the potentiometer limits the current intensity, there is a time when its resistance value reaches zero and your LED will burn out on duty.

CIRCUIT WIRING DIAGRAM

The assembly in the breadboard will be similar to this. We use Arduino just to power the circuit. You will see that the intensity of the light in the LED diode varies continuously by rotating the shaft of the potentiometer. • Remember that, due to the high resistance value of the test potentiometer, most of the time (while we rotate the shaft) the LED diode will be off.
• Note that in this case we use Arduino as a voltage source just to power the circuit.

We can use Arduino to interact with the outside world applying HIGH/LOW signals in the digital pins and using PWM we can quite successfully simulate analog signals in some of these pins as well.

We have also seen how to detect whether a button is pressed, defining as inputs the digital pins. But often the sensors we use to watch over the outside world provide us an analog signal. This applies to sensors of temperature, distance, pressure, PH, current intensity in a circuit or water flow in a pipe.

To read this type of continuous signals we need a analog to digital converter (ADC) because they allow us to read the value of an analog signal at a given moment.

These converters takes a sample of the current value and return its instantaneous value, measured in volts. By repeated reading of samples over time we can rebuild the original signal more or less accurately, depending on the accuracy of our measure and the speed at which you can take those samples. Arduino UNO provides six analog to digital converters, from A0 to A5, labelled as ANALOG IN. Let’s see how to use the analog input using the following wiring diagram. The ends of the potentiometer are wired to ground and +5V, respectively, and the central pin (the variable) is wired to the Arduino analogical pin A5. • It seems a good time to note that ADC converters read voltage values instead of resistance values, so we read a voltage drop across the potentiometer as we turn the shaft.

The first thing to note is that we don’t need to declare inside the setup() function that we are going to use an analog port. And the second thing is that in order to take an analog sample from the pin A5, we will use the analogRead() function:

`int Val = analogRead(A5);`

• The analog to digital converters in Arduino UNO and Mega have 10 bit resolution, so they will provide us values ranging from 0 to 210 = 1.024 values when the voltage applied ranges between 0 and +5V. However, Arduino DUE has 12 bits ADC converters so it will provide values ranging from 0 to 1012 , that is, 4.096 values.  Arduino DUE has better resolution but it can only accept voltage values ranging from 0 to 3,3 volts.
• Be sure not to use sensors that provide more than 5 volts maximum (in Arduino UNO and Mega), otherwise you will damage the main chip in the Arduino board.

Let’s write a program that reads the value from the pin A5 and send it to the serial port in order to be watched in the console.

Try out this program!:

```void setup()
{
Serial.begin(9600);    // Serial port initialization
}
void loop()
{
delay(200) ;
}```

After uploading the program, open the serial monitor and you will see how the readings vary in a continuous way as you turn the shaft, indicating the position of the wiper. The readings show the voltage drop in volts across the potentiometer. I can’t help suggesting you this test: Unplug the potentiometer from the pin A5 and watch the results that Arduino sends to the serial monitor console. Where does these values come from?

• As the pin A5 is not connected to any valid reference, we say that it is in a floating state and the values that it reads are just a sample of this incoherence. Actually what your Arduino is doing is to capture radio frequency random noise and try to make sense, but it is not doing it fine, as you can see.
• However, under normal conditions the read values will be relatively low. Would you like these oscillations grow in value? It is easy, just connect an antenna. We can use a simple jumper wire connected to the pin A5 and leave the other end unconnected (or you can take it between with fingers, this way you will be the antenna yourself). You have just built the most useless radio frequency receiver in the world.

A LAST COMMENT

As we said in the previous chapter, the fidelity with which we can take samples of an analog signal depends, basically, on the resolution of the sample and the speed at which we can take these samples, that is, the sample rate.

We already said that the Arduino family provides analog to digital converters with 10 bits of resolution (210 = 1.024), 12 bits (212 = 4.096) in the case of the Arduino DUE. But up to now we have not seen at which rate we can take samples with our Arduino. Let’s try it out with this same circuit.

C++ provides the millis() function that returns the elapsed time since we power up Arduino and we can use it to check how many samples per second we can take.

```void setup()
{
Serial.begin(9600);
}

void loop()
{     unsigned long T;
int n = 0 ;
T = millis();

while (millis() <= T + 1000)      // We read the values until one second passes, that is, 1000 milliseconds
{
n++ ;                         // We increment the counter every time we read
}
Serial.println(n);
}```

• We have used an unsigned long data type to store the value returned by millis() because Arduino uses this data type internally for its clock. It would be wrong to use the integer data type to store the value returned by the millis() function because its maximum value is 32,767 milliseconds and the counter would overflow in little more than 32 seconds.

If you run this program in Arduino UNO it will take, more or less, 8.940 samples or readings per second. It is not that bad.

It is suitable for sampling signals that do not vary too quickly over time, as are almost all the usual sensors in the industry, but it will fall short if you want to sample audio signals.

• To play with audio you’d better use an Arduino DUE. Its clock speed is 4 times faster (you will need it), is able to take samples at 40 KHz (audio speed) and have true analog to digital converters, ADCs.
•  In fact, it is not complicated to increase the sampling rate up to 20.000 samples per second using an Arduino UNO, but in that case we would have to bypass Arduino and program directly the main chip, the Atmel AtMega328. This is no time for it, but it is possible.

Summary

• We know how a potentiometer works.
• We have introduced the basic concepts on analog to digital conversion.