Skill 02

Electronics

Electronics is how electricity moves through a circuit: voltage, current, the components that shape it, and the microcontrollers that control it.

01 Why now
The improvised explosive device is the signature weapon of the last half-century of irregular war, and it has always been an electronics problem more than an explosives one. The charge is the simple part. The electronics that decide when it fires, a switch that closes, a trigger that completes a circuit, are the whole contest. A few dollars of those parts have forced the most expensive militaries on earth into armored trucks, signal jammers, and convoy drills. The scarce thing was never the parts. It is the understanding, and the understanding is electronics.
02 Case files
The Provisional IRA proved it first. Through the 1970s and 80s their bomb-makers and the British Army ran an arms race over the trigger, from command wire to timers to radio control to a way around every jammer the Army fielded. Iraq and Afghanistan industrialized it: for a decade the IED was the leading killer of coalition troops, and the guides written to fight it sorted the devices not by their explosive but by their electronics. Ukraine made the same idea small and cheap enough that anyone can field it, the trigger now riding a hobby drone instead of buried in a road. The platform keeps changing; the lesson does not. This module takes the three things at the heart of it, a switch, a sensor, a timer, and teaches what they are and how they work, wired into a node that reads the world. The fundamentals do not care which way they are pointed. This module points them at awareness.
03 Overview
You start with a breadboard and a handful of parts, salvaged or bought, and finish with an ESP32 node that senses four things and acts on what it finds. First the basics, one rung at a time: a switch completes a circuit, a sensor is an automatic switch, a timer decides for how long, and the microcontroller reads them all and decides what to do. Then the build, in order: power the board, read a button and light an LED, drive a real load, add a timer, then give the loop four senses (motion, magnetic, temperature, and light), and put it on the network you built in Module 01.
On this page
01

What you are building

You build one device: an ESP32 sensor node on a breadboard. It reads four inputs, decides what to do using a timer or a threshold, drives an output, and reports what it sees to the network you built in Module 01.

Every part of the build follows the same loop: input, decide, output. A switch or a sensor is the input. The microcontroller decides. A light, a relay, or a message on the network is the output. You learn each piece on its own, then wire them into the loop.

The kit is small, and most of it is cheap or salvaged: an ESP32 board, a breadboard and jumper wires, four sensors (motion, magnetic, temperature, light), a few output parts (an LED, a transistor, a relay), and a resistor assortment. Each unit names the exact part as you reach it.

Check on learning
What loop does every part of this build follow?
Where does the finished node send what it senses?
02

Electricity

Three quantities describe everything in the circuit. Voltage is the push, measured in volts. Current is the flow, measured in amps. Resistance is how much the path holds the flow back, measured in ohms. The water analogy holds: voltage is pressure, current is the flow rate, resistance is a narrow pipe.

One formula ties them together, Ohm's law: V = I × R. Rearranged, current is I = V / R and resistance is R = V / I. That is most of the math you need.

A worked example you will use in the build: you want about 20 milliamps through an LED on the 3.3 volt supply. The LED itself drops roughly 2 volts, which leaves 1.3 volts across the resistor. R = V / I = 1.3 / 0.02 = 65 ohms, so you reach for the nearest common value, 68 or 100 ohms.

The other formula is power, P = V × I, in watts. It matters because every part carries a power rating, and a part run past its rating heats up and fails. A quarter-watt resistor cannot carry whole watts.

Interactive Falstad CircuitJS, embedded below: change the voltage and the resistance, and watch the current move with Ohm's law.
Check on learning
You hold the voltage the same and halve the resistance. The current...
Why put a resistor in series with an LED?
03

The circuit, and how it is drawn

A circuit is a complete loop: current leaves a pin or the supply, runs through the parts, and returns to ground. Break the loop anywhere and the flow stops. You will meet that loop drawn three ways in this module, and you read all three. First, the symbols they use.

The symbols you will see
supplygroundresistorLEDswitch +

Here is one real circuit, a GPIO driving an LED through a resistor to ground, drawn all three ways. They say exactly the same thing.

