Sub topic 2
Selecting the board and port, then uploading
This chapter assumes you have the Arduino IDE installed, a USB cable, and a basic grasp of voltage, current, and resistance.
Selecting the board and port
Before you upload a sketch (the Arduino term for a program), the IDE must know two things. The board tells the compiler which chip and pin layout to target. The port is the USB connection the computer uses to reach the board.
- Open Tools > Board and choose your board, such as Arduino Uno.
- Open Tools > Port and choose the port that appears when you plug the board in. Windows shows
COM3, macOS shows/dev/cu.usbmodem..., and Linux shows/dev/ttyACM0.
Uploading the blink sketch
Paste this sketch into the editor. It uses the built-in LED, which the IDE names LED_BUILTIN.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}Click the Upload button (the right arrow). The status bar reports "Done uploading" when the transfer succeeds.
Making the blink visible with delay()
delay(1000) pauses the program for 1000 milliseconds, which is one second. Without a pause, loop() would switch the LED on and off so fast that your eye would see a steady glow. The delay keeps each state on screen long enough to notice.
Which way round an LED goes
An LED is a diode, so current flows through it in only one direction. The anode is the positive leg, usually the longer one. The cathode is the negative leg, usually the shorter one, and it sits next to a flat edge on the plastic body.
Why the LED needs a resistor, and how to size it
An LED has a roughly fixed forward voltage (Vf), the drop it takes to conduct. A pin supplies a voltage (Vs), and without a resistor the current rises until the LED overheats. A series resistor limits the current:
R = (Vs − Vf) / I
For a red LED (Vf ≈ 2 V) on a 5 V pin with a target of 20 mA, R = (5 − 2) / 0.020 = 150 Ω. A 220 Ω resistor is the nearest common value and gives about 13.6 mA, which is safe.
| Supply (Vs) | Forward voltage (Vf) | Target current (I) | Resistor (R) |
|---|---|---|---|
| 5 V | 2 V | 20 mA | 150 Ω |
| 5 V | 2 V | 13.6 mA | 220 Ω |
Exercise:
Exercise
A blue LED has Vf = 3 V and you run it from a 5 V pin at 10 mA. What resistor value do you need, and what is the nearest common value?
Show answer
R = (5 − 3) / 0.010 = 200 Ω. The nearest common value is 200 Ω, or 220 Ω if you want a little extra margin.