Hello, I am new to Arduino but I am looking forward to learning about it and using it for some practical applications. Currently, I am using a Lilypad Simple Board with the MCP9700 temperature sensor, both of which have been disconnected from the protosnap board. My goal is to measure the ambient temperature and read it off of the COM port. However, I am getting erratic readings on screen.
The board is connected to the computer via USB, with the sensor connected to the board in what I understand to be the correct way: hot to hot, ground to ground, and data to pin A2 (my own choice).
The code I am using I found online:
//TMP36 Pin Variables
int sensorPin = A2;
#define BANDGAPREF 14 // special indicator that we want to measure the bandgap
/*
* setup() - this function runs once when you turn your Arduino on
* We initialize the serial connection with the computer
*/
void setup()
{
Serial.begin(9600); //Start the serial connection with the computer
//to view the result open the serial monitor
delay(500);
}
void loop() // run over and over again
{
// get voltage reading from the secret internal 1.05V reference
int refReading = analogRead(BANDGAPREF);
Serial.println(refReading);
// now calculate our power supply voltage from the known 1.05 volt reading
float supplyvoltage = (1.05 * 1024) / refReading;
Serial.print(supplyvoltage); Serial.println("V power supply");
//getting the voltage reading from the temperature sensor
int reading = analogRead(sensorPin);
// converting that reading to voltage
float voltage = reading * supplyvoltage / 1024;
// print out the voltage
Serial.print(voltage); Serial.println(" volts");
// now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset
//to degrees ((volatge - 500mV) times 100)
Serial.print(temperatureC); Serial.println(" degress C");
// now convert to Fahrenheight
float temperatureF = (temperatureC * 9 / 5) + 32;
Serial.print(temperatureF); Serial.println(" degress F");
delay(2000); //waiting a second
}
However, my output is unrealistic:
403
2.67V power supply
0.15 volts
-34.63 degress C
-30.33 degress F
159
6.76V power supply
0.40 volts
-9.72 degress C
14.51 degress F
150
7.17V power supply
0.72 volts
22.10 degress C
71.78 degress F
137
7.85V power supply
0.82 volts
32.01 degress C
89.61 degress F
136
7.91V power supply
0.66 volts
15.62 degress C
60.12 degress F
Troubleshooting, the code was modified to output the analog read of Sensor Pin A2. The read out was erratic, increasing and decreasing regardless of when heat was applied to the sensor. The temperature sensor was then replaced with the included light sensor. There, the analog readout would increase with increasing light and vice-versa. This leads me to believe that the problem may lie with the hardware (the temperature sensor). On the other hand, I know that the sketch was meant for a different temperature sensor, but I do not see why it would not work in this case. Any insight is appreciated.
I agree, there really isn't any reason to include it. I modified the code. The readings are now consistent but still incorrect (negative values). The voltage output from the sensor is now decreasing with increasing temperature. Is that characteristic of the sensor?
Please take a picture of your setup and post it. Take care that every wire is visible on the picture. Your output doesn't look like the readings of a thermal sensor, so let's check the wiring.
I soldered the wires but no luck. I ended up buying a new temp sensor and everything is working properly; I'm getting room-temperature readings. I decided to go back to using the bandgap reference because I can't assume 5000mV is supplied by the USB. It also means that I don't have to worry about floating vs integer arithmetic since the supply voltage reading is a floating point number. Overall, this was a good learning experience. Thanks for your help.