1 minute read
How to Measure Temperature
204 Hacking Electronics
The “loop” function simply takes a reading and checks to see if it has exceeded the threshold. If it has, it turns the LED on.
void loop() { int reading = analogRead(sensePin); if (reading > normalReading + threshold) { digitalWrite(ledPin, HIGH); } }
To calibrate the sensor, 100 readings are made with a onemillisecond delay between each reading, and the average is returned. A variable of type “long” is used to hold the total, as this number may be too big to fit in the usual “int” type.
int calibrate() { int n = 100; long total = 0; for (int i = 0; i < n; i++) { total = total + analogRead(sensePin); delay(1); } return total / n; }
How to Measure Temperature
A number of different sensor ICs are designed for measuring temperature. Perhaps the simplest to use is the TMP36 (Figure 8-12).
You can experiment with the sensor, just printing the temperature to the Serial Monitor, or you can combine the sensor with the relay module we made in Chapter 6.
Figure 8-12 The TMP36