Lesson 2 of 6 · 15 min
Blink: your first program
Every embedded journey starts with one pin going high and low. Blink is not a toy: it proves that your toolchain works, that the board is alive, and that you understand the three things every program on a microcontroller does: configure, act, wait.
The circuit
An LED is a diode: current flows one way only, and it needs a resistor to limit that current. Without one, the LED (and possibly the pin) burns out in a moment.
How big should the resistor be? Ohm's law, with the LED dropping about 2 V:
R = (Vpin - Vled) / I = (5 V - 2 V) / 0.015 A = 200 Ω
The nearest standard value is 220 Ω. That gives about 13.6 mA: bright enough, safely under the limit.
Blue pins: analog inputs. Orange pins: digital I/O.
The program
void setup() {
pinMode(13, OUTPUT); // configure: pin 13 drives current out
}
void loop() {
digitalWrite(13, HIGH); // act: pin 13 to 5 V, LED on
delay(500); // wait
digitalWrite(13, LOW); // act: pin 13 to 0 V, LED off
delay(500); // wait
}
setup() runs once at power-up. loop() runs forever. That is the whole execution model.
Try it
Change the delay, press Run, and watch the pin.
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}What delay() really does
delay(500) makes the CPU spin in a loop for 500 ms. Nothing else can happen during that time: no button reads, no sensor polling. For Blink that is fine. Later in this course you will replace it with millis(), which lets you keep several timers running at once.
Check yourself
Which function runs exactly once, when the board powers up?
Check yourself
You power the LED from 5 V, it drops 2 V, and you want 10 mA. What resistor value do you need?