system
October 26, 2010, 3:43pm
1
I understand when I read the voltage on an input pin the ADC converts it to a number between 0-1023. (and how this makes it a digital representation of the infinite number of possible analog inputs)
Can someone help me understand how to reverse this to get the voltage value? 1023 = 5.0 0 = 0.0 etc.
system
October 26, 2010, 4:22pm
2
figure it out, had to modify the map function to return a float. This will give you the voltage on an analog input
void loop()
{
int raw = analogRead(analoginput);
float val = fmap(raw, 0, 1023, 0.0, 5.0);
Serial.println(val); //voltage
delay(1000);
}
float fmap(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
int value;
float voltage;
Setup()
{}
void loop()
{
value = analogRead(analogPin);
voltage = (value * 5.0) / 1023;
Serial.println(value, 3);
delay(1000);
}
system
October 26, 2010, 9:43pm
4
also...
map(raw,0,1023,0,50)/10.0;
cmiyc
October 27, 2010, 5:08am
5
@robtillaart 's code has a bug...
Serial.println(value, 3);
This line should be...
Serial.println(voltage, 3);
Thanx James, you are right
system
October 27, 2010, 5:24pm
7
It may seem a minor point, but we might as well get it as close as we can.
The calculation for Vin is given by
Vin = (ADC_reading * Vref) / 1024.0
The denominator is 1024, not 1023.
The voltage indicated by a full scale reading (1023 decimal = 0x3ff hex) is one LSB less than Vref.
Regards,
Dave