Go Down

Topic: Project 3 - Love-o-Meter - TMP36 Sensor (Read 1 time) previous topic - next topic

Fuzionist

Dec 11, 2016, 09:01 pm Last Edit: Dec 11, 2016, 10:22 pm by Fuzionist
This is my first time posting on this forum.  I appreciate everyone taking the time to reply.

I managed to execute this project successfully, but along the way I had to do some troubleshooting, and I wanted to share my experience and hopefully gain some educational insight into why certain actions I took produced certain results. This could also help others troubleshoot.

To note, I am using the Genuino Starter Kit, and only the components that came in the kit (wires, LEDs, USB cable, etc.).  For consistency, I am using the code example from the Arduino software (File -> Example -> 10.StarterKit_BasicKit -> p03_LoveOMeter).




1. Different USB Ports Result in Different Temperature Readings

I use a desktop computer.  I have an Anker USB 3.0 Hub, two USB 3.0 ports on the front of my case (that connect to the motherboard by cables), and then four USB 3.0 ports on the rear of my case (built into my motherboard).  I get different readings depending on which one of these USB ports I plug the Genuino into (ambient temperature readings:)

USB Hub - Reports about 11 degC
Front Port - Reports about 18 degC
Rear Port - Reports about 21 degC

I think the 21 degC is the most accurate.  

Thoughts: My understanding is that the output from the TMP36 sensor is a value of 0-1023 which is not dependent on the voltage input.  So, even if the voltage from the USB ports was inconsistent for some reason (which I don't know to be the case) then the TMP36 sensor should not care. Am I wrong?




2. Moving the USB Cable Changes the Reported Value

Plugged into the Rear Port, the reported temperature reading changes depending on the position/movement of the USB cable.

Cable Laying Across Top of Desktop Case - Reports about 18 degC
Manually Holding Cable Away from Desktop Case - Reports 23 degC

Thoughts: Is the Genuino very sensitive to electromagnetic interference?  If yes, would you think that there would be a meaningful difference in EM interference between these two  cases?




3. Changing Number of LEDs Changes the Reported Value

With the three LEDs wired to digital ports 2, 3, and 4 exactly as directed in the exercise , I get the expected temperature readings (18-22 degC, per above).  However, changing the number of LEDs in the code, and/or in the circuit, makes the readings go haywire.  

a) Keep LEDs wired but remove code
If I leave the 3xLEDs wired to the Genuino, but comment/delete all mention of the LEDs from the code (e.g. comment out all pinMode and digitalWrite lines), then - Reports about 5 degC

Code: [Select]
/*
  Arduino Starter Kit example
 Project 3  - Love-O-Meter

 This sketch is written to accompany Project 3 in the
 Arduino Starter Kit

 Parts required:
 1 TMP36 temperature sensor
 3 red LEDs
 3 220 ohm resistors

 Created 13 September 2012
 by Scott Fitzgerald

 http://www.arduino.cc/starterKit

 This example code is part of the public domain
 */

// named constant for the pin the sensor is connected to
const int sensorPin = A0;
// room temperature in Celcius
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 + 2) {
//    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(1);
}



b) Keep 2xLEDs wired and edit code
If I keep 2 of the LEDs wired, and edit the code to refer to only these two LEDs, then then - Reports about 13 degC
Code: [Select]
/*
  Arduino Starter Kit example
 Project 3  - Love-O-Meter

 This sketch is written to accompany Project 3 in the
 Arduino Starter Kit

 Parts required:
 1 TMP36 temperature sensor
 3 red LEDs
 3 220 ohm resistors

 Created 13 September 2012
 by Scott Fitzgerald

 http://www.arduino.cc/starterKit

 This example code is part of the public domain
 */

// named constant for the pin the sensor is connected to
const int sensorPin = A0;
// room temperature in Celcius
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 < 4; 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 + 2) {
    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(1);
}

 

c) Keep 1xLEDs wired and edit code
If I keep 1 of the LEDs wired, and edit the code to refer to only this one LED, then then - Reports about 17 degC

I'll stop posting the code.  You get the idea.

d) Remove all LEDs and edit code
If I remove all of the LEDs, and edit the code to remove all reference of the LEDs (same code as posted under (a) above), then - Reports -50C.

This case (d) I am most interested in.  All that is left on the circuit is the temperature sensor, and its connections to power, ground, and A0.  And it reports -50C (sensor value 0).  My goal is to understand how to use the temperature sensor, such as for future projects.  Is it required to have a certain amount of other loads connected to the Genuino - like 3 LEDs in this example?

Help?

Thanks
~Nick.

Go Up