One circuit, three ways. The resistor and the LED are in series, one after the other, so the same current runs through both and the resistor holds it down.
Schematic (symbols) GPIO5330 ΩLEDGND
On the breadboard + 123456789 AEFJ GPIO5 +
Connection table
FromTo
GPIO5resistor, end A
resistor, end BLED, long leg +
LED, short leg −GND
When they disagree, trust the table A schematic and a breadboard picture can be drawn loosely, and wire color is only a habit. The connection table is the exact one: it names which pin reaches which, and nothing more. Follow it.
Check on learning
What has to be true for any circuit to work?
A breadboard diagram and the connection table disagree about a wire. Which do you follow?
04

The switch

A switch is the simplest thing in electronics: it completes a circuit or breaks it. Closed, current flows; open, it stops. A push button is a switch you hold down. A reed switch is one a magnet closes. A sensor, as the next unit shows, is a switch the world closes for you.

The ESP32 reads a switch on a GPIO pin (general-purpose input/output). The pin reads HIGH (near 3.3 volts) or LOW (near 0). The trap is a pin connected to nothing: it floats and reads random noise. You fix that with a pull resistor. A pull-down ties the pin to ground so it rests LOW until the switch pulls it HIGH; a pull-up does the reverse. The ESP32 has built-in pull-ups you turn on in code with INPUT_PULLUP.

One more real-world catch: a mechanical switch does not close cleanly. The contacts bounce for a few milliseconds, so a single press can read as several. You debounce it, either in code by ignoring further changes for a short window, or in hardware with a small capacitor.

A floating pin lies An input pin wired to nothing reads noise, not a steady zero. Every switch and every digital sensor needs a defined resting state, a pull-up or a pull-down, or the readings are garbage.
Check on learning
You wire a button to a GPIO with no pull resistor. What does the pin read?
What does debouncing a switch fix?
05

Reading the world

A sensor is an automatic switch. Some are exactly that: a PIR closes when it sees motion, a reed switch closes near a magnet. The ESP32 reads those as digital, HIGH or LOW, the same way it reads a button.

Others do not snap on and off; they slide. A photoresistor's resistance falls as the light rises; a thermistor's changes with heat. These are analog: a voltage somewhere between 0 and 3.3 volts. The ESP32 reads them with its ADC (analog-to-digital converter), which turns that voltage into a number from 0 to 4095.

Two limits decide how you wire every sensor, and getting them wrong is the most common beginner failure:

3.3 volts, and the right ADC The ESP32 runs at 3.3 volts and no pin tolerates more than 3.3 volts; a 5 volt sensor output can damage it, so a 5 volt part needs a level shifter. And read analog only on ADC1 (GPIO 1 to 10); the other converter, ADC2, stops working once WiFi is on, and the node runs on WiFi.
Check on learning
A photoresistor's resistance changes smoothly with light. How does the ESP32 read it?
Your node uses WiFi. Which pins do you read analog sensors on?
06

Identify your hardware

This is the whole kit. Most of it is cheap or salvaged, and the few parts where the exact one matters are marked. The board is the one piece to buy deliberately: this build assumes an ESP32-S3-DevKitC-1 carrying the N16R8 module, and the pin numbers and the unusable pins below are specific to it.

Hardware to get
2N2222 NPN transistor switches a load a pin cannot driveAny brand
Breadboard and jumper wires solderless prototypingAny brand
DS18B20, TO-92 digital temperature sensorSpecific
ESP32-S3-DevKitC-1, N16R8 the microcontroller boardSpecific
HC-SR501 PIR motion sensorSpecific
LED, 5mm the simplest outputAny brand
Photoresistor (LDR) light sensorAny brand
Reed switch magnetic sensorAny brand
Relay module, 5V single-channel switches a heavier load than a pin canAny brand
Resistor assortment, 1/4W current limiting, pull-ups, dividersAny brand
Schottky diode, 1N5817 flyback protection for inductive loadsAny brand
Tactile push button a switch you pressAny brand
Software, all free and open source
Arduino IDE write and flash the firmwareFree
ESP32 board support adds the ESP32-S3 to the Arduino IDEFree
OneWire and DallasTemperature libraries that read the DS18B20Free

The breadboard, the jumper wires, and the resistor assortment carry across every later unit; each sensor unit names its own part as you reach it. Before wiring anything, learn the board: click each pin to see its job, and the few to leave alone.

