Fanlab

Robotics · Microcontrollers · Grade 7

Arduino Uno: the first program

Build the board yourself, plug in the USB, upload Blink — and what is the secret of pin 13?

Open the experiment

The situation

For the first robot in the club you need an Arduino Uno. But first build the board yourself: starting from a bare board, place the parts, solder them, plug in the USB and upload the first program — Blink. Then you will want to change the brightness smoothly, but the light on pin 13 does not fade smoothly, it only “switches on and off”. Why? Which pins can do PWM? And what happens if you fit the capacitor the wrong way round or connect 5V to GND?

Topic

Arduino Uno: assembling the board and the first program. From a bare board to a working Arduino: placing parts, soldering, USB, Blink and Fade

Key ideas

  • Microcontroller
  • Pins and PWM
  • setup() / loop()

Formulas

T=ton+toff

The Blink period: the sum of the `delay()` calls — 1000 + 1000 ms = 2 s, frequency 0.5 Hz.

D=tonT=v255

PWM duty cycle: `analogWrite(pin, v)` — at a frequency of 490 Hz the light is on for a fraction v/255 of the time; the eye sees the average brightness.

I=5V−ULEDR+Rpin

The current of an external LED: the pin gives ≈ 5 V and has its own output resistance of ≈ 25 Ω; the limit for a pin is 40 mA, the normal value is 20 mA.

Iavg=D⋅I

With PWM the average current (and brightness) is proportional to the duty cycle.

Theory

What the board is made of

The heart of the Arduino Uno R3 is the ATmega328P microcontroller (a 28-pin DIP package, sitting in a socket): with a 16 MHz crystal it makes 16 million steps a second. The ATmega16U2 is a second chip that talks to the computer over USB. The 5 V and 3.3 V regulators prepare the power, and a 500 mA polyfuse (resettable fuse) protects the USB port from a short circuit. The black headers at the edges are the pins: DIGITAL 0–13, ANALOG IN A0–A5, POWER (5V, 3V3, GND, VIN).

Assembly order

At the factory the tiny SMD parts (chips, resistors, capacitors) are first placed on solder paste and “flow” into place in an oven at 245 °C. Then the large through-hole (THT) parts — sockets, headers, capacitors, the crystal, the DIP socket — are pushed into the holes and soldered from underneath. An unsoldered leg means no contact: if you plug USB into such a board, nothing lights up.

setup() and loop()

Every Arduino program has two functions: setup() runs once (setting up the pins), loop() repeats endlessly. `pinMode(13, OUTPUT)` makes the pin an output, `digitalWrite(13, HIGH)` gives it 5 V (LOW — 0 V), `delay(1000)` pauses the program for 1000 ms. The L light on the board is connected to pin 13 — that is why Blink works without any external parts.

PWM: why pin 13 does not fade smoothly

`analogWrite(pin, v)` does not give an analogue voltage — it switches the pin on and off ≈ 490 times a second, with an on-fraction of v/255. This is PWM (pulse-width modulation); the eye sees the average brightness. On the Uno, PWM works only on the pins marked ~: 3, 5, 6, 9, 10, 11. On any other pin (pin 13 too) `analogWrite` acts like an ordinary switch: v<128 — LOW, v≥128 — HIGH. That is why Fade on pin 13 is abrupt, not smooth.

The external LED and the pin limit

A pin is a small source: ≈ 5 V and an internal ≈ 25 Ω. An LED must have a resistor: with 220 Ω the current is ≈ 12–13 mA. Without a resistor the current exceeds 100 mA — the absolute limit for a pin is 40 mA: the chip heats up and is destroyed (smoke — the die has burnt). If you fit the capacitor the wrong way round, the electrolyte heats up, the can swells and bursts. A 5V–GND short circuit — the polyfuse cuts the current; remove the short and it cools down and conducts again.

About the model

The program runs on a small part of Arduino C (int/float, if, for, while, functions, Serial). `int` is 16-bit — above 32 767 it becomes negative, just like on a real board. Time comes from the simulation clock: at 1× speed Blink runs in real seconds. Soldering, heating and smoke are simplified: a real chip can survive excess current briefly, but its life gets shorter.

Did you know?

  • Arduino began in 2005 in Ivrea, Italy (Interaction Design Institute Ivrea) as a cheap teaching board for students; its name comes from the café “Bar di Re Arduino” in Ivrea.
  • The ATmega328P has only 2 KB of working memory (SRAM) and 32 KB of program memory — millions of times less than an ordinary smartphone, but enough for a robot, because it executes 16 million instructions a second.

Inside the experiment

A real board in a hand-inked isometric drawing: turn it, place the parts, wire the circuit, write and upload the code — current and voltage are computed at once, and a wrong connection burns a part. Inside are missions, theory, a quiz and a guided tour.