Go Down

Topic: reading voltages with decimal value and serial printing it (Read 294 times) previous topic - next topic

krishna_madan

i want to read a voltage say 4.5 and print it in serial print as 4.5.Iam using the code below but it is printing only the integer 4.
The code is ..

int volt_in_an=A0;    // select the input pin for measuring analog i/p voltage
double volt_in=0;
void setup()
{
   //pinMode(pwm_out, OUTPUT);   //sets the pin as output
   Serial.begin(9600);
}
void loop() {
   volt_in=analogRead(volt_in_an);
   double volt=map(volt_in, 0, 1023, 0, 5);
   Serial.println(volt,DEC);   // displays analog input voltage  value 0 to 5
}


suggestions are welcome....

PaulS

Quote
but it is printing only the integer 4.

No. It is printing the float value 4.0000000, but, since the fractional part is 0, it is not printed.

The analogRead() function returns an int. Storing that in a float won't improve its accuracy.

The map function takes a collection of integer values and returns an integer result. Storing an int in a float won't restore the truncated portion.

Go Up