LC78 OFFROAD — Water + Air Monitor

Arduino UNO R4 WiFi SH1106 OLED DS18B20 + DHT22 active buzzer
Firmware: projects/water_monitor/firmware/water_monitor/water_monitor.ino. This is the interactive version of README.md — same content, click-through for newcomers.

1 What it does

  • Reads water temperature from a DS18B20 waterproof probe.
  • Reads air temperature + humidity from a DHT22.
  • Shows all three on a 1.3" SH1106 OLED, over a scrolling off-road truck animation.
  • Prints a status line to Serial every 2 s.
  • Sounds an active buzzer in three stages based on the water temperature.

2 Hardware (bill of materials)

#PartNotes
1Arduino UNO R4 WiFi5 V logic
2SH1106 OLED 1.3" 128×64, I2Caddress 0x3C
3DS18B20 waterproof probeOneWire; needs a 4.7 kΩ pull-up
4DHT22 (AM2302) breakouttemp + humidity; has its own pull-up
5Active buzzerfixed tone, on/off only
64.7 kΩ resistorDS18B20 data pull-up to 5 V
7Breadboard + jumper wires
Active buzzer (confirmed by test): it makes a tone from plain digitalWrite(HIGH). It has one fixed pitch — loudness cannot be changed in code. So "alarm" vs "warn" is steady vs pulsing, not louder vs softer.

3 Pin map

PartPin on partUNO R4 pin
OLEDSDAA4
OLEDSCLA5
OLEDVCC / GND5V / GND
DS18B20 (water)Red (VCC)5V
DS18B20 (water)Black (GND)GND
DS18B20 (water)Yellow (DATA)D2
DS18B20 (water)4.7 kΩ resistorbetween DATA and 5V
DHT22 (air)VCC / +5V
DHT22 (air)DATA / OUTD4
DHT22 (air)GND / −GND
Buzzer+D3
Buzzer−GND

4 Circuit diagram

Hover a wire to highlight it. [4.7k] is the DS18B20 pull-up. Every VCC is 5 V, every GND common.

UNO R4 WiFi A4 A5 D2 D3 D4 5V GND 5V GND OLED SH1106 SDA→A4 SCL→A5 DS18B20 water DATA→D2 4.7k Buzzer (active) + →D3 DHT22 air DATA→D4 common GND: OLED, DS18B20, DHT22, buzzer(−) 5V: OLED, DS18B20, DHT22, 4.7k pull-up
I2C (SDA/SCL) OneWire data DHT single-wire buzzer 5V GND

Signal summary

PinDirectionConnected to
D2inDS18B20 water probe (OneWire, 4.7k pull-up to 5V)
D3outactive buzzer (+)
D4inDHT22 air temp + humidity (single-wire)
A4I2COLED SDA (data)
A5I2COLED SCL (clock)
5VpowerOLED, DS18B20, DHT22, 4.7k pull-up
GNDpowerOLED, DS18B20, DHT22, buzzer (−)

Breadboard wiring view

The same circuit as it sits on a breadboard — components, jumper wires and the 4.7 kΩ pull-up in place.

Breadboard wiring diagram

4b How the temperature sensors work

From the analog physics inside the chip to the °C/°F your code prints. The full chain:

physical heat → analog voltage inside the chip → ADC → digital number
→ 1-wire protocol on the pin → Arduino library decodes → your °C/°F math
Both your sensors are digital on the wire. The DS18B20 and DHT22 each do the analog measurement and the analog-to-digital conversion inside their own silicon, then send a ready-made number over one data pin. "Analog viewpoint" here means what happens inside the chip — that is where the sensing physics lives. Section 5 (LM35 / thermistor) shows the classic case where the Arduino's own ADC does the work.

1 · The physics — heat becomes a voltage

MechanismUsed byWhat changes with temperature
Silicon bandgapDS18B20, DHT22, LM35, TMP36forward voltage of a diode/BJT junction
Thermistor (NTC)cheap probes, 3D printersresistance of a ceramic bead
Thermocouplekilns, ovenstiny voltage from two metals (Seebeck effect)

