Units of displayed readings

Hi,

I'm new in arduino world
I wrote this sketch to read the light intensity as voltage and display readings in serial moniteor

int SensorPin = A0;
int SensorReading;


void setup(void) 
  // serial.begin command to connect with PC
 { Serial.begin(9600); }


void loop(void)
{
       
 
  // the main code that will repeated
  SensorReading = analogRead(SensorPin);
  Serial.print("Voltage = ");
  float voltage= SensorPin * (5.0 / 1023.0);
  /*float command to take decimals 
  into account more prices than integer*/
  Serial.print(SensorReading);
  Serial.println(SensorPin, DEC);
  //show decimals in serial monitor
      delay(60L*1000L);
  
  }

and these are the readings
V=7314
V= 014
V=014
V=1014
V=914
V= 014
V= 014
V=614
V=6814
V= 2314

actually that's not make sense for me! what's the unit of these readings? is it miiliVolt?

This:

  Serial.println(SensorPin, DEC);

is giving you a suffix of 14 on all your 'readings'. Get rid of it.

Then you'll be printing the direct output of the ADC. Better if you print your voltage variable instead.

...and, of course, correct the formula for voltage so that it uses the right variables :wink:

wildbill:
This:

  Serial.println(SensorPin, DEC);

is giving you a suffix of 14 on all your 'readings'. Get rid of it.

Then you'll be printing the direct output of the ADC. Better if you print your voltage variable instead.

i need print the voltage vairable, but I'm confused how to do this in unit (volt)?

i neglect Serial.println(SensorPin, DEC);

then!!

igendel:
...and, of course, correct the formula for voltage so that it uses the right variables :wink:

pardon me, can you explain more?
I don't know what's the correct formula.

I don't know what's the correct formula.

You need to think this through. As you learn programming you will need to train yourself to think logically.

What is SensorPin? Will it ever change?

What is the SensorReading? Where to do you get its value from? How do you get the value? What do you expect it to be? What do you think Serial.print (SensorReading) will give you.

You have declared a float type variable named voltage but you never use it. Why not? Why did you use that formula to become voltage?

What should go in the brackets of Serial.print() to print out the voltage?

I think that's it :slight_smile:

 SensorReading = analogRead(SensorPin);
  Serial.print("Voltage = ");
  float Voltage = float(SensorReading) * (5.0 / 1023.0);
 
  Serial.print(Voltage);
  Serial.println("V");
 
      delay(3000L);

the outputs values must be in (Volt) now, right?

Suaad:
I think that's it :slight_smile:

 SensorReading = analogRead(SensorPin);

Serial.print("Voltage = ");
 float Voltage = float(SensorReading) * (5.0 / 1023.0);

Serial.print(Voltage);
 Serial.println("V");

delay(3000L);




the outputs values must be in (Volt) now, right?

I don't know why so many people here have a penchant for being sarcastic and offering no help.

What you need to know is how the A/D converter works in order to use it properly.

Normally, it takes in a DC voltage between 0.0 and 5.0 volts. Since it's a 10 bit AD, it's results can vary from zero to 2^10 (2 to the 10'th power) which is 1024-1 (1023).

So, before you make any calculations, if you put in 0 volts, you get a reading of 0. Put in 2.5 (half of 5) and you get 511 or 512 (half of 1024). Put in 5 volts and you get 1023. Put in any more and you still get 1023 - can't go any higher (and of course you burn out the input, but that's not the point).

OK so now you know that 0 volts is AD 0 and 5 volts is AD 1023 and it's a straight line linear function.

Certainly you know where to go from here?

Say you want to make a display of some sort that shows "0" with 0 volts input and "5" with 5 volts input. What do you have to divide 1023 by to get 5? Just do "factor = 5 / 1023" and factor is 0.00488... which in effect means "there's 0.005 volts for every AD count".

See now? Just do this (pseudo-code):

factor = (5 / 1023); // conversion factor
loop {
    raw = read_ad; // get raw reading 0...1023
    actual = (raw * factor); // convert it to 0...5
    display actual; // display actual volts
}

You could do it the other way around too...

[b]factor = (1023 / 5);
actual = (raw / factor);[/b]

......same thing, just algebra from the other direction.

Make sense?