Unit 03 · Phase 1 · Electrical foundations
Microcontroller inputs and timing
Mission: Make the LED blink on a timing you predicted, and count every button press exactly once.
The interactive version of this unit needs JavaScript. Below are its steps as text.
Steps
1. The board runs a loop
Look at the board on the left. Inside its chip a program runs three jobs in a circle: read the inputs, decide, write the outputs. Press Run the loop and watch the gold dot and the pass counter.
Why it matters: A microcontroller is a small computer on one chip that runs one program from power-on. That program is usually a loop that reads inputs, decides and writes outputs, over and over. A real board can go round thousands of times per second, so the stage slows it down for you. Everything in this unit sits inside that loop.
2. A pin is a small logic signal
The LED and 1 kΩ resistor from Unit 02 now hang off pin 13. A trace strip has appeared at the bottom. Press Set pin 13 HIGH and watch the trace and the LED.
Why it matters: A general-purpose input/output pin (GPIO pin) is a connection the program can set HIGH or LOW. On this 5 V board HIGH is close to 5 V and LOW is close to 0 V: a logic level, read as 1 or 0. The program picks the level, and the resistor still sets the current, about 3 mA here. The strip draws the level against time, like an oscilloscope.
3. A pin is not a motor supply
A small motor sits beside the board. Press Try the motor on pin 13 and watch the current gauge. Then watch the model rewire it the safe way.
Why it matters: A GPIO pin carries information, not power. The chip on Uno-style boards (ATmega328P) lists 40 mA per pin as an absolute maximum, a damage limit, and about 20 mA is a common working figure; check your own board’s datasheet. A small hobby motor can draw hundreds of mA, more as it starts, so it goes through a driver with its own supply. This is Unit 01’s rule again: the information path and the power path stay separate.
4. Blink: on, wait, off, wait
The program is now: set pin 13 HIGH, wait 500 ms, set it LOW, wait 500 ms, repeat. Predict, then press Run Blink and count the flashes in the 3 s trace window.
Why it matters: Waiting is how this program sets time: a delay of 500 ms, where ms means milliseconds, thousandths of a second. One HIGH plus one LOW is one cycle, and the length of a cycle is the period, here 1000 ms. Because the loop repeats, the pattern repeats. A timing you can predict is the first half of this unit’s mission.
5. Period, frequency and duty cycle
The program changes to 200 ms on and 800 ms off. Predict how the blink will look, then press Run the new timing and read the two brackets on the trace.
Why it matters: Frequency is how many cycles happen each second: 1 ÷ period, so a 1000 ms period gives 1 hertz (Hz). Duty cycle is the share of each period spent HIGH: 200 ms ÷ 1000 ms = 20 %. Two blinks can share a frequency and still differ in duty cycle. Duty cycle returns when motors are driven by fast pulses.
6. Wire a button, nothing else
A push button now joins pin 2 to ground (GND, the 0 V return). Nothing else is connected to pin 2. Keep your hands off the button and press Read pin 2.
Why it matters: With the button open, pin 2 connects to nothing: it is a floating input. An input pin draws almost no current, so stray charge and nearby wires can push it either way. The program can’t tell a real press from this noise. The pin needs a default level.
7. Give the pin a default: pull-up
Press Turn on the pull-up and press. The program switches on a resistor inside the chip between pin 2 and 5 V; then the model holds the button down once.
Why it matters: A pull-up resistor gently connects the pin to the logic supply, so it reads HIGH when nothing else drives it. Pressing the button connects the pin straight to ground, which wins, so pressed reads LOW: active-low. The chip’s internal pull-up is roughly 20–50 kΩ, so only about 0.1–0.25 mA flows while pressed. A pull-down is the mirror image: resistor to ground, button to 5 V, and pressed reads HIGH.
8. Zoom into the contact: bounce
Press Push once, zoomed in. The magnifier shows the metal contact inside the button, and the trace now spans only 20 ms. The log counts every HIGH-to-LOW change as a press.
Why it matters: This is switch bounce: the springy metal contact rattles for a few ms before it settles. The loop is fast enough to see each rattle as a separate edge, a change from one level to the other. That is why one press can look like many. Bounce time differs between switches and changes as they wear.
9. Debounce: wait until it settles
The program now accepts a new button level only after the raw reading has held steady for 20 ms. Press Push with 20 ms debounce and compare the raw trace with the accepted trace below it.
Why it matters: Debouncing means ignoring changes that don’t last. Each new raw edge restarts a 20 ms timer, and only a level that holds for the whole 20 ms is accepted. That delays every press by 20 ms, too little to notice. Too long a wait would swallow quick taps; the Play tab lets you find that edge.
10. Blink and listen at once
One program now does both jobs: Blink with 500 ms waits, and read pin 2 once per pass of the loop. Three presses arrive while it runs. Press Run Blink and the presses; the ticks on the pin 2 trace mark each moment the program reads it.
Why it matters: While the program waits, the loop is stuck and reads nothing. A press shorter than the wait can come and go between reads and is simply missed. The fix is not a shorter wait but no waiting at all: check the clock on every pass instead.
11. Check the clock, never wait
The program now reads the board’s millisecond clock, millis(), on every pass. It flips the LED when its time is up and reads pin 2 every pass, with a 20 ms debounce. Ten bouncy presses arrive. Press Run ten presses.
Why it matters: Instead of waiting, the loop asks “has enough time passed?” and moves on, so it is free to read inputs on every pass. The timing comes from the clock, not from stalling. This pattern, often called blink without delay, lets one small program do several jobs at once. Your bench test runs this program with real presses.
12. What if the ground wire is loose?
Same program, same ten presses, but the button’s ground wire sits loose in the breadboard. Press Wiggle the ground wire and watch the wire, the trace and the log.
Why it matters: The button works only if its other side really reaches ground. A loose jumper can open for a moment, and then a press does nothing; it can also flicker during a press. Missing presses point to wiring, and extra presses point to bounce or a floating pin. Pushing every wire home and checking it against the diagram fixes most of these.
13. Your bench: blink, then ten presses
Press Show the wiring order to number each wire in the order you build it. Then open the Notebook tab, write your planned timing and prediction first, and run the real test.
Why it matters: Your own counts are the evidence; the simulation only shows what to look for. Time ten blinks with a stopwatch and divide by ten to get the period. Press the button ten times on purpose and count the log lines. If the counts disagree, use this unit’s floating, bounce and loose-ground checks one at a time.
Go deeper
Math: time, frequency and duty cycle
period T = t_on + t_off (ms) frequency f = 1 / T (T in s gives Hz) duty cycle D = t_on / T (× 100 for %) Example: 200 ms on, 800 ms off T = 1000 ms = 1 s, f = 1 Hz, D = 200 / 1000 = 20 %
The bench sketch
Arduino-style C++ for the lab. It blinks without waiting and logs each debounced press to the serial monitor. Change ON_MS and OFF_MS to your planned timing before uploading.
// Unit 03 bench sketch (Arduino-style C++, 5 V Uno-style board).
// Blinks pin 13 without delay() and logs each debounced press on pin 2.
const int LED_PIN = 13;
const int BUTTON_PIN = 2; // button from pin 2 to GND
const unsigned long ON_MS = 200; // your planned on time
const unsigned long OFF_MS = 800; // your planned off time
const unsigned long DEBOUNCE_MS = 20;
bool ledOn = false;
unsigned long ledChangedAt = 0;
int lastRaw = HIGH, stable = HIGH;
unsigned long rawChangedAt = 0, presses = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // internal pull-up: idle HIGH, pressed LOW
Serial.begin(9600);
}
void loop() {
unsigned long now = millis();
// Write: flip the LED when its time is up (never wait).
if (now - ledChangedAt >= (ledOn ? ON_MS : OFF_MS)) {
ledOn = !ledOn;
digitalWrite(LED_PIN, ledOn ? HIGH : LOW);
ledChangedAt = now;
}
// Read and decide: accept a new level only once it has held for DEBOUNCE_MS.
int raw = digitalRead(BUTTON_PIN);
if (raw != lastRaw) { lastRaw = raw; rawChangedAt = now; }
if (now - rawChangedAt >= DEBOUNCE_MS && raw != stable) {
stable = raw;
if (stable == LOW) {
presses++;
Serial.print("press "); Serial.print(presses);
Serial.print(" at "); Serial.print(now); Serial.println(" ms");
}
}
}Logic levels in more detail
A pin does not need exactly 0 V or 5 V to read as LOW or HIGH. The ATmega328P datasheet guarantees HIGH above 0.6 × supply (3 V on a 5 V board) and LOW below 0.3 × supply (1.5 V). Between those, the reading is not guaranteed, which is one reason a floating pin misbehaves.
Model limits
- The stage slows the loop down; real loop speed depends on how much the code does.
- The floating-input trace is an illustration, not a physical simulation; real floating pins respond to nearby wiring, hands and mains hum.
- Bounce times are illustrative. Real switches vary, and their bounce grows as they wear.
- The 20 mA working figure and 40 mA absolute maximum are from the ATmega328P; the whole chip also has a total current limit. Use your own board’s documentation.
- millis() wraps around after about 49.7 days. Subtracting times as unsigned numbers, as the sketch does, keeps the comparison correct across the wrap.
Public video question
Why can one button press look like many?
Watch and read
- MIT 6.002 Circuits and Electronics, Lecture 1: circuit abstraction (MIT OpenCourseWare, archived 2000 lecture). Why a pin can be treated as a 0 or 1 signal. Basic algebra is enough; watch selected parts.
- Arduino IDE built-in examples: Blink (01.Basics), and Button, Debounce and BlinkWithoutDelay (02.Digital).
- Jack Ganssle, “A Guide to Debouncing”: bench measurements of real switch bounce and hardware and software fixes.