In the last lesson you learned about using the analogRead() function to collect data from a sensor connected to one of the Arduino’ analog pins. The range of data we received from the analogRead() function, was mapped from 0 to 1023. What if we wanted to know the actual voltage being applied at the pin?
You Will Need
Potentiometer (any resistance range will work)
Jumper Wires – at least 3
Place your potentiometer into your breadboard.
Run a jumper wire from the 5-Volt pin of the Arduino either one of the outside pins of your potentiometer.
Run another jumper wire from one of the ground pins on your Arduino (labeled GND) to the other outside pin of your potentiometer.
Run the final jumper wire from pin A0 on your Arduino to the middle pin of the potentiometer.
Plug your Arduino into your computer.
Open up the Arduino IDE.
Go to File, Examples, 01.Basics, ReadAnalogVoltage
Click the Verify button on the top left side of the screen. It will turn orange and then back to blue once it has finished.
Click the Upload button (next to the Verify button). It will turn orange and then back to blue once it has finished.
On the menu bar, go to Tools, Serial Monitor – this will open the Serial Monitor window – you should see numbers rolling down this screen.
Now adjust the knob of your potentiometer and watch the serial monitor window, the numbers should adjust between 0 and 5.
This sketch does the exact same thing the last lesson covered except for one important change. It takes the reading provided by the analogRead() function and converts it into the actual voltage value at the respective analog pin. Let’s start from the top just to review what is taking place…
We have no variable to declare and initialize at the beginning of the sketch so we jump right into the setup() function. Inside the curly braces of setup() we begin serial communications by setting the baud rate. This is done using the function
That is all there is to the setup of this sketch. the next block of code is loop(). Inside the curly braces of loop() the first thing we do is read the value at analog pin A0 and assign it to an integer variable called sensorValue.
int sensorValue = analogRead(A0);
Once we have recorded this value, we now want to convert it to an actual voltage. You will recall that the range returned by the analogRead() function is between 0 and 1023. We want this to reflect the actual voltage at the pin – which is between 0 and 5 volts depending on where we have our potentiometer turned to. So lets take a look at how we might accomplish this…
float voltage = sensorValue * (5.0 / 1023.0);
The first thing we encounter is a new data type – called float. A float is just a number with a decimal point; say for example 3.14 or 2.17781778. Floats, also called floating point numbers, can be huge in value, and take much more time for the Arduino to churn through than integers – this is why they are avoided unless necessary.
We want a float in this case because it will give us more resolution than an integer.
So what is that calculation anyway – it looks kind of confusing. The numbers on the right are just a conversion factor. We want to convert one scale to another.
When we multiply this small number times the sensorValue variable, it will scale down the range from 0-1023 (which is the range that analogRead() returns) to the range 0-5 which is the actual range of the voltage.
Once we have this new value, we assign it to the variable voltage.
Lets recap the program…
Read the sensor value at an analog pin.
Assign this value to a variable.
Convert this value to a voltage
Save the voltage measurement to another variable
Well, we print it back to the serial monitor window so we can see what the voltage is at our pin.
The loop will start over again, it will sample a new value at the pin, it will convert that value and print it, and loop and loop and – you get the idea.
*Try On Your Own*
Switch from the using the 5-volt pin on the Arduino to the 3.3-volt pin. What do you think the new conversion factor should be?
Can you change the range from 0 to 100?