Check on learning
Why learn the pinout before you wire anything?
What is true of the strapping (boot) pins?
07

Set up the Arduino IDE

Install the Arduino IDE, add ESP32 board support, select your board and port, and flash the Blink example. Blink uses the onboard LED, so it proves the toolchain end to end before you wire anything. Open the serial monitor at 115200 baud.

Check on learning
Why flash the Blink example first?
What baud rate does this build use for the serial monitor?
08

Power and the breadboard

You learn how the breadboard rows and power rails connect, and how to power the ESP32 safely from USB or a supply. Most first faults live here: the wrong rail, reversed power, or a short.

Check on learning
What are the long side rails on a breadboard used for?
Most first faults on a new breadboard build come from...
09

Button and LED

Wire a push button as an input and an LED as an output: press the button, the LED lights. This is the whole loop at its simplest, and where you handle debounce in code.

Press to light. The button pulls GPIO4 to ground when pressed; the firmware reads that and drives GPIO5, lighting the LED through its current-limiting resistor.
Schematic (the electrical path)ESP32-S3GPIO4GPIO53V3pull-up ≈45kbutton330 ΩLEDGND
On the breadboard++123456789AEFJGPIO5+GPIO4AB− rail = GND, to ESP32
FromToWhy
GPIO5resistor, end Adrives the LED
resistor, end BLED long leg +limits current
LED short leg −− rail (GND)completes the loop
GPIO4button terminal Athe input pin
button terminal B− rail (GND)a press pulls GPIO4 low
The electrical why A red LED drops about 2 volts when lit, so across the 330 Ω resistor the current is 1.3 V ÷ 330 Ω ≈ 3.9 mA: bright, and well under the ~20 mA a pin should source. Drop the resistor and the current runs to tens of milliamps and cooks both the LED and the pin. The button needs no resistor because the chip supplies one: INPUT_PULLUP ties GPIO4 to 3.3 V through an internal ~45k resistor, so an open button reads HIGH and a press (straight to ground) reads LOW, drawing only 3.3 V ÷ 45k ≈ 73 µA. Use plain INPUT and the open pin floats, reading noise.
Button orientation A tactile button has four legs: the two on each side are the same terminal. Seat it across the center channel so a press bridges the two halves. Wire it the wrong way (both legs of one terminal) and it reads pressed all the time.
Check on learning
In the button-and-LED loop, what is the input and what is the output?
The LED needs which part in series, and why?
10

Drive a real load

An LED draws little; a GPIO pin can source only a small current, roughly 20 milliamps in safe use and about 40 at its absolute limit. To switch a relay, a lamp, or a buzzer, you drive a transistor or MOSFET from the pin, and put a flyback diode across a relay coil.

A coil kicks back When a relay or motor coil switches off, its collapsing magnetic field produces a reverse voltage spike that can destroy the transistor driving it. A flyback diode across the coil shorts that spike harmlessly. Never switch a coil without one.
Switch a load the pin cannot. The pin drives the transistor's base through a resistor; the transistor switches the relay coil's larger current; the diode absorbs the coil's turn-off spike.
Schematic (the electrical path)ESP32-S3GPIO51k2N2222BCErelay coil1N5817+5VGND
On the breadboard+5V123456789AEGPIO51k2N2222EBCRELAY 5V1N5817 (flyback)
FromToWhy
GPIO51k resistor → transistor basea tiny base current
transistor emitter− rail (GND)low-side switch
transistor collectorrelay coil pin 1switches the coil
relay coil pin 2+5V railcoil runs on 5V, not 3.3
flyback diode (band to +5V)across the coilabsorbs the turn-off spike
The electrical why A pin sources only ~20 mA; a 5 V relay coil pulls far more, so the pin never drives the coil. Instead it feeds the transistor's base through the 1k resistor: with the base-emitter drop of ~0.7 V, the base current is (3.3 − 0.7) V ÷ 1k ≈ 2.6 mA, and the transistor's gain lets that switch the much larger coil current to ground. The coil runs on +5V (USB/VBUS), not the 3.3 V logic rail.
The flyback diode is not optional A relay coil is an inductor. When the transistor switches off, the coil's collapsing field drives a high-voltage reverse spike that will punch through the transistor. The diode, reverse-biased in normal use (band toward +5V), gives that spike a path to circulate and clamps it. Leave it out and the transistor dies, often on the first release. 2N2222 leg order (E–B–C) varies by maker. Confirm the silkscreen on your part.
Check on learning
Why can a GPIO pin not drive a relay directly?
What does the flyback diode across a relay coil do?
11

