Knob Reading to Volts
Part of the Inputs section of Coddy's Arduino journey. Lesson 9 of 22.
A reading of 1023 means 5V, so each step is 5 / 1023 of a volt. Multiply to turn a reading into volts:
int reading = analogRead(A0);
float volts = reading * 5.0 / 1023.0;
Serial.println(volts);The decimal points matter. reading * 5 / 1023 uses only whole numbers, so it is integer division and drops the fraction: a reading of 512 gives 2, not 2.5. 5.0 makes the math keep its decimals.
The order matters too: reading / 1023 * 5.0 divides two whole numbers first, which is 0 for every reading below 1023.
Serial.println() shows a float with two decimals, like 2.50.
Challenge
EasyThe knob is on A0. Print the voltage on the pin:
- read A0 into an
int - turn it into volts in a
float:reading * 5.0 / 1023.0 - print the volts, then wait 100 ms
Press Run, turn the knob, then Check.
Try it yourself
void setup() {
Serial.begin(9600);
}
void loop() {
int reading = analogRead(A0);
// turn the reading into volts, print them, wait 100 ms
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Inputs
Practice on your own: Arduino playground