Thermometer and fever lightTinkerPieCircuit tutorial 23 of 24 · Intermediate · 20 minutes
Read a temperature sensor, convert volts to degrees Celsius and light a warning LED.
Open this circuit in TinkerPieCircuit (free, runs in your browser)
What you will learn
- Reading an analog sensor with analogRead
- Converting a reading to volts and then °C
- Using the Serial monitor
Parts
Steps
Wire the sensor
V →
3V3, G →GND, OUT →P11.Convert the reading
analogReadgives 0 to 4095 for 0 to 3.3 V. So volts = reading × 3.3 ÷ 4095.Find the temperature
The sensor gives 0.5 V at 0 °C plus 0.01 V per degree, so °C = (volts − 0.5) × 100.
Warn when hot
If the temperature is over 40 °C, turn on the LED on
S1.Test
Press ▶ Start, open the Serial monitor and drag the sensor slider.
The code
function setup() {
pinMode(S1, OUTPUT);
}
function loop() {
const volts = analogRead(P11) * 3.3 / 4095;
const tempC = (volts - 0.5) * 100;
Serial.println("Temperature = " + tempC.toFixed(1) + " C");
if (tempC > 40) {
digitalWrite(S1, HIGH);
} else {
digitalWrite(S1, LOW);
}
delay(300);
}
Copy this into the Code tab. It is Arduino-style JavaScript. The real board uses the same pin names (S1, P10…) in TinkerPie Studio.
How it works
The sensor outputs a voltage that grows with temperature. The program turns that voltage back into degrees with a simple formula. This is how most analog sensors work.
← Motion alarm with a PIR sensor · All 24 circuit tutorials · Plant watering helper →