Add a timer

You make the output hold after the input, keeping the LED or relay on for a set time, three ways: the RC time constant, the 555 timer, and the ESP32's own millis(). That completes a working input-decide-output loop before you add a single sensor.

Interactive Falstad CircuitJS: the RC time constant, the capacitor charging on a curve.
Where a delay comes from. A capacitor charging through a resistor rises on a curve; the time it takes is set by R and C. That curve is the root of every hardware timer.
RC schematic3V3RCVc+
The charge curveVmax63%capacitor voltagetime →
Way to make a delayNeedsTrade-off
RC time constanta resistor + a capacitorsimple, but approximate and fixed
555 timerthe 555 chip + R + Ca dedicated timer, no code
millis() (this build)nothing, code onlyexact, adjustable, never blocks
The electrical why The capacitor fills through the resistor like water through a narrow pipe: the bigger the resistor or the capacitor, the slower it fills. The time constant is τ = R × C. With R = 10k and C = 100µF, τ = 1 second. After one τ the capacitor is at 63% of the supply; after about five it is full. That rising curve is how an RC or a 555 makes a delay in hardware. This build uses millis() instead: it reads the ESP32's running clock, so the hold time costs no parts, is exact, and can change in code. Paired with the non-blocking pattern from the firmware unit, it never freezes the loop the way delay() would.
Check on learning
You want the relay to stay on for 30 seconds after motion. On the ESP32 you do this with...
The RC time constant sets...
12

Motion

Replace the button with a PIR motion sensor (HC-SR501), an automatic switch that closes when it detects movement. Wire it, read its output, and set its warm-up and retrigger behavior.

Read an active sensor. The HC-SR501 decides for itself and drives OUT HIGH on motion; you just read it like a button, no resistor.
Schematic (the electrical path)ESP32-S3HC-SR501GPIO4+5VVCCOUTGNDGND
On the breadboard+5V123456789AEPIR HC-SR501VCCOUTGNDGPIO4middle pin = OUT (always)
FromToWhy
HC-SR501 VCC+5V railthe module needs 5V
HC-SR501 OUT (middle)GPIO4digital input, HIGH on motion
HC-SR501 GND− rail (GND)ground
The electrical why A PIR senses the infrared a warm body gives off. When it detects motion it drives OUT HIGH; otherwise OUT is LOW. The module powers itself from 5 V (VCC) and actively drives OUT both ways, so you read it as a plain digital input, with no pull-up and no resistor. On most HC-SR501 boards OUT swings 0 to ~3.3 V even with 5 V on VCC, which is safe for the 3.3 V pin. The two trimmers set sensitivity and how long OUT stays HIGH after a trigger.
Warm-up, retrigger, and confirm the output Give it ~30 to 60 seconds to settle after power-up; ignore readings during warm-up. The on-board jumper sets retrigger mode (repeat while motion continues, or one pulse per event). The middle pin is always OUT, but the outer VCC and GND vary by maker, so read the silkscreen. And confirm OUT measures ~3.3 V on your board: if it swings to 5 V, add a divider or level shifter before the pin.
Check on learning
You power the node and the PIR fires constantly for the first minute, then settles. What happened?
How does the ESP32 read the HC-SR501 output?
13

Magnetic

Add a reed or hall sensor, a switch a magnet closes. Wire it to watch a door or a drawer, and read it the same way you read the button.

