Built-in project · ATmega328P, 16 MHz crystal

ATmega328P on a breadboard

The Uno’s chip on its own in a breadboard, wired as the tutorial wires it: 5 V on VCC and AVCC, both grounds, RESET held high, and an LED on PB5, which is pin 13 in code. The whole build, an ATmega328P on a breadboard and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 20 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $8.20. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • What a bare microcontroller needs to run
  • Pull-up resistor on the RESET pin
  • Current-limiting resistor for an LED
  • Blinking with delay
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: ATmega328P, 16 MHz crystal
  • Parts: 6

How it works

This is the chip from an Uno, pulled out and set on a breadboard by itself. It needs a few connections to wake up: 5 V on VCC and AVCC, ground on its GND pins, and the RESET pin held high through a 10k resistor.

The sketch is the classic blink. It sets the LED pin as an output, then switches it on for half a second and off for half a second, forever. The LED sits on PB5 (the Uno's pin 13) through a 220 Ω resistor to ground.

Parts list

6 parts, plus the jumper wires. They retail for about $8.20; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
ATmega328P, 16 MHz crystal pin AVCCATmega328P, 16 MHz crystal pin VCC
Resistor, 10k Ω pin 1ATmega328P, 16 MHz crystal pin VCC
LED, red pin CATmega328P, 16 MHz crystal pin GND
Resistor, 10k Ω pin 2ATmega328P, 16 MHz crystal pin RESET
Resistor, 220 Ω pin 1ATmega328P, 16 MHz crystal pin PB5
LED, red pin AResistor, 220 Ω pin 2

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · ATmega328P, 16 MHz crystal
// Blink: the smallest thing a board can do.
//
// Pin 13 has the LED soldered next to the USB socket, so LED_BUILTIN is 13.
// Wire an LED and a 220 ohm resistor from pin 13 to GND to see it on the
// breadboard too.
//
// delay() puts the chip to sleep between timer ticks rather than spinning, so
// this sketch costs the simulator almost nothing while it waits.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Runs once at power-up. pinMode(LED_BUILTIN, OUTPUT) makes the LED pin an output so the chip can drive current out of it. On this chip LED_BUILTIN is pin 13, which is the PB5 leg wired to the resistor.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

loop sketch.ino

Runs over and over. It sets the pin HIGH, waits 500 ms, sets it LOW, and waits another 500 ms. That makes one full blink per second.

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The chip does nothing and the LED never lights This project
    Check that VCC, AVCC and both GND pins are connected, and that RESET is pulled up to 5 V through the 10k resistor.
  • The LED stays dark even though the chip is running This project
    LEDs only work one way around. The anode (A) goes toward the resistor, and the cathode (C) goes to ground.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.
  • The chip does not run at all. ATmega328P
    Connect both VCC (pin 7) and AVCC (pin 20) to 5 V, and both GND pins (8 and 22). Add 100 nF capacitors close to the supply pins. Pull RESET (pin 1) high through 10 k.
  • The sketch will not upload, or runs very slowly or oddly. ATmega328P
    A 16 MHz crystal needs two 22 pF capacitors from each leg to GND, close to pins 9 and 10. The chip also needs the Arduino bootloader burned, or use an ISP programmer.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.