# How to change the value within a variable using math? [closed]

I'm up to Lesson 11 in the TopTechBoy tutorial: https://www.youtube.com/watch?v=afurKLOqqSg

I can use the analogRead() to fill a variable with a value and Serial.printld it out, but I want to be able to increase that integer by × 4.887585533

int pinV; // This empty variable will be filled with an integer value between 0 and 1023.
int receiverPin=0; // This is the RX pin, a ‘receiver’ that connects to the USB to Serial interface.
int ResultsPerSec=500; // The Arduino reads and prints out a result every half second.
float printingValue; // Another empty variable that is given the pinV value to multiply and print.
void setup() {
Serial.begin(9600);
pinMode(receiverPin,INPUT);
}

void loop() {
pinV=analogRead(receiverPin);
// Each Volt has a value of 204.6 (4.88 mV per integer of the 1023 available)
printingValue = (pinV *4.887585533);
// 5,000 mV divided by 1023 possible values = is 4.887585533 mV per integer
// The Arduino can count from 0 to 5 Volts in 4.887585533 mV increments.
// 1,000 mV divided by 4.887585533 mV = 204.599999989 integers out of the 1023 available equal 1V
// Each integer of the 1023 possible values that analogRead() outputs can be increased by 4.887585533
// This shows the real Voltage in millivolts (mV).
Serial.println(printingValue);
delay(ResultsPerSec);
}


This is actually acceptable as far as the IDE is concerned, but it doesn't work as intended. I'm getting outputs in the thousands, when it should be printing 0 in the Serial monitor. How do I do this properly?

## 1 Answer

I just got a reply from someone from one of the threads on https://8ch.net/tech/catalog.html

It turns out that the Digital pins (0,1,2,3,4,5,6,7,8,9,10,11,12,13) are not capable of reading Analog voltages. Pins A0 to A5 need to be used instead. Also, if not voltage is supplied, the Arduino will print random numbers in the Serial Monitor.

• 0 as parameter of analogRead is A0. you used A0. if pin is not connected to a circuit or ground it is called 'floating' – Juraj May 14 '18 at 18:42
• The page you linked to clearly shows analogue pin A0 being used for the reading. You seem to have thought that pin 0 (on the other side of the board) would be an acceptable alternative. – Nick Gammon May 15 '18 at 0:41
• Its great how I can turn my Arduino into a voltmeter (As long as its below 5V). You can touch two wires on either ends of an AA battery and and see its voltage. But I wonder if the math is correct? Its been said that the float can only hold up to 7 numbers after the decimal (4.887585533 counts nine). – Maitland May 15 '18 at 9:49
• 1. The voltage you should print is 5e3/1024*analogRead(A0). Ignore the tutorial using 1023: the datasheet is the only authoritative reference. 2. Floats do not hold decimals: they are binary, whith a machine epsilon of 2^(−23) ≈ 1.19e-7. The ratio 5e3/1024 is an exact float. 5e3/1023 is not, and the compiler rounds it to the nearest float, namely 4.88758563995361328125. The rounding error is inconsequential given the low resolution of the measurement. – Edgar Bonet May 15 '18 at 10:42
• gph.is/1dogvB3 – Maitland May 15 '18 at 12:10