Built-in project · ATmega328P, 8 MHz internal clock

ATmega328P at 8 MHz

An ATmega328P in a breadboard on its internal 8 MHz clock, with no crystal, running a blink built for 8 MHz. The whole build, an ATmega328P on its 8 MHz clock 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 15 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

  • Running a chip alone on a breadboard
  • The internal clock versus a crystal
  • Why the build must match the clock speed
  • A pull-up resistor on RESET
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: ATmega328P, 8 MHz internal clock
  • Parts: 6

How it works

This is a bare ATmega328P chip on a breadboard, with no crystal. It runs from its own internal 8 MHz clock. Power goes to VCC and AVCC, and both GND pins go to ground. A 10k resistor ties RESET to 5 V so the chip does not reset by accident.

The LED sits on PB5, which the sketch calls LED_BUILTIN. The 220 ohm resistor limits the current. The sketch turns the pin on for half a second, then off for half a second, forever.

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, 8 MHz internal clock pin AVCCATmega328P, 8 MHz internal clock pin VCC
Resistor, 10k Ω pin 1ATmega328P, 8 MHz internal clock pin VCC
LED, red pin CATmega328P, 8 MHz internal clock pin GND
Resistor, 10k Ω pin 2ATmega328P, 8 MHz internal clock pin RESET
Resistor, 220 Ω pin 1ATmega328P, 8 MHz internal clock 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, 8 MHz internal clock
// Blink, on a Pro Mini running at 8 MHz from 3.3 V.
//
// The LED is on pin 13, as it is on an Uno, so LED_BUILTIN is 13 here too.
// What is different is underneath: this image was built with F_CPU set to
// 8 MHz, so delay(500) counts half as many timer ticks as it would on a 16 MHz
// board and still lasts half a second. Load the Nano's blink onto this board
// instead and it flashes at half the speed, which is what happens on the
// bench when the board setting in the IDE is wrong.

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. It sets LED_BUILTIN as an output so the pin can drive the LED. Nothing else needs preparing.

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

loop sketch.ino

Sets the pin HIGH, waits delay(500), sets it LOW, waits again. The sketch was built with F_CPU set to 8 MHz, so delay(500) still lasts half a second. Code built for 16 MHz would blink at half speed on this chip.

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 LED blinks at the wrong speed This project
    The code was built for a different clock. Build for 8 MHz so delay counts correctly.
  • The chip does not run at all This project
    Check VCC, AVCC and both GND pins, and that the 10k resistor joins RESET to 5 V.
  • 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, or timing is wrong. ATmega328P
    Burn the bootloader or fuses for the 8 MHz internal oscillator first, and select that board setting. Code made for 16 MHz runs at half speed, so delay and serial baud rate are off.
  • Upload fails. ATmega328P
    A bare chip needs a programmer or a USB serial adapter with the reset capacitor. Connect VCC, AVCC and all the GND pins. Add a 10 k pull-up on RESET and a 0.1 uF cap near the supply.

Change it and keep it

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