Lesson 19: Thermometer
此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
How warm is it?
- A TMP36 is a small temperature sensor with three legs. Its middle leg gives a voltage that grows with the temperature.
- The Arduino reads that voltage on A0. In the simulator the knob plays the sensor: turn it up and the room gets warmer.
- First you turn volts into °C. Then six LEDs show the temperature as a bar, like the red line in a glass thermometer.
Words for this lesson
| Word | Meaning |
|---|---|
| temperature | how hot or cold something is, in °C |
| sensor | a part that measures something and gives a voltage |
| float | a number with a decimal point, like 24.78 |
| bar | a row of lights; more lights = more |
| constrain | keep a number between a lowest and a highest value |
English
From a reading to °C
| Step | Formula | At 0.75 V |
|---|---|---|
| reading → volts | volts = reading × 5.0 ÷ 1023 | reading 153 → 0.748 V |
| volts → °C | temp = (volts − 0.5) × 100 | (0.748 − 0.5) × 100 = 24.8 °C |
- The sensor gives 0.5 V at 0 °C and 0.01 V more for each degree: 0.6 V is 10 °C, 0.75 V is 25 °C.
- The Arduino reads whole steps of about 0.005 V, so you may see 24.78 instead of 25.00.
English
From °C to LEDs
| Temperature | (temp − 10) ÷ 5 | LEDs on |
|---|---|---|
| 5 °C | −1 | 0 (constrain) |
| 17 °C | 1.4 | 1 |
| 32 °C | 4.4 | 4 |
| 60 °C | 10 | 6 (constrain) |
- An
intkeeps only the whole part: 4.4 becomes 4. constrain(n, 0, 6)stops the bar at 0 and at 6 LEDs.
English
Decimal numbers
void setup() {
Serial.begin(9600);
int whole = 7 / 2;
float exact = 7.0 / 2;
Serial.println(whole);
Serial.println(exact);
}
void loop() {
}
7 / 2with whole numbers gives 3. With7.0it gives 3.50: one number with a decimal point is enough.
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
A TMP36 temperature sensor on A0 gives 0.5 V at 0 °C and 0.01 V more for every degree. The sketch already turns the reading into volts. Work out the temperature with temp = (volts - 0.5) * 100 and print it after Temp: . Turn the knob to warm or cool the sensor.
点击“运行”查看此处输出。
难度 4/5 · 今日首次完成可获 280 金币及少量额外奖励
Six LEDs on pins 2 to 7 make a bar: one more LED for every 5 °C above 10 °C. Work out n = (temp - 10) / 5 as a whole number, keep it from 0 to 6 with constrain, and show it with show(n).
点击“运行”查看此处输出。