A switch a magnet closes. Wire the reed exactly like the button: across the pin and ground, with the pin's internal pull-up. A magnet nearby closes it.
Schematic (the electrical path)ESP32-S3GPIO43V3pull-up ≈45kreed (magnet closes)GND
On the breadboard+123456789AEGPIO4reedno resistor: INPUT_PULLUP does it
FromToWhy
GPIO4reed switch, end Athe input pin
reed switch, end B− rail (GND)a magnet pulls GPIO4 low
The electrical why A reed switch is two thin metal blades sealed in a glass tube; a nearby magnet pulls them together and closes the circuit. Electrically it is identical to the button: set the pin to INPUT_PULLUP so it idles HIGH, and a closed reed pulls it to ground and reads LOW. No external resistor, the chip supplies the pull-up. It is non-polar, so either leg can go to the pin. Mount the reed on the frame and the magnet on the door or lid: together they sit closed, apart they open, so the pin tells you whether the door is shut.
Check on learning
How is a reed switch read, compared to the button?
A reed switch with a magnet on a door tells you...
14

Light

This is your first analog read. A photoresistor in a voltage divider gives the ESP32 a voltage that changes with light, which you read on an ADC1 pin: not on or off, but how much.

Interactive Falstad CircuitJS: the voltage divider, with the photoresistor's resistance sliding as the light changes.
Turn light into a voltage. The LDR and a fixed 10k resistor split 3.3 volts; the midpoint voltage rises and falls with the light, and the ADC reads it.
Schematic (the electrical path)ESP32-S3GPIO1 · ADC13V3LDR10kGND
On the breadboard3V3123456789AEGPIO1LDR10kcol 4 = midpoint = ADC
FromToWhy
3V3 railLDR, end Atop of the divider
LDR, end B10k resistor, end A (midpoint)the sensed node
midpointGPIO1 (ADC1)the ADC reads this voltage
10k resistor, end B− rail (GND)bottom of the divider
The electrical why An LDR changes resistance with light (bright ≈ a few kΩ, dark ≈ hundreds of kΩ), but the ADC reads voltage, not resistance. The fixed 10k turns the changing resistance into a changing voltage: the midpoint sits at 3.3 V × 10k ÷ (RLDR + 10k). Bright (LDR ~1k) → ~3.0 V; dark (LDR ~200k) → ~0.16 V. The ESP32's 12-bit ADC maps 0–3.3 V onto 0–4095, so analogRead() swings with the light. Pick the fixed resistor near the LDR's mid-range so the swing lands in a useful part of the scale.
ADC1 only, and stay under 3.3 V Read analog on an ADC1 pin (GPIO1–10). The other converter, ADC2, stops working the moment WiFi turns on, and the node runs on WiFi. Keep the divider on the 3.3 V rail, never 5 V: no ESP32 pin tolerates more than 3.3 V, and a 5 V divider would push the midpoint past that.
Check on learning
A photoresistor alone is not enough to read on the ADC. What do you build with it?
You read the light value on which pin?
15

Temperature

Read heat two ways: a thermistor on the analog divider, or a DS18B20 over the 1-Wire bus. Pick the one whose interface you will reuse most.

1-Wire needs its pull-up The DS18B20 data line floats without a 4.7k resistor pulled up to 3.3 volts. Leave it out and the sensor reads nothing, or garbage. This is the same defined-resting-state rule from the switch unit, on a data bus.
One wire carries the data. The DS18B20 takes power and ground and talks over a single data line; that line needs a pull-up resistor to work.
Schematic (the electrical path)ESP32-S3DS18B20GPIO44.7k3V3VDDDQGNDGND
On the breadboard3V3123456789AEDS18B20GNDDQVDDGPIO44.7kpull-up holds the data line HIGH
FromToWhy
DS18B20 VDD3V3 railpower
DS18B20 GND− rail (GND)ground
DS18B20 DQGPIO4the 1-Wire data line
4.7k resistorDQ ↔ 3V3pull-up: holds the line HIGH
The electrical why 1-Wire sends commands and readings both ways over a single data line, plus power and ground. The line is open-drain: a device can only pull it LOW; nothing drives it HIGH. The 4.7k pull-up to 3.3 V is what holds the line HIGH when idle, so the LOW pulses read cleanly. The DS18B20 is powered normally here (VDD to 3.3 V); it can also run "parasite" off the data line, but normal power is simpler and more reliable.
No pull-up means no sensor Leave the 4.7k out and the floating line never reads HIGH: the library returns -127 (its disconnected value) or finds no device at all. Use a real pull-up, not INPUT_PULLUP; the internal pull-up is too weak for the 1-Wire timing. The TO-92 pin order (GND–DQ–VDD shown) varies by maker, and reversing VDD and GND can cook the part, so confirm the printing on your sensor before powering up.
Check on learning
The DS18B20 talks over which interface, and what does its data line need?
Why might you choose the DS18B20 over a thermistor?
16

