Society of Robots - Robot Forum
Software => Software => Topic started by: galannthegreat on September 22, 2010, 09:21:41 PM
-
I am currently working on a data logging project right now with an Arduino and I have some code working, but I want to make it a bit more accurate. I am wondering if people know of any ways I can do that.
Here is my code:
#define SENSOR 0 // select the input pin for
// the LM335A temperature sensor
float val = 0; // variable used to store the value
// coming from the sensor
float val2 = 0;
float celcius = 0;
void setup()
{
Serial.begin(9600); // open the serial port to send
// data back to the computer at
// 9600 baud
}
void loop()
{
val = analogRead(SENSOR); // read value from the sensor
val2 = val/2;
celcius = val2 - 274;
Serial.println(celcius); // print the value to
// the serial port
delay(1000); // wait 1 second between each
// each reading
}
Thanks, Kurt.
-
What sensor are you using? Also, what do you want to make a bit more accurate? Please clarify
Sorry, I didn't read the title.
The LM335 has an output of 1mv/K
Your code uses analogRead, which uses a 10-bit ADC to convert the 0-5v into 0-1024 bits.
As such, you need to start by converting the reading back into volts:
val *= 0.0048828125;
Then, divide the voltage by 100 to get 10mV resolution. At this point, you should cast to a shorter variable to avoid floating-point operations
int deg = val / 100;
Lastly, subtract 273.15 degrees to get degrees Celcius from Kelvin:
deg -= 273;
In summary:
deg = (int)(analogRead(SENSOR)*0.0048828125/100)-273;
Make sense?
-
Thank you very much for that :), it helped me figure out what I wanted to do.
Here is the new code: (I didn't follow exactly what you did but I did the same sort of thing)
#define SENSOR 0 // select the input pin for
// the LM335A temperature sensor
float val = 0; // variable used to store the value
// coming from the sensor
float val2 = 0;
float deg = 0;
float celcius = 0;
void setup()
{
Serial.begin(9600); // open the serial port to send
// data back to the computer at
// 9600 baud
}
void loop()
{
val = analogRead(SENSOR); // read value from the sensor
val2 = val * 0.00489; // take SENSOR value and multiply it by 4.89mV
// and store value to val2
deg = val2 * 100; // multiply by 100 to get degrees in K
celcius = deg - 273.15; // subtract absolute zero to get degrees celcius
Serial.println(celcius); // print the value to
// the serial port
delay(1000); // wait 1 second between each
// each reading
}
P.S. It also turns out that I should precisely calibrate the sensor to get more accurate results, but it is doing what I want. YAY!