Reputation: 19
We are using an LM35 temperature sensor with the Arduino board pin A0 to A7, any one pin of them really. The problem is that I can not get the steady and accurate value in the serial window in the Arduino software. Below is the code I am using:
int pin = 0; // analog pin
int tempc = 0, tempf = 0; // Temperature variables
int samples[8]; // Variables to make a better precision
int maxi = -100, mini = 100; // To start max/min temperature
int i;
void setup()
{
Serial.begin(9600); // Start serial communication
}
void loop()
{
for(i = 0; i <= 7; i++) { // Gets 8 samples of temperature
samples[i] = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;
tempc = tempc + samples[i];
delay(1000);
}
tempc = tempc/8.0; // Better precision
tempf = (tempc * 9)/ 5 + 32; // Converts to fahrenheit
if (tempc > maxi) {
maxi = tempc;
} // Set max temperature
if (tempc < mini) {
mini = tempc;
} // Set min temperature
Serial.print(tempc,DEC);
Serial.print(" Celsius, ");
Serial.print(tempf,DEC);
Serial.print(" fahrenheit -> ");
Serial.print(maxi,DEC);
Serial.print(" Max, ");
Serial.print(mini,DEC);
Serial.println(" Min");
tempc = 0;
delay(1000); // Delay before loop
}
Upvotes: 0
Views: 1776
Reputation: 141
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
int samples =0;
float temprature = 0.0;
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a temprature
temprature += sensorValue * (500.0 / 1023.0);
// print out the value you read:
samples++;
if(samples >= 10){
temprature = temprature/10;
Serial.println(temprature);
temprature = 0.0;
samples = 0;
}
delay(50);
}
Upvotes: 0
Reputation: 4150
It would be nice to know what the issues are, but here are some things to consider:
analogRead
reading to some value, then convert it to temperature before printing. This way you avoid some potential floating-point rounding errors.float
sHowever, the bigger problem could be the inherent precision of your circuit. At 25 degrees, the LM35 outputs .25 V, which shows up as reading 51 on your ADC, and for every +1 degree change in temperature you get +2 reading from the ADC, so the ADC is accurate to 1/2 a degree. LM35 is accurate to 1/2 a degree at room temperature, so now you are at +1/-1 deg C accuracy, and this could be the cause of your jitter. If you are just measuring temperatures under 100 degrees C, you could use a 3.3 V reference for your ADC (again depending on which Arduino you use) which will give you a better precision.
However, you will always have some jitter.
Upvotes: 1