Put it on the network

Connect the node to WiFi and tie it into the network from Module 01. It reports readings and alerts to your server, takes commands so you can control its output from your server, and updates over the air so you can reprogram it without a cable.

Check on learning
How does the node get its readings to your team?
What does OTA let you do?
17

Verify and own it

Run the whole node: it senses, decides, acts, and reports.

Before you call it finished, build it once from a brief, with no walkthrough. Make every decision yourself.

You own one more thing too: reading a symptom back to its cause. Work through the faults that catch everyone before you meet them on the bench.

Then the honest part: it works on a breadboard, but it is not deployable as is. Soldering makes it permanent, CAD prints its enclosure, and Salvage sources its parts. Those are the next skills.

Check on learning
The node works on a breadboard. Why is it not deployable as is?
Which skill makes the breadboard circuit permanent?

Glossary

Plain definitions for every term, as it is used in this build.

1-Wire
A one-data-line bus, plus power and ground, used by the DS18B20 temperature sensor. The data line needs a 4.7k pull-up resistor.
555 timer
A small timer chip that makes a fixed delay or a steady pulse from one resistor and one capacitor, with no code. One of three ways this module holds an output on after a trigger; the others are the RC time constant and the ESP32's millis().
ADC
Analog-to-digital converter. Turns a voltage between 0 and 3.3 volts into a number from 0 to 4095, so the ESP32 can read an analog sensor. Use ADC1 (GPIO 1 to 10); ADC2 stops working with WiFi on.
debounce
Ignoring the few milliseconds of contact bounce after a switch closes, so one press reads as one press, not several. Done in code or with a small capacitor.
duty cycle
The fraction of time a signal is on. A part driven at 30 percent duty cycle is on for 30 percent of each period; used to dim or slow without changing the voltage.
flyback diode
A diode across a relay or motor coil that absorbs the voltage spike when the coil switches off. Without it, that spike can destroy the transistor driving the coil.
GPIO
General-purpose input/output. A pin you can read (input) or drive (output) in code. The ESP32's GPIO is 3.3 volt logic.
Hall sensor
A magnetic sensor, like a reed switch but solid-state: no moving contact, it reports a nearby magnet electronically. Used the same way as a reed switch to tell whether a door or lid is open.
level shifter
A small circuit that translates a signal between two voltages, for example a 5 volt sensor output down to the 3.3 volts the ESP32 can safely read. Needed whenever a part's output swings above 3.3 volts.
MOSFET
A transistor used as an electronic switch. A small voltage on its gate lets a much larger current flow, so a 3.3 volt pin can switch a relay, a lamp, or a motor.
OTA
Over-the-air update. Reflashing the ESP32's firmware over WiFi instead of with a cable.
PIR
Passive infrared sensor. Detects motion from the infrared a warm body gives off, and outputs a digital HIGH when it triggers. The HC-SR501 is the common part.
pull-up / pull-down
A resistor that ties an input pin to a known state, high or low, so it does not float and read noise. The ESP32 has built-in pull-ups (INPUT_PULLUP).
reed switch
A switch that closes when a magnet is near. Read as a digital input, the same as a button; used to tell whether a door or lid is open.
thermistor
A resistor whose resistance changes with temperature. Read as an analog value through a voltage divider.
TO-92
The small black half-cylinder plastic package with three legs in a row, used by the 2N2222 transistor and the DS18B20 sensor. The flat printed face is the reference for which leg is which.
transistor
A part that switches or amplifies: a small current or voltage on one leg controls a much larger current through the other two, so a 3.3 volt pin can switch a load it could never drive directly. The 2N2222 (an NPN bipolar transistor) is this module's part; a MOSFET does the same job with a voltage on its gate.
voltage divider
Two resistors in series that split a voltage. With a sensor (a photoresistor or thermistor) as one of them, the middle voltage tracks the sensor and the ADC reads it.