Learn & build · Electronics · Part II · Chapter 22
A microcontroller board: an ESP32 weather display
The dusk light made its decision with a comparator and one threshold. Anything cleverer, like showing the temperature, logging it, or deciding by the time of day, wants a small computer: chapter 13's microcontroller. This build puts one in charge: an ESP32 reads temperature, humidity and pressure from a sensor and draws them on a little screen, all three talking over two shared wires. The electronics is mostly wiring, so the decisions move elsewhere: which ready-made boards to buy, which pins to use, and whether the bus and the supply can keep up.
1. The goal, and boards instead of chips
The goal: a little screen shows the room's temperature, humidity and pressure, updated every few seconds, powered from USB. The blocks: a microcontroller, a sensor, a display. Here, for the first time, none of them is a bare chip. Each is a module, a small board someone else designed around a chip, with the parts that chip needs already on it:
- The ESP32 dev board carries the ESP32 module (the chip, its flash memory and an antenna under a metal can), a 3.3 V regulator fed from USB's 5 V, a USB-to-serial chip for programming, and two buttons.
- The BME280 breakout carries the tiny sensor (2.5 mm square, impossible to solder by hand) on pins you can plug in, often with pull-up resistors, and on some boards a regulator.
- The OLED module carries a 128×64 screen and its SSD1306 driver chip.
Chapter 19's question, whether one chip's high counts as a 1 at the next, is easy here: all three boards run at 3.3 V, so their highs and lows match (the ESP32's output high is at least 2.64 V, 80% of 3.3 V, its datasheet says). The trap is the breakout's supply pin. Some BME280 boards carry their own 3.3 V regulator and level shifters (small parts that translate signals between 3.3 V and 5 V) and accept 5 V; others are the bare sensor on a board, and its absolute maximum is 4.25 V. Read which kind you have before connecting VCC to anything but 3.3 V.
Modules save a lot of work, and they hide things: the question is always what's on the little board. The answer is the seller's page, or better, the module's own schematic. Here's this build's schematic, drawn with labels instead of long wires: every SDA label is one wire, every SCL label another, just like the supply labels in chapter 17.
The two modules both have pins called SDA and SCL. What connects them?
- Nothing but the labels: every pin labeled SDA is one wire, every SCL another, shared by all three boards
- Each module has its own pair of wires to the ESP32
- The pull-up resistors
2. Choosing the pins
A microcontroller has dozens of pins, and the code can usually put a function on almost any of them. But not every pin is free: some are wired to the module's flash memory, some decide how the chip boots, some can only listen. The ESP32's datasheet lists them all; this table is what it says, for the pins a dev board brings out. Pick a job, then a pin for it.
🎯 Challenge: place all five jobs on pins without a single red or orange warning.
What the colors mean, from the datasheet
- Flash (GPIO6–11): wired to the module's own flash memory, where the program lives. Using them crashes the chip.
- Strapping pins (GPIO0, 2, 5, 12, 15): read once at reset to decide how to boot, each with a weak internal pull-up or pull-down setting its default. GPIO0 held low means "wait for a new program" (it's the BOOT button); GPIO12 (MTDI) held high selects 1.8 V for the flash, and on a module with 3.3 V flash the chip then fails to boot. You can use them after boot, if whatever you connect never overrides the default at reset.
- GPIO1 and GPIO3: UART0's TX and RX, the dev board's USB serial link: programming and printing. Leave them alone.
- Input only (GPIO34–39): no output driver and no internal pull-ups. Fine for a sensor's analog voltage; a button there needs its own pull-up resistor.
- ADC1 (GPIO32–39) and ADC2: both are analog inputs, but Espressif's documentation notes that ADC2 is also used by Wi-Fi; an ADC1 pin keeps an analog sensor out of that question.
3. Two devices, one bus, two addresses
Each I²C device answers to a 7-bit address (chapter 13). Two devices with the same address on one bus both answer at once, and nothing works. The datasheets fix these: the BME280 is 0x76 with its SDO pin to ground, 0x77 with SDO to 3.3 V; the SSD1306 is 0x3C, or 0x3D on boards with an address jumper.
You want two BME280s, one inside and one by the window, on the same two wires. How?
- Just connect them both: the ESP32 reads them in turn
- Tie one's SDO to ground (0x76) and the other's to 3.3 V (0x77)
- You can't: it needs a second bus
4. Pull-ups, and how fast the bus can be
I²C lines are open drain (chapter 13, and chapter 20's comparator): devices only pull down; a resistor pulls up. Every rising edge is that resistor charging the wire's capacitance, chapter 2's RC curve. The I²C specification sets the limits: an edge must rise from 30% to 70% of the supply within 1000 ns in standard mode (100 kHz) and 300 ns in fast mode (400 kHz), and a device needs only sink 3 mA while holding the line at 0.4 V. Modules often carry their own pull-ups, and all of them end up in parallel.
🎯 Challenge: with both modules' own pull-ups as they are and 200 pF of wire, get the bus fast-mode ready without asking any device to sink more than 3 mA.
So the schematic's R1 and R2 are a decision, not a rule: check what the modules already carry (two small resistors near the SDA and SCL pins, marked 103 for 10 kΩ or 472 for 4.7 kΩ), add yours only if the total is too weak, and never let the total drop below about 1 kΩ on 3.3 V.
Why is the rise time 0.85 RC?
- A line pulled up through R charges like chapter 2's capacitor: \(V(t) = V_s\,(1 - e^{-t/RC})\).
- It reaches 30% of the supply at \(t_{30} = RC\ln\frac{1}{0.7}\) and 70% at \(t_{70} = RC\ln\frac{1}{0.3}\).
- The rise time the I²C specification measures is the difference: \(RC\,(\ln\frac{1}{0.3} - \ln\frac{1}{0.7}) = RC\ln\frac{7}{3} \approx 0.847\,RC\).
- With 4.7 kΩ and 100 pF: \(0.847 \times 4.7\text{k} \times 100\text{p} \approx 400\) ns: fine at 100 kHz, too slow for 400 kHz.
5. Power: peaks, not averages
The sensor draws 3.6 µA reading once a second (its datasheet); the display more, depending on how many pixels are lit, but still little next to the ESP32. The ESP32 is different: its datasheet gives 240 mA while transmitting Wi-Fi, in short bursts, and recommends a 3.3 V supply that can deliver 500 mA or more. That rules out chapter 18's MCP1700 (250 mA) for the ESP32 itself: let the dev board's own regulator, fed by USB, power it, and use its 3V3 pin for the two small modules.
Your board works on USB, then resets every time Wi-Fi connects when you run it from a long, thin cable and a weak supply. Why?
- A bug in the Wi-Fi code
- The display draws too much
- A brown-out: the 240 mA burst drops volts in the cable's resistance and the supply sags below what the chip needs, so it resets
6. The breadboard
Wide dev boards cover most of a breadboard's holes, so here the ESP32 sits off to the side on four jumper wires. The two modules plug their headers into row a, one strip per pin, and lean over the edge. Their pin orders differ (the BME280 board starts with VCC, the OLED with GND): wire by the printed labels, never by position.
🎯 Challenge: click the SDA strip (column 20, bottom half) and check that all three boards and one pull-up meet there.
7. The program, in words
The electronics are done; the rest is code, which this course leaves to the libraries' own examples. In words, the program the ESP32 runs is short:
- Start the I²C bus on GPIO21 (SDA) and GPIO22 (SCL), the Arduino ESP32 core's defaults.
- Start the sensor at address 0x76 and the display at 0x3C, through their libraries (Adafruit's BME280 and SSD1306 libraries are the common ones; each comes with a working example).
- Forever: read temperature, humidity and pressure; clear the screen; draw three lines of text; wait two seconds.
Before any of that, run the I²C scanner, the first program for every new bus: it tries every address and prints the ones that answer. On this build it must print exactly 0x3C and 0x76. Fewer means a wiring or power problem; a different number means a different address than you assumed. The Arduino Wire library's examples include one.
8. The shopping list
| Ref | Part | Why this one |
|---|---|---|
| U1 | ESP32 dev board (ESP32-WROOM-32 module) | Wi-Fi, two cores, USB programming, a 3.3 V regulator for itself. |
| U2 | BME280 I²C breakout | Temperature, humidity and pressure. Check: 3.3 V or 5 V-tolerant board? which pull-ups? |
| U3 | 0.96″ 128×64 SSD1306 OLED, I²C (4-pin) | Address 0x3C; 3.3 V supply. |
| R1, R2 | 4.7 kΩ ¼ W (only if needed) | Pull-ups, after counting the modules' own (section 4). |
| — | 100–470 µF electrolytic, 10 V+ | Bulk capacitor on 3V3 against brown-outs. |
| — | A good USB cable and a 5 V, 1 A+ supply | Wi-Fi bursts need the current. |
9. Power up and check
- 1. Before the modules: USB in, the bottom + rail 3.3 V from the board's 3V3 pin.
- 2. With the modules, no program yet: SDA and SCL idle at 3.3 V, held up by the pull-ups. A line at 0 V is shorted to ground or a module is in backwards; a line floating near nothing means no pull-up at all.
- 3. The scanner: exactly 0x3C and 0x76.
- 4. The real program: the room's temperature on the screen, within a degree or two of a thermometer (the sensor warms a little from its neighbors on a breadboard).
10. What's wrong?
The scanner finds nothing at all. Both modules' power LEDs are on.
- The addresses are wrong in the program
- SDA and SCL are swapped somewhere, or the bus is on different pins than the program uses
- The modules aren't powered
You moved the button to GPIO12 because it was handy. Now the board won't start when the button is held at power-up, and sometimes not at all.
- GPIO12 is a strapping pin: held high at reset, it tells the chip its flash runs on 1.8 V
- It's the USB serial pin
- GPIO12 is input only
11. Make it yours
- Log it: the ESP32 has Wi-Fi: post each reading to a web page or a spreadsheet.
- Battery power: chapter 10's battery budget, with the ESP32's 10 µA deep sleep from its datasheet: wake every ten minutes, read, send, sleep. Which drains the battery more, the sleeping or the sending?
- An outdoor sensor: a second BME280 at 0x77 (section 3), on long wires: then section 4's capacitance gets real, and slower bus speeds or stronger pull-ups follow.
Next: design your own, the last chapter: a design review where you find the mistakes, and a whole project, a thermostat fan, done by you with the steps of Part II.
Previous: A speaker amplifier · All chapters: Learn & build.
Last updated