Hi, brand new user of Arduino and have a query about Project 3. I've read some other threads on it and I don't think i'm having some issues that I can see covered before.
I believe the circuit is wired correctly and the code is correct (see below). When it first runs I get what appear to be accurate temperature readings eg:
sensor Value: 146, Volts: 0.71, degrees C: 21.29
sensor Value: 146, Volts: 0.71, degrees C: 21.29
sensor Value: 147, Volts: 0.72, degrees C: 21.78
sensor Value: 146, Volts: 0.71, degrees C: 21.29
sensor Value: 145, Volts: 0.71, degrees C: 20.80
All 3 LEDS are off at this stage.
However, as soon as i pinch the TMP36 the temperature seems to spike, eg:
sensor Value: 148, Volts: 0.72, degrees C: 22.27
sensor Value: 182, Volts: 0.89, degrees C: 38.87
sensor Value: 252, Volts: 1.23, degrees C: 73.05
sensor Value: 241, Volts: 1.18, degrees C: 67.68
sensor Value: 240, Volts: 1.17, degrees C: 67.19
The LEDs all come on.
I am definitely using TMP36 not transistor. I think it's wired correctly (have rewired several times). The code I am using is:
const int sensorPin = A0;
// room temperature in Celsius
const float baselineTemp = 20.0;
void setup(){
// open a serial connection to display values
Serial.begin(9600);
// set the LED pins as outputs
// the for() loop saves some extra coding
for(int pinNumber = 2; pinNumber<5; pinNumber++){
pinMode(pinNumber,OUTPUT);
digitalWrite(pinNumber, LOW);
}
}
void loop(){
// read the value on AnalogIn pin 0
// and store it in a variable
int sensorVal = analogRead(sensorPin);
// send the 10-bit sensor value out the serial port
Serial.print("sensor Value: ");
Serial.print(sensorVal);
// convert the ADC reading to voltage
float voltage = (sensorVal/1024.0) * 5.0;
// Send the voltage level out the Serial port
Serial.print(", Volts: ");
Serial.print(voltage);
// convert the voltage to temperature in degrees C
// the sensor changes 10 mV per degree
// the datasheet says there's a 500 mV offset
// ((volatge - 500mV) times 100)
Serial.print(", degrees C: ");
float temperature = (voltage - .5) * 100;
Serial.println(temperature);
// if the current temperature is lower than the baseline
// turn off all LEDs
if(temperature < baselineTemp){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
} // if the temperature rises 2-4 degrees, turn an LED on
else if(temperature >= baselineTemp+2 && temperature < baselineTemp+4){
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
} // if the temperature rises 4-6 degrees, turn a second LED on
else if(temperature >= baselineTemp+4 && temperature < baselineTemp+6){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
} // if the temperature rises more than 6 degrees, turn all LEDs on
else if(temperature >= baselineTemp+6){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
delay(100);
}
(I copied and pasted the code from online as the code i typed had similar issues.)
My guess is it's to do with the sensorVal being incorrect? leading to voltage showing as incorrect, thus calculating an incorrect temperature? But i think the code is as it states in the book.
Any ideas?
Thanks and appreciate the help!