The DS18B20 uses the silicon bandgap effect: a forward-biased junction's voltage drops about −2 mV per °C. Running two junctions at different current densities and subtracting gives a voltage proportional to absolute temperature (the PTAT voltage). That PTAT voltage is the analog signal the chip measures.

V_junction
   ^
   |\
   | \____           higher temp → lower junction voltage
   |      \____       (about -2 mV per °C)
   |           \____
   +------------------> temperature

2 · Inside the DS18B20 — analog → digital, all on-chip

 [junction/PTAT]        analog voltage ∝ temperature
        |
 [analog front-end]     amplifies + references the PTAT voltage
        |
 [sigma-delta ADC]      converts the analog voltage to a digital count
        |
 [temperature register] signed 16-bit number, units of 1/16 °C
        |
 [1-Wire interface]     sends those bytes out on the single data pin

The ADC resolution is configurable — exactly what your code sets:

waterSensor.setResolution(10);   // line 159 of water_monitor.ino
ResolutionStep sizeConversion timeThis project
9-bit0.5 °C~94 ms
10-bit0.25 °C~188 ms← used here
11-bit0.125 °C~375 ms
12-bit0.0625 °C~750 ms

The chip stores temperature as a signed 16-bit integer in units of 1/16 °C. So raw 0x0191 = 401, and 401 ÷ 16 = 25.0625 °C. The library does that division, which is why you get a float back.

3 · The 1-Wire protocol — how the number crosses the pin

The DS18B20 uses Dallas 1-Wire: one data line (D2) carries both directions. The line idles HIGH, so it needs the 4.7 kΩ pull-up to 5 V — both ends talk by briefly pulling it LOW. No pull-up → the line floats → you see W:--.

A reading is a two-step dance, and the code reads first, then requests:

if (millis() - lastWater > 500) {
  float t = waterSensor.getTempCByIndex(0);  // read result of the LAST request
  ...
  waterSensor.requestTemperatures();         // kick off the NEXT conversion
  lastWater = millis();
}

This is deliberately non-blocking: the conversion started 500 ms ago is already finished, so the read is instant; then a fresh conversion starts for next time. The 500 ms interval comfortably exceeds the 188 ms conversion, so the value is always ready — the animation never freezes.

A disconnected probe returns DEVICE_DISCONNECTED_C (−127 °C):

if (t != DEVICE_DISCONNECTED_C) { waterOK = true; }
else waterOK = false;   // missing probe → W:-- , never false-triggers the buzzer

4 · Inside the DHT22 — same idea, different packaging

  • Humidity: a capacitor whose dielectric absorbs water vapour. More moisture → higher capacitance (analog), converted to %RH on-chip.
  • Temperature: a bandgap element, same principle as section 1.

One reading is 40 bits: 16 humidity, 16 temperature, 8 checksum. Temperature is a signed integer in units of 0.1 °C (raw 0x00E6 = 230 → 23.0 °C). Your code just asks:

float t = dht.readTemperature();   // line 202 — °C
float h = dht.readHumidity();      // line 203 — %RH

The DHT22 samples at only ~0.5 Hz, so it is read every 2 s; faster polling returns stale data or NaN, caught here:

if (!isnan(t) && !isnan(h)) { airOK = true; }
else airOK = false;

5 · The "true analog" case (for comparison): LM35

Not used here, but this is what "analog temperature sensing" usually means — the Arduino's own 10-bit ADC does the conversion. An LM35 outputs 10 mV per °C:

int raw = analogRead(A0);              // 0..1023 (10-bit ADC)
float volts = raw * (5.0 / 1023.0);    // ADC count → volts (5V reference)
float celsius = volts * 100.0;         // 10 mV/°C → ×100

