Displaying floating point numbers on serial monitor

Hi! I am recently working on my project of temperature measurement using thermistor. I am making a sketch for calculating the the value of resistance of themistor. From thermistor resistance, i can calculate the temperature. Here is code for that :

 int val;
 double voltage;
 double ratio;
 double R2;

void setup()
 {
  
 analogReference(EXTERNAL);
 // I got 0.5v applied on AREF pin through voltage divider
 Serial.begin(9600);// put your setup code here, to run once:

}


 


void loop()
 
{

 val = (analogRead(A0)- 46);
 // value of analogread as seen on serial monitor was 62, so val is 16 

 voltage = (val/1024)*0.5 ;
 ratio = voltage/5;
 R2= (ratio*4997)/(1-ratio);

 
 Serial.println("Analog value :");
 Serial.println(val);
 Serial.println("voltage");
 Serial.println(voltage);
 Serial.println("thermistor resistance:");
 Serial.println(R2);
 

delay(350);

}

The problem here is that value of variable "voltage" is too small since value of variable "val" is 16 and on serial monitor is see variable "val as 16 and "voltage" as 0.00
The value of variable "voltage" is actually a small number which is around 0.0078, iguess thats the reason. Is there any way to display such small floating number on serial monitor. Also, Same happens with variable "R2" . Where am i going wrong? any solution ?

Use

Serial.println(val, 5);

That tells Serial.println() to use 5 numbers after the decimal point

Bear in mind that the A/D resolution is 1 part in 1024 . You don’t get any more resolution using float numbers to manipulate an integer value .
I would stick to integers , just printing the decimal point in the right place so it reads ok to a human

darrob:
Use

Serial.println(val, 5);

That tells Serial.println() to use 5 numbers after the decimal point

Well, now i see 0.00000 for voltage

Your variable 'val' is an integer between 0 and 1023 inclusive.
1024 is an integer.

Therefore, using integer division, 'val / 1024' is ZERO.

Oh yeah ! Didn't see that.Its working great now! Thanks for helping out !

I'd use a little algebra to keep your values integers for as long as possible:

uint32_t val, x1, x2;
double r2;

x1 = val * 4997;
x2 = 10240 - val;
r2 = (double) x1 / x2;

gfvalvo:
I'd use a little algebra to keep your values integers for as long as possible:

uint32_t val, x1, x2;
double r2;

x1 = val * 4997;
x2 = 10240 - val;
r2 = (double) x1 / x2;

Thanks! btw what does : (double) x1/x2 mean in programming ?

The '(double)' is a Type Cast so that the division operation will be performed in floating point rather than integer arithmetic.

BTW, don't fool yourself. On an 8-bit AVR processor, using 'double' over 'float' gains you nothing. They are both IEEE 754, 32-bit floating point. I believe on an ARM, 'double' will get you 64-bit format.

okay. Thanks for letting me know that!