Measure temperature with a DS18B20
Follow the student handout to build, test and document this stage of the water-quality system.
Prepared lesson material. Start this task when your teacher introduces it; it is not currently assigned homework.
For this lesson
Read the handout text online
Lesson 6: Digital Temperature Sensing with the DS18B20
Wiring, libraries, code, and response test
Grade 9 MYP Design | From USB to Field System
| Name: __________________ | Class / team: __________________ | Date: __________________ |
| Today’s goals • Wire the DS18B20 to GPIO4. • Use OneWire and DallasTemperature. • Explain the pull-up resistor. |
Key vocabulary
| Term | Working definition |
| digital sensor | |
| OneWire | |
| data line | |
| pull-up resistor | |
| defined state | |
| library |
1. Wiring netlist
| ESP32.3V3 -> DS18B20.VCCESP32.GND -> DS18B20.GNDDS18B20.DATA -> ESP32.GPIO46.8kΩ resistor between DS18B20.DATA and ESP32.3V3 |
| Check wire colors Probe wire colors are not universal. Confirm the supplied sensor documentation or teacher-labelled cable before applying power. |
2. Libraries
☐ Install OneWire by Paul Stoffregen.
☐ Install DallasTemperature by Miles Burton.
☐ Confirm both include statements compile.
3. Test code
| #include <OneWire.h>#include <DallasTemperature.h>#define DS18B20_PIN 4OneWire oneWire(DS18B20_PIN);DallasTemperature sensors(&oneWire);void setup() { Serial.begin(115200); delay(1500); sensors.begin(); Serial.print("Sensors found: "); Serial.println(sensors.getDeviceCount());}void loop() { sensors.requestTemperatures(); float tempC = sensors.getTempCByIndex(0); if (tempC == DEVICE_DISCONNECTED_C) { Serial.println("ERROR: sensor disconnected"); } else { Serial.print("Temperature_C,"); Serial.println(tempC, 2); } delay(2000);} |
4. Response test
| Condition | Time | Temperature (°C) | Expected direction | Notes |
| Room-temperature water | ||||
| Cooler water | ||||
| Warmer water | ||||
| Return to room-temperature |
5. Explain
Why does the DATA line need a resistor to 3.3V?
________________________________________________________________________________________________________________
________________________________________________________________________________________________________________
________________________________________________________________________________________________________________
________________________________________________________________________________________________________________
What would an error value tell you to check first?