Each ADC count ≈ 4.9 mV ≈ 0.49 °C of resolution. Moving the ADC inside the chip (like the DS18B20) avoids noise pickup, wire resistance and rail wobble corrupting a long analog wire.

6 · The °C → °F conversion

waterF = t * 9.0 / 5.0 + 32.0;   // °F = °C × 9/5 + 32
// 25 × 9 / 5 + 32 = 77.0 °F
Write 9.0 / 5.0, not 9 / 5. Integer division 9 / 5 equals 1 (remainder dropped), which breaks the formula. The .0 forces floating-point division (1.8). A classic C/C++ trap — the code gets it right.

Quick reference

DS18B20 (water)DHT22 (air)
Sensing elementsilicon bandgap junctionbandgap temp + capacitive humidity
Where the ADC livesinside the chipinside the chip
On-wire format1-Wire, signed, 1/16 °Csingle-wire, 40 bits, 0.1 °C
Pull-up?yes, 4.7 kΩbreakout has its own
Resolution here10-bit → 0.25 °C0.1 °C
Max read rate~5 Hz~0.5 Hz (every 2 s)
PinD2D4
Library callgetTempCByIndex(0)readTemperature() / readHumidity()
Error sentinelDEVICE_DISCONNECTED_C (−127)NaN

Full text: sensor-explainer.md

5 Buzzer logic (based on WATER temperature)

Click a temperature zone to see what the buzzer, screen and Serial do:

below 35 °C
Silent
35 – 40 °C
Beep
above 40 °C
Loud
Pick a zone above
—
Water tempBuzzerScreenSerial
below 35 °Csilent(none)OFF
35 to 40 °Cbeep on/off every 400 msflashing WARMBEEP
above 40 °CON continuouslyflashing W HOT!LOUD-CONT

Thresholds (top of the sketch)

#define WARN_LIMIT 35.0     // beep from here
#define ALARM_LIMIT 40.0    // loud continuous above here
The buzzer is driven with digitalWrite only — never tone(). Mixing tone() and digitalWrite on the same pin jams the pin (the tone() timer stays attached), which was an earlier bug. One method for all states fixes it.
A disconnected water probe reads as OFF and never false-triggers the alarm.

6 Display layout

  +--------------------------------------------+
  | W:24.5'                        FriedRust   |   <- water C, brand
  | A:22.0'                                    |   <- air C
  | H:48%           [ WARM ]                   |   <- humidity, warn box
  |         /\        __                       |
  |   ___/\/  \___   /LC78\___   /\/\          |   <- truck on scrolling ground
  |__/            \_/         \_/     \________|
  +--------------------------------------------+
  • W: water °C, A: air °C, H: humidity % — top-left.
  • FriedRust brand — top-right.
  • WARM / W HOT! box flashes only when the buzzer is active.
  • The truck hops when a boulder passes under it. Animation is non-blocking.

7 Serial output (9600 baud)

Water: 24.5C/76.1F   Air: 22.0C/71.6F  Hum:48%   Buzzer: OFF
Water: 37.2C/99.0F   Air: 22.1C/71.8F  Hum:47%   Buzzer: BEEP
Water: 92.3C/198.1F  Air: 22.1C/71.8F  Hum:47%   Buzzer: LOUD-CONT

Printed once every 2 s (paced by the DHT22, which can only read that often).

8 Read timing

SensorIntervalWhy
DS18B20 water0.5 sfast, non-blocking request/read
DHT22 air2 sDHT22 hardware limit (~0.5 Hz)
Display + buzzerevery loop (~40 ms)smooth animation, responsive beep

All reads are non-blocking (millis() timers), so the animation never stalls.

9 Libraries

Install via Tools → Manage Libraries:

  1. Adafruit SH110X (pulls in Adafruit GFX + Adafruit BusIO)
  2. Adafruit GFX Library
  3. OneWire (Paul Stoffregen)
  4. DallasTemperature (Miles Burton)
  5. DHT sensor library (Adafruit) — say yes to Adafruit Unified Sensor
All of these are already vendored under ../../libraries/.

9b Build and upload from the command line (arduino-cli)

Install the board core (one-time)

The UNO R4 WiFi uses the Renesas core. Install it once:

arduino-cli core update-index
arduino-cli core install arduino:renesas_uno

Check it is there: arduino-cli core list should show arduino:renesas_uno.

Find your board's port

Plug the board in over USB, then:

arduino-cli board list

Example output:

Port                            Protocol Type              Board Name           FQBN                             Core
/dev/cu.usbmodemF412FA9C12345   serial   Serial Port (USB) Arduino UNO R4 WiFi  arduino:renesas_uno:unor4wifi    arduino:renesas_uno

The Port column is what you pass to -p below. By platform:

OSPort looks likeTips
macOS/dev/cu.usbmodemXXXXlist with ls /dev/cu.usbmodem*
Linux/dev/ttyACM0 (or ttyUSB0)may need sudo usermod -aG dialout $USER, then re-login
WindowsCOM5also in Device Manager → Ports (COM & LPT)
If the board does not appear: try another USB cable (some are charge-only), press the board's reset button, and re-run arduino-cli board list.

Compile and upload

Run these from the repo root. --libraries ./libraries uses the repo's vendored libraries, so you do not have to install them by name.

# compile
arduino-cli compile \
  --fqbn arduino:renesas_uno:unor4wifi \
  --libraries ./libraries \
  projects/water_monitor/firmware/water_monitor

# find your board's port (Port column; e.g. /dev/cu.usbmodemXXXX)
arduino-cli board list

# upload (replace the port with the one from 'arduino-cli board list')
arduino-cli upload \
  --fqbn arduino:renesas_uno:unor4wifi \
  --libraries ./libraries \
  -p /dev/cu.usbmodemXXXX \
  projects/water_monitor/firmware/water_monitor

Watch the Serial output from the CLI (9600 baud, matches the sketch):

arduino-cli monitor -p /dev/cu.usbmodemXXXX -c baudrate=9600

10 Bring-up (step by step)

Click each step as you finish it — your progress shows in the sidebar and is saved in this browser.

  • Blink test — upload projects/_utilities/blink_led/blink_led.ino. The on-board LED path (D8) proves the toolchain and the board.
  • Find the OLED — upload projects/_utilities/i2c_address_finder/i2c_address_finder.ino, open Serial at 115200. It should report 0x3C. If it reports something else, change SCREEN_ADDRESS.
  • Wire the sensors per the pin map above. Double-check the DS18B20 4.7 kΩ pull-up.
  • Upload projects/water_monitor/firmware/water_monitor/water_monitor.ino. Open Serial at 9600.
  • Verify: the OLED shows the truck + readouts; Serial prints the status line.
  • Test the buzzer: warm the water probe past 35 °C (beeps), then past 40 °C (steady).

11 Troubleshooting

SymptomLikely causeFix
Blank OLEDwrong I2C address or SDA/SCL swappedrun the I2C scanner; A4=SDA, A5=SCL
SH1106 allocation failed on SerialOLED not found on I2Ccheck 0x3C, wiring, 5V/GND
Water shows W:--probe unplugged or missing pull-upadd 4.7 kΩ DATA→5V, check D2
Air/Hum shows --DHT22 wiring or too-fast readscheck D4, VCC, GND; keep 2 s interval
Buzzer never soundswrong pin or buzzer polaritybuzzer + to D3, − to GND
Buzzer stuck / weirdmixing tone() with digitalWriteuse digitalWrite only (active buzzer)
Values look wrong on first readsensors still warming upignore the first second or two

12 Tuning

  • Thresholds: edit WARN_LIMIT / ALARM_LIMIT.
  • Beep speed: change 400 in the beep block (ms per on/off toggle).
  • Animation speed: change the final delay(40) and scroll += 2 in loop().
  • DS18B20 precision/speed: waterSensor.setResolution(10) — 9..12 bits.