Sensor value for TMP36 suddenly spikes and stays high

Hello!

I was Googleing the solution for this problem but nothing came up but an old forum thread which stayed unanswered. There was message saying that since that thread is too old I should create a new topic instead. So here I am.

So, I am building the Love-O-Meter project from the Arduino Project book. This project uses the TMP36 sensor and three LEDs which are lighting up one by one when the sensor is sending a higher Sensor Value.

First, I wrote the first half of the code and everything looked fine (I rechecked and it is okay). The TMP36 gives me back realistic data and there are no spikes. Also, returns to idle quite quickly.

const int ThermoPin = A0; //TMP36 value pin is on port A0
const float baselineTemp = 26.0; // room tempreture (it is not this hot but that's what TMP36 shows

void setup() {
  Serial.begin(9600); // open serial port
  for(int pinNumber = 2; pinNumber<5; pinNumber++){
    pinMode(pinNumber, OUTPUT);
    digitalWrite(pinNumber, LOW);
  }

}

void loop() {
  int SensorVal = analogRead(ThermoPin);
  Serial.print("Sensor value: ");
  Serial.print(SensorVal);
  
  float voltage = (SensorVal/1024.0) * 5.0;
  Serial.print (", Sensor voltage: ");
  Serial.print (voltage);

  Serial.print(", °C: ");
  float temp = (voltage - .5) * 100.0;
  
  Serial.println(temp);

Although, when I am adding the second part of the code (which lights up the LEDs) I experience that the SensorVal spikes and stays up until I press the reset button on the Arduino Uno. Here is the second part of the code:

if(temp < baselineTemp+2){
  digitalWrite(2, LOW); //red LED
  digitalWrite(3, LOW); //yellow LED
  digitalWrite(4, LOW); //Zöld LED
}

else if(temp >= baselineTemp+2 && temp < baselineTemp+4){
  digitalWrite(2, LOW); //red LED
  digitalWrite(3, LOW); //yellow LED
  digitalWrite(4, HIGH); //green LED
}

else if(temp >= baselineTemp+4 &&
  temp < baselineTemp+6){
  digitalWrite(2, LOW); //red LED
  digitalWrite(3, HIGH); //yellow LED
  digitalWrite(4, HIGH); //green LED
}

else if(temp >= baselineTemp+6){
  digitalWrite(2, HIGH); //red LED
  digitalWrite(3, HIGH); //yellow LED
  digitalWrite(4, HIGH); //green LED
}

delay(1);
  
}

This is what I see on the Serial Monitor (all I do is put my finger on the top of the sensor):

Sensor value: 157, Sensor voltage: 0.77, °C: 26.66
Sensor value: 158, Sensor voltage: 0.77, °C: 27.15
Sensor value: 159, Sensor voltage: 0.78, °C: 27.64
Sensor value: 159, Sensor voltage: 0.78, °C: 27.64
Sensor value: 159, Sensor voltage: 0.78, °C: 27.64
Sensor value: 159, Sensor voltage: 0.78, °C: 27.64
Sensor value: 159, Sensor voltage: 0.78, °C: 27.64
Sensor value: 159, Sensor voltage: 0.78, °C: 27.64
Sensor value: 159, Sensor voltage: 0.78, °C: 27.64
Sensor value: 159, Sensor voltage: 0.78, °C: 27.64
Sensor value: 159, Sensor voltage: 0.78, °C: 27.64
Sensor value: 159, Sensor voltage: 0.78, °C: 27.64
Sensor value: 159, Sensor voltage: 0.78, °C: 27.64
Sensor value: 159, Sensor voltage: 0.78, °C: 27.64
Sensor value: 160, Sensor voltage: 0.78, °C: 28.12
Sensor value: 201, Sensor voltage: 0.98, °C: 48.14
Sensor value: 219, Sensor voltage: 1.07, °C: 56.93
Sensor value: 223, Sensor voltage: 1.09, °C: 58.89
Sensor value: 222, Sensor voltage: 1.08, °C: 58.40
Sensor value: 222, Sensor voltage: 1.08, °C: 58.40
Sensor value: 222, Sensor voltage: 1.08, °C: 58.40
Sensor value: 222, Sensor voltage: 1.08, °C: 58.40
Sensor value: 222, Sensor voltage: 1.08, °C: 58.40
Sensor value: 221, Sensor voltage: 1.08, °C: 57.91

I don’t think it is a sensor problem, because without controlling the LEDs it works fine. I also rule out wiring for the same reason. So, what left is the code, but I guess that’s also alright. I attached the schematics.

Thanks a lot for helping me understand what is bad.

Regards,
Oderus

Post your code - all of it.

wildbill:
Post your code - all of it.

Thanks, and here you go:

const int ThermoPin = A0; //TMP36 value pin is on port A0
const float baselineTemp = 26.0; // room tempreture (it is not this hot but that's what TMP36 shows

void setup() {
  Serial.begin(9600); // open serial port
  for(int pinNumber = 2; pinNumber<5; pinNumber++){
    pinMode(pinNumber, OUTPUT);
    digitalWrite(pinNumber, LOW);
  }

}

void loop() {
  int SensorVal = analogRead(ThermoPin);
  Serial.print("Sensor value: ");
  Serial.print(SensorVal);
  
  float voltage = (SensorVal/1024.0) * 5.0;
  Serial.print (", Sensor voltage: ");
  Serial.print (voltage);

  Serial.print(", °C: ");
  float temp = (voltage - .5) * 100.0;
  
  Serial.println(temp);


if(temp < baselineTemp+2){
  digitalWrite(2, LOW); //red LED
  digitalWrite(3, LOW); //yellow LED
  digitalWrite(4, LOW); //Zöld LED
}

else if(temp >= baselineTemp+2 && temp < baselineTemp+4){
  digitalWrite(2, LOW); //red LED
  digitalWrite(3, LOW); //yellow LED
  digitalWrite(4, HIGH); //green LED
}

else if(temp >= baselineTemp+4 &&
  temp < baselineTemp+6){
  digitalWrite(2, LOW); //red LED
  digitalWrite(3, HIGH); //yellow LED
  digitalWrite(4, HIGH); //green LED
}

else if(temp >= baselineTemp+6){
  digitalWrite(2, HIGH); //red LED
  digitalWrite(3, HIGH); //yellow LED
  digitalWrite(4, HIGH); //green LED
}

delay(1);
  
}

Code looks ok. My only guess is that you have a power issue and that the LEDs are drawing enough current to cause a voltage drop. It seems unlikely though unless you're using super strong leds or have a weak power supply. Are you using the 220 ohm resistors as shown?

What happens if you leave the code the same but disconnect the LEDs?

Hey Wildbill,

Your hunch was correct I assume. When I removed the LEDs from the circuit, the TMP36 started to give back normal readings once again. The system is powered from my laptop via USB.

Currently I do not have a multimeter, but as soon as I get one I will measure the resistors and the power.

Thanks!

You must have an entirely separate ground wire between Arduino and LEDs from the ground wire between Arduino and TMP36. Any current from the LEDs must not flow on wires that go to the sensor.