I have been trying to find a way to serial.print a voltage value from a photodiode that I am using as a sensor. I have setup a voltage divider circuit with my sensor and a resistor and placing the output in analog pin 0. When i run my code the values are returned in ASCII format. I want to convert then to the true decimal value reading of my circuit (voltage reading) Can anyone help?? Here is the beginning of my code
When i run my code the values are returned in ASCII format.
Printing a value will always return it in ASCII format. If you want the real data then you have to split the value into bytes and send it as bytes with:-
val=analogRead(ledpin);
int a= val >> 8;
Serial.print(a,BYTE);
int b= val & 0xff;
Serial.println(b,BYTE);
But it is not decimal it is in the real value of the number. Printing in decimal always prints out ASCII. If you say what you want to do with it maybe we could work out what you need.
What I am doing is using the sensor to capture the change of voltage related to the change of intensity of light. I am using a photodiode to capture the intensity of the sun. When the intensity changes the output voltage from my circuit changes.
And what I want to do is to capture the actual voltage readings from analog pin 0 and serial print them. I know I can have them print as a binary, but i wanted to know if there was a code to convert them to a regular number like .58volts. instead of 0100101010, you get my point
analogRead provides 1024 steps between 0 and 5 volts. Each step is 5/1024 which is 4.88 milivolts.
To get the value in millivolts you multiply the analogRead value by 4.88
Arduino version 0013 supports floating point but only two decimal places so you are best off working in millivolts. But if you do want the result in volts, Multiply analogRead by .000488.
int value = analogRead(0);
double v = value * 4.88;
Serial.print("The value in millivolts is ");
Serial.print(v);
pinMode is for digital pins, and its not advisable to set digitalPin 0 as output. 0 and 1 are used by the serial port. pinMode is not needed for analogRead.
I am not sure what the OP intended with those pin numbers.
koyaanisqatsi you were correct thank you so much. When trying to take a reading through the analog pin the correct code shoud be
pinMode(ledpin, INPUT);
But if you do want the result in volts, Multiply analogRead by .000488.
mem thank you so much. your code was right on. I only changed the value by one decimal point. I am also using arduino -0013 so i can print a double. My final Code for this part of my project is
int ledpin=0;
int val=0;
void setup()
{
pinMode(ledpin, INPUT);
Serial.begin(9600);
}
void loop()
{
int value = analogRead(0);
double v = value * .00488;
Serial.print("The value in volts is ");
Serial.println(v);
delay(5000);
}
Thank you guys again for all your help. I will make sure to add my complete project to the website
A few minor points:
You may want to change the name of the variable ledpin to something more meaningful (perhaps: voltsPin)
And you could remove the line setting pinMode, its not needed for analogRead.