Learning Goals 5 min
By the end of this lesson you will be able to:
- Tell, at a glance, which of the four pin operations a line of code is doing — digital read, digital write, analog read (true analog input), or analog write (PWM output, misleadingly named).
- Name which pins on a Uno support which operations — D0–D13 are digital, A0–A5 are analog inputs that can also act as digital pins, and only the six ~-marked pins support
analogWrite. - Read or write a sketch that uses all four operations side by side without confusing the naming or wiring up the wrong pin.
Warm-Up 10 min
You've now used four different pin-related functions across the syllabus — digitalWrite, digitalRead, analogWrite, and (since L01-36) analogRead. They sound like a tidy 2×2 set: "digital or analog" × "read or write". But Arduino's naming hides a real twist that confuses every beginner. Today we untwist it.
Quick-fire puzzle
Here are three lines of code from past lessons. For each, answer: which pin is it talking to, and what physical thing is the pin doing?
analogWrite(9, 128);— pin 9 is doing what, exactly?analogRead(A0);— and pin A0?- Is pin 9 in line 1 the same kind of pin as pin A0 in line 2?
Reveal the answer
- Pin 9 is a digital pin.
analogWriteis misleadingly named — it doesn't output an analog voltage; it switches the digital pin on and off very fast (PWM, from L01-31). The "128" controls the on/off ratio, which your eye averages into a brightness. - Pin A0 is a true analog input pin.
analogReadreally does measure a continuous voltage from 0–5 V using a real analog-to-digital converter chip inside the Arduino. - No. Pin 9 is a digital pin that can simulate analog output via PWM. Pin A0 is an actual analog input. They're in different rows on the board, served by different hardware. The shared word "analog" in the two function names hides this.
This is the single biggest source of confusion when learning Arduino. The C++ keyword "analog" in analogWrite and analogRead refers to different things. Once you've internalised the difference — once today — you'll never trip over it again.
New Concept — four operations, two kinds of pin 20 min
The big idea — digital vs analog, plus a naming trap
A pin can do two things in two directions = four operations. Three of them are honestly named. One isn't. Here's the truth in one table.
| Operation | Function | Direction | What the pin does | Returns/Takes |
|---|---|---|---|---|
| Digital write | digitalWrite(pin, …) | Output | Holds the pin at 0 V (LOW) or 5 V (HIGH). | HIGH or LOW |
| Digital read | digitalRead(pin) | Input | Asks "is the pin near 0 V or near 5 V?" | HIGH or LOW |
| Analog read | analogRead(pin) | Input | Measures the pin's voltage continuously from 0 V to 5 V. | 0 to 1023 |
| "Analog write" (actually PWM) | analogWrite(pin, …) | Output | Switches the pin between 0 V and 5 V fast, with the on-time ratio set by your number. | 0 to 255 |
The pin layout — two rows, double duty
Look at your Uno. The pins are in two rows:
- Top row — digital pins, D0–D13. Fourteen pins. All support
digitalWrite,digitalRead, andpinMode(... OUTPUT / INPUT / INPUT_PULLUP). Six of them — D3, D5, D6, D9, D10, D11 — also supportanalogWrite, marked with a ~ beside the number. - Bottom-right row — analog input pins, A0–A5. Six pins. All support
analogRead(the only pins that do!). They also support every digital operation if you address them with their digital aliases D14–D19, or just by writingA0toA5in calls likedigitalWrite(A0, HIGH).
So pins A0–A5 are flexible: when used as analog inputs they go through the ADC; when used as digital they behave like extra digital pins. Pins D0–D13 are not flexible — they can't do analogRead, ever.
The naming trap, explained honestly
The Arduino designers in 2005 picked names that match what the user perceives:
analogReadreads a value that feels analog (0–1023, a smooth range). Honest name — it really is analog.analogWriteoutputs a value that feels analog to your eye (a smooth dimming). But under the hood it's still digital on/off, just very fast. The honest name would have beenpwmWrite. Too late now — the world usesanalogWriteand the name is permanent.
When you read someone else's code, mentally translate:
analogWrite(pin, value) // = PWM output. Works on the six ~ pins only.
analogRead(pin) // = real analog input. Works on A0..A5 only.Why each operation needs different hardware
- Digital write is the simplest: the chip just connects the pin to its internal +5 V rail or its GND rail. One transistor.
- Digital read uses a comparator that asks "is the pin closer to 5 V or to 0 V?" There's no in-between answer.
- Analog write (PWM) uses a timer that flips the pin between HIGH and LOW about 490 times per second, with the ratio set by your value. The Uno only has three timers, and they're shared between PWM pins, the
tone()function, andmillis()— which is why only six pins support PWM (each pair shares one timer) and whytone()on pin 11 can disrupt other PWM (L01-32). - Analog read uses a single analog-to-digital converter (ADC) chip shared across all six analog pins. The chip is more expensive (in chip-real-estate terms) than the digital input circuit — which is why there are only six of them.
The "true analog output" gap
Notice what's missing from the table: true analog output. There's no analogWrite that produces a real 2.5 V on a pin. The Uno simply doesn't have the hardware for it. PWM is the workaround: switch fast, let the load (LED, motor, your eye) do the averaging. Higher-end Arduinos (like the Due or some MKR boards) include a DAC — Digital-to-Analog Converter — that can output a real intermediate voltage. The Uno doesn't. If you ever truly need an analog voltage on an Uno, you wire a low-pass filter (a resistor and capacitor) on a PWM pin to "smooth" the fast switching into a steady voltage — but that's a Level 2 topic.
Why it matters
This is the lesson where the foundation gets cemented. Every sensor you meet from here on is some flavour of "read it with analogRead on an A-pin"; every actuator you've used is either "drive it with digitalWrite" (LED on/off, buzzer on/off) or "drive it with analogWrite on a tilde pin" (LED dimming, motor speed). Knowing exactly which pin can do which operation — and which function actually does what — is the difference between "the LED doesn't light, I don't know why" and "the LED doesn't light, so I picked the wrong pin and here's the fix".
Worked Example — one sketch, all four operations 20 min
You'll build a single sketch that uses every pin operation in plain sight. One button (digital read), one LDR (analog read), one plain LED (digital write), one dimmable LED (analog write / PWM). Watch the Serial Monitor as you press, cover, uncover, and the LEDs react in two different ways at once.
The wiring
Nothing new — just everything you already have, on one board:
- Button on D7 with
INPUT_PULLUP(L01-17 circuit). - Plain LED on D8 with a 220 Ω resistor (L01-07 circuit). Pin D8 is not a tilde pin — perfect for digitalWrite-only.
- Dimmable LED on ~D9 with a 220 Ω resistor. The tilde matters — analogWrite needs PWM.
- LDR + 10 kΩ on A0 (L01-36 voltage divider).
- One shared GND rail back to the Arduino.
The sketch
// All four pin operations in one sketch
const int BUTTON_PIN = 7; // digital input
const int PLAIN_LED = 8; // digital output (no ~)
const int DIM_LED = 9; // PWM output (~)
const int LDR_PIN = A0; // analog input (A-row)
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP); // digital setup
pinMode(PLAIN_LED, OUTPUT); // digital setup
pinMode(DIM_LED, OUTPUT); // digital setup (PWM uses OUTPUT too)
// No pinMode needed for analogRead
}
void loop() {
int btn = digitalRead(BUTTON_PIN); // 1) digital read → HIGH/LOW
int light = analogRead(LDR_PIN); // 2) analog read → 0..1023
digitalWrite(PLAIN_LED, btn == LOW); // 3) digital write → on while pressed
analogWrite(DIM_LED, light / 4); // 4) PWM write → brighter with light
Serial.print("btn="); Serial.print(btn);
Serial.print(" light="); Serial.print(light);
Serial.print(" pwm="); Serial.println(light / 4);
delay(100);
}Upload and try every operation
- Upload at 9600 baud. Open the Monitor.
- You should see a stream like
btn=1 light=620 pwm=155. The button reads 1 (HIGH, released); the LDR reads ~620 in normal room light; the PWM LED on D9 glows at brightness ~155. - Press the button:
btn=0appears, the plain LED on D8 lights up. - Cover the LDR with your hand:
lightdrops below 100, the PWM LED on D9 dims toward off. - Shine a torch at the LDR:
lightclimbs toward 1000, the PWM LED brightens almost to full.
Two outputs reacting differently — one on/off, one smoothly — driven by two different inputs — one binary, one continuous — all in 20 lines.
Match function to pin — fill in the table
For each line of the worked example's loop(), identify the operation, the function name, the physical pin, and the value involved.
| Line | Function | Operation type | Pin | Value type |
|---|---|---|---|---|
digitalRead(BUTTON_PIN) | digitalRead | digital read | D7 | HIGH or LOW |
analogRead(LDR_PIN) | ____ | ____ | ____ | ____ |
digitalWrite(PLAIN_LED, …) | ____ | ____ | ____ | ____ |
analogWrite(DIM_LED, …) | ____ | ____ (remember the naming trap!) | ____ | ____ |
If you can fill in all four rows from memory, you've internalised the 2×2.
Try It Yourself — three pin experiments 15 min
Goal: A pin-capability quiz. Without uploading, predict what each of these lines does — or whether it would even compile / behave usefully on a Uno. Then test the ones you can.
// (a) analogWrite(9, 200);
// (b) analogWrite(8, 200);
// (c) analogRead(9);
// (d) analogRead(A0);
// (e) digitalWrite(A0, HIGH);
// (f) digitalRead(A3);Questions — answer each before reading the next:
- (a) Pin 9 — is it a ~ pin? Will the LED dim smoothly? ____
- (b) Pin 8 — is it a ~ pin? What happens for values 0–127 vs 128–255? ____
- (c)
analogRead(9)— does this read pin D9? Or does it read something completely different? ____ (Hint:analogRead's parameter is an analog channel number, not a digital pin.analogRead(0)would read A0;analogRead(9)isn't a valid channel on a Uno.) - (d) and (f) — both use A-pins. One is the "natural" use; the other is the "double-duty" use. Which is which? ____
- (e) — yes, A0 can be a digital output. Has it ever come up in this syllabus? Why might you actually want to? ____ (Hint: running out of digital pins on a big project.)
Goal: Use A2 as a plain digital input. Move your L01-17 button from D7 to A2 — same INPUT_PULLUP wiring. The sketch then drives the plain LED on D8 exactly as before, but with the button reading from an analog pin.
const int BUTTON_PIN = A2; // analog pin used as digital input
const int LED_PIN = 8;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP); // works on A2 just like D7
pinMode(LED_PIN, OUTPUT);
}
void loop() {
int b = digitalRead(BUTTON_PIN);
digitalWrite(LED_PIN, b == LOW);
}Questions:
- Why does
pinMode(A2, INPUT_PULLUP)work even though A2 is "an analog pin"? ____ - If you ran this sketch on a Uno project that also used
analogRead(A2), what would happen? ____ (Hint: a pin can do one job at a time.) - You have 14 digital pins (D0–D13) and 6 analog pins (A0–A5). If a project needs twenty buttons, you'd run out of digital pins. Does this idea solve it, and how many buttons can you then handle? ____
Goal: Read the same physical pin two ways at once. Wire your LDR voltage divider to A0. In one loop pass, do both analogRead(A0) and digitalRead(A0). Print both numbers and watch how the analog value crosses the digital "is it HIGH or LOW?" threshold.
void setup() {
Serial.begin(9600);
}
void loop() {
int a = analogRead(A0); // 0..1023, continuous
int d = digitalRead(A0); // HIGH (1) or LOW (0)
Serial.print("analog="); Serial.print(a);
Serial.print(" digital="); Serial.println(d);
delay(100);
}Cover and uncover the LDR slowly. Watch the analog value swing smoothly while the digital value snaps from 0 to 1 somewhere in the middle.
Questions:
- Roughly what analog reading does the digital flip happen at — close to 100, close to 500, or close to 900? ____ (Hint: it's about 2.5 V — half of 5 V — which is around the middle of the 0–1023 range.)
- Why is this useful? When would you read both ways of the same pin? ____ (Hint: rarely useful in production; very useful for learning.)
- The digital flip threshold is fixed by the chip; you can't change it. The analog comparison threshold (
if (a < 300)) is set by you. Which approach is more flexible — and why is that almost always worth the extra line of code? ____
Mini-Challenge — pin-operation cheat sheet 10 min
"Build the table you'll keep open on a sticky note"
Build a sketch that doesn't move any LEDs or buttons — it just runs once in setup() and prints a perfectly formatted reference table of every pin-operation combination, with the Uno's specifics filled in. The output is what you'll save as your forever cheat sheet.
Target output (lines may wrap a little in the Monitor — that's fine):
=== Arduino Uno: pin operations cheat sheet ===
digitalWrite : OUTPUT any HIGH/LOW D0-D13, A0-A5 (as D14-D19)
digitalRead : INPUT any HIGH/LOW D0-D13, A0-A5
analogWrite : OUTPUT 0..255 (PWM) D3, D5, D6, D9, D10, D11 (~)
analogRead : INPUT 0..1023 A0..A5 ONLY
=== Remember: analogWrite is misnamed — it's PWM on a digital pin ===Your task:
- In
setup()only, callSerial.begin(9600), then a sequence ofSerial.printlncalls that produce exactly the output above. loop()stays empty.- The benefit isn't the sketch — it's the act of writing out the truth in your own words. By the time you've typed and read through this once, you'll remember it. Future you can re-upload the sketch and re-read the table whenever you need to check.
It works if:
- Opening the Monitor (or resetting the Arduino with it open) shows the table once.
- Every fact in the table is true — no hardcoded wrong numbers.
- You can read your own table back and instantly answer "what does
analogWritedo? and on which pins?" without hesitation.
Reveal one valid sketch
// Pin-operation cheat sheet — print once at startup
void setup() {
Serial.begin(9600);
delay(500); // give the Monitor a moment to open
Serial.println("=== Arduino Uno: pin operations cheat sheet ===");
Serial.println("digitalWrite : OUTPUT any HIGH/LOW D0-D13, A0-A5 (as D14-D19)");
Serial.println("digitalRead : INPUT any HIGH/LOW D0-D13, A0-A5");
Serial.println("analogWrite : OUTPUT 0..255 (PWM) D3, D5, D6, D9, D10, D11 (~)");
Serial.println("analogRead : INPUT 0..1023 A0..A5 ONLY");
Serial.println("=== Remember: analogWrite is misnamed — it's PWM on a digital pin ===");
}
void loop() { }Five lines of fact, each one of them the answer to a question you'll otherwise look up dozens of times. Keep this .ino file in a folder called references/ alongside whatever project you're working on. When in doubt about a pin's capabilities, the table is one upload away.
Recap 5 min
A pin can do four operations: digital read, digital write, analog read (true), analog write (actually PWM — a misleading name). The Uno has 14 digital pins (D0–D13) and 6 analog input pins (A0–A5). All 20 can do digital read/write; only the six tilde pins (D3, D5, D6, D9, D10, D11) can do PWM "analog write"; only the six A-pins can do true analogRead. The confusing function name is analogWrite — it's PWM on a digital pin, not analog output. Once that's locked in your head, every sensor in Cluster F (and the rest of this syllabus) just plugs in and reads.
- Digital pin
- A pin whose value is either HIGH (~5 V) or LOW (~0 V), with no in-between. On a Uno: D0–D13.
- Analog input pin
- A pin that can be read as a continuous voltage from 0 to 5 V using the chip's ADC. On a Uno: A0–A5. These can also be used as digital pins if needed.
- PWM pin (~)
- A digital pin that supports
analogWrite— fast on/off switching whose duty cycle simulates analog brightness. On a Uno: D3, D5, D6, D9, D10, D11. Marked with a tilde printed beside the pin number on the board. - The four operations
digitalWrite(output HIGH/LOW),digitalRead(input HIGH/LOW),analogWrite(PWM output 0–255),analogRead(true analog input 0–1023). Together they cover every input/output a Uno can do.- ADC (analog-to-digital converter)
- The chip inside the Arduino that converts an analog voltage to a 10-bit integer (0–1023). Shared by all six A-pins; one
analogReadcall takes about 100 µs. - Naming trap
- The fact that
analogWriteis misleadingly named — it doesn't write a real analog voltage, it does PWM. The C++ keyword "analog" in the two function names refers to different things. Mentally translateanalogWriteaspwmWrite.
Homework 5 min
The "all four" sketch, rebuilt from memory. Without looking back at today's worked example, rewrite a sketch that uses all four pin operations in one loop(). Your version can drive any combination of components — the choices below are suggestions, not requirements:
- A button (digital read) controls whether a buzzer beeps (digital write).
- An LDR (analog read) controls an LED's brightness (analog write / PWM).
Wire it on a fresh patch of breadboard. Upload. Verify each operation works as you expect by interacting with the inputs and watching the outputs.
Also: a design reflection on paper.
- If someone shows you a line of code
analogWrite(11, 200)and asks "is the pin 11 doing analog?", what do you answer? Use one sentence — be precise about what the pin is doing physically. ____ - Sketch the Uno's pin layout on paper from memory. Number the digital pins on top (D0–D13) with tildes next to D3, D5, D6, D9, D10, D11. Label the analog row (A0–A5) on the bottom right. Check against the board after — what did you get wrong, if anything? ____
- What's the difference between "the pin reads 2.5 V" (in real life) and the value
analogReadreturns? ____ (Hint:analogRead(2.5)isn't a thing; you'd get back the integer 511.) - If Arduino's designers got a do-over, what would have been a better name than
analogWrite? ____
Bring back next class:
- The saved
.inofile (call itall-four-operations). - A photo of the wiring on your breadboard.
- Your four written reflection answers + the pin-layout sketch, in your notebook.
Heads up for next class: L01-38 "Voltage Dividers Explained" opens the hood on the divider you used in L01-36 — why does the LDR + 10 kΩ pair actually produce a useful voltage? What happens if you change the 10 kΩ to 1 kΩ or 100 kΩ? It's the maths behind every analog sensor circuit in the rest of the cluster.