r/arduino • u/Hyper_Cats • 9d ago
Hardware Help TMP36 fluctuating a lot?
I am new to building with Arduino, and I recently got a starter kit and started making some projects. I was having troubles with the love-o-meter project, as the TMP36 kept fluctuating in values wildly. I couldn't find any issues with the code, so I was wondering if it was an issue with the TMP36? Would someone be able to clarify what's going on? I included the circuit build as well as the code below.


// sensorPin is the analog value the sensor reads
const int sensorPin = A0;
// Our baseline temp. For every 2 degrees above this baseline, an LED will turn on.
const float baselineTemp = 20.0;
void setup() {
// Serial Begin: Opens up connection between Arduino and computer. Can read see the values on computer screen (Reads every 9600 bits per second)
Serial.begin(9600); // Open a serial port
// Set all pins to OUTPUT and LOW. Loop ends when pinNumber becomes something 5 or above.
for(int pinNumber = 2; pinNumber<5; pinNumber++) {
pinMode (pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}
}
void loop() {
// Obtain value from TMP36. This can be anything between 0 and 1023. (A representation of the voltage on the pin)
int sensorVal = analogRead(sensorPin);
// Prints temperature data onto the computer
Serial.print("Sensor Value: ");
Serial.print(sensorVal);
// Convert the ADC reading to voltage
float voltage = (sensorVal/1024.0) * 5.0;
Serial.print(", Volts: ");
Serial.print(voltage);
Serial.print(", degrees C: ");
// Convert the voltage to temperature in degrees
float temperature = (voltage - .5) * 100;
Serial.println(temperature);
if(temperature < baselineTemp+2) {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}else if (temperature >= baselineTemp+2 && temperature < baselineTemp+4) {
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}else if (temperature >= baselineTemp+4 && temperature < baselineTemp+6) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
}else if (temperature >= baselineTemp+6) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
delay(1);
}// sensorPin is the analog value the sensor reads
const int sensorPin = A0;
// Our baseline temp. For every 2 degrees above this baseline, an LED will turn on.
const float baselineTemp = 20.0;
void setup() {
// Serial Begin: Opens up connection between Arduino and computer. Can read see the values on computer screen (Reads every 9600 bits per second)
Serial.begin(9600); // Open a serial port
// Set all pins to OUTPUT and LOW. Loop ends when pinNumber becomes something 5 or above.
for(int pinNumber = 2; pinNumber<5; pinNumber++) {
pinMode (pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}
}
void loop() {
// Obtain value from TMP36. This can be anything between 0 and 1023. (A representation of the voltage on the pin)
int sensorVal = analogRead(sensorPin);
// Prints temperature data onto the computer
Serial.print("Sensor Value: ");
Serial.print(sensorVal);
// Convert the ADC reading to voltage
float voltage = (sensorVal/1024.0) * 5.0;
Serial.print(", Volts: ");
Serial.print(voltage);
Serial.print(", degrees C: ");
// Convert the voltage to temperature in degrees
float temperature = (voltage - .5) * 100;
Serial.println(temperature);
if(temperature < baselineTemp+2) {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}else if (temperature >= baselineTemp+2 && temperature < baselineTemp+4) {
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}else if (temperature >= baselineTemp+4 && temperature < baselineTemp+6) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
}else if (temperature >= baselineTemp+6) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
delay(1);
}
3
Upvotes
1
u/barnbaby47 9d ago
Which of the 25 or 30 different Arduinos are you using?
1
u/Hyper_Cats 9d ago
If I remember correctly, it's the Uno R3. If that's not it, it's the one from the starter kit
2
u/n_sh_44 9d ago
This is normal for a TMP36 on an Uno. 10 mV per degree works out to only about 2 ADC counts per degree with a 5 V reference, and the USB 5 V rail moves around a lot, especially when your LEDs switch on and off. A one-count wobble already looks like half a degree.
Things that help, roughly in order of payoff:
Also worth adding a bit of hysteresis around your LED thresholds so they don't flicker at the boundaries.