Skip to content
You Start with one site
Amounts in Start with one site
Engineering · Embedded Systems & Electronics

Embedded Programming: ATmega328P

Program the ATmega328P in plain C, register by register, with no Arduino library. Ten projects, from a Morse beacon to a stepper controller that reads G-code.

Found this useful?
The wiring of the last lesson: PD4 and PD5 of the ATmega328P drive STEP and DIR of an A4988 driver, which drives the two coils of a NEMA 17 motor from 12 V. A limit switch goes to PD2, INT0, and a 10 kilohm jog potentiometer to PC0, ADC0.

A button reads as pressed when nobody touched it. A program will not upload, and the one-click tool hides which step failed. A sensor on the bench gives a number that jumps between 507 and 516. A battery sensor in the field is dead after three weeks.

Each of these comes from one bit, one setting or one small current that a library hides. In this course you write to the registers yourself, with the datasheet open. Each lesson adds one peripheral, and the last lesson joins them all in one controller.

How each lesson goes
  1. 1Start from a problemA 2 ms pulse comes and goes while the loop is busy.
  2. 2Find the registersWhich bits control the part, from the datasheet.
  3. 3Write the Cavr-gcc and avrdude. Only the code you wrote goes in.
  4. 4Wire it and test itOn a breadboard, one or two new parts each lesson.
You will be able to
  1. Build and upload C programs with your own toolchain, and see every step from code to chip
  2. Drive pins, timers, interrupts, UART, SPI, I2C and the ADC from their registers
  3. Make a device sleep for years on two AA cells, and join every part in one controller

New to this? New to embedded? Start with lesson 1: install the tools and blink your name in Morse.

You need An Arduino Nano or Uno, a breadboard and the few parts of each lesson. avr-gcc and avrdude on Windows, Mac or Linux.

The engineering behind it

The simplest processor that can take a reading and act on it, with nothing hidden.

Lessons
Lesson 1 · 8 minAVR Toolchain and Bare-Metal CA Morse beacon that blinks your name, built with your own Makefile, in 428 bytes.avr-gccavrdudeMakefileFuses Lesson 2 · 7 minGPIO Registers and Digital I/OAn electronic dice with four LEDs and a button, with no digitalRead() and no digitalWrite().DDR, PORT, PINBit masksPull-upsLFSR Lesson 3 · 8 minTimer/Counter FundamentalsA tone generator: two buttons step the pitch, and a timer drives the buzzer by itself.PrescalerCTCPWM Lesson 4 · 6 minInterrupts and Event-Driven DesignA reaction tester: an LED lights at a random moment, and the board times your press in milliseconds.INT0Timer interruptsvolatileAtomic blocks Lesson 5 · 6 minUART Serial CommunicationA temperature logger that sends one reading a second to your computer as a line of text.Baud rateRing bufferprintfThermistor Lesson 6 · 7 minSPI and Peripheral InterfacingA clock on a 128x64 OLED, with the display driver written from scratch.SPI modesSSD1306Pages Lesson 7 · 8 minI2C Bus and Sensor IntegrationA weather station: a BME280 on two shared wires, its readings on the OLED.TWIAddressesACK and NACKPull-ups Lesson 8 · 7 minADC and Analog AcquisitionTwo light sensors as two bars on the OLED, with readings that hold still.10-bit ADCReferenceOversamplingDividers Lesson 9 · 12 minPower Management and WatchdogA door alert on two AA cells: it sleeps, wakes when the door opens, sounds and sleeps again.Sleep modesWatchdogEEPROMBattery life Lesson 10 · 8 minStepper Motor and Motion ControlOne axis of a CNC machine: G-code over serial, timed step pulses, homing on a limit switch.G-codeTimer1 stepsHomingEEPROM
More about this course

Every number has a reason

A register value is not copied from a forum. Each number comes from the clock, the part on the pin or the battery.

  • Baud rateboth ends must agree; from 16 MHz, 57,600 and 115,200 are more than 2 % offUART
  • Compare valuewith the prescaler, sets the frequency the timer makes by itselfTimers
  • Pull-up resistor4.7 kΩ on each I2C wire; without them a sensor answers nothingI2C
  • Readings averaged16 of them, so a reading on the bench holds stillADC
  • Sleep current5 mA awake lasts about 21 days on two AA cells; asleep, the door alert lasts about 2.5 yearsPower

A sketch is not bare metal

Arduino sketch
digitalWrite() and delay()
  • Timers and pins set up for you, out of sight
  • Blink compiles to 924 bytes
  • One click to upload

Best to try an idea in minutes.

Bare-metal C
DDRx, PORTx and PINx
  • Every register write is yours
  • The Morse beacon fits in 428 bytes
  • Each build step in your own Makefile

Best when you must know why it fails.

The tools

avr-gcc, avrdude and makeThe steps the Arduino IDE hides, in plain view.
The datasheetEvery lesson reads its register tables.
Nano or UnoThe same chip and pins; the code works on either.
No debugger neededA USB cable and a serial terminal to start.
Ideas that carry overRegisters, interrupts and buses work this way on every chip.
Windows, Mac and LinuxThe same steps on every system.

Honest limits. An 8-bit chip with 32 KB of flash and 2 KB of RAM, and no radio. The STM32 and ESP32 courses take the same ideas further.

Before you start

  • No embedded experience needed
  • C basics: variables, loops, functions and bit operators
  • Voltage, current and resistance
  • An Arduino Nano or Uno, a breadboard and a USB cable
The full course notes

Learn embedded systems from the ground up by programming the ATmega328P in pure C. No Arduino IDE, no abstraction layers. Every register write is intentional, every peripheral is understood at the datasheet level. Each lesson builds a real, working project with minimal components. #EmbeddedSystems #AVR #BareMetal

Why Bare-Metal ATmega328P?

Understand, Don't Abstract

The Arduino IDE hides what matters. Writing to DDRx, PORTx, and PINx directly teaches you how microcontrollers actually work. This knowledge transfers to every chip you will ever use.

The Datasheet Is Your Teacher

Every lesson references the ATmega328P datasheet directly. You learn to read register descriptions, timing diagrams, and electrical specs. This is the skill that separates hobbyists from engineers.

Smaller, Leaner Firmware

The Arduino IDE’s Blink sketch compiles to 924 bytes of flash. Our Morse code beacon does more and uses under 428 bytes. Bare metal includes only what you write, with no hidden timer setup, pin lookup tables, or runtime overhead.

Real Projects, Real Hardware

Each lesson produces something purposeful: a temperature logger, a weather station, a reaction time tester. You build things worth keeping while learning each peripheral.

Minimal, Affordable Hardware

An Arduino Nano costs under 5 USD. A breadboard and a handful of components is all you need. No expensive dev kits, no debugger required to start. You can begin today.

Course Structure

Every lesson follows the same cycle:

  1. The Problem A real, specific thing to build. Not “learn about timers” but “build a precision metronome.”

  2. The Registers Which registers control this peripheral, what each bit does, and why. Direct datasheet references throughout.

  3. The Code Bare-metal C, compiled with avr-gcc, flashed with avrdude. Every line explained at the register level.

  4. The Build Wire it on a breadboard, flash, and test. Each lesson adds one or two new components to your growing kit.

  5. Going Further Challenges and extensions for those who want to push deeper into the peripheral.

Lessons

Lesson 1: AVR Toolchain and Bare-Metal C

AVR Toolchain and Bare-Metal Setup. Install avr-gcc, avrdude, and write your first Makefile. Build a Morse code beacon that blinks your name on an LED. First contact with register-level programming. Build: Morse code beacon. Parts: Nano, breadboard, LED, 220R resistor.

Lesson 2: GPIO Registers and Digital I/O

GPIO Registers and Digital I/O. Master DDRx, PORTx, PINx registers and bit manipulation. Build an electronic dice with button input and random LED patterns using a simple LFSR. Build: Electronic dice. Parts: 3 more LEDs, push button, 10K resistor.

Lesson 3: Timer/Counter Fundamentals

Timer/Counter Fundamentals. Explore Timer0, Timer1, and Timer2 in Normal, CTC, and PWM modes. Prescalers, overflow interrupts, output compare. Build a tunable tone generator with precise frequencies. Build: Tunable tone generator. Parts: Piezo buzzer.

Lesson 4: Interrupts and Event-Driven Design

Interrupts and Event-Driven Design. Configure INT0/INT1, pin-change interrupts, and timer interrupts. Learn ISR design patterns, volatile variables, and atomic blocks. Build a reaction time tester. Build: Reaction time tester. Parts: Reuse existing.

Lesson 5: UART Serial Communication

UART Serial Communication. Set up the USART peripheral from scratch: baud rate calculation, TX/RX registers, ring buffers, printf redirect. Build a temperature logger that streams CSV to your PC. Build: Temperature logger with CSV export. Parts: 10K thermistor.

Lesson 6: SPI Protocol and Peripheral Interfacing

SPI Protocol and Peripheral Interfacing. Master the SPI peripheral: SPCR, SPSR, clock polarity/phase, master/slave select. Write an SSD1306 OLED driver from scratch. Build a real-time clock display. Build: OLED clock display. Parts: SSD1306 OLED (SPI).

Lesson 7: I2C Bus and Sensor Integration

I2C Bus and Sensor Integration. Implement I2C (TWI) communication: start/stop conditions, addressing, ACK/NACK, multi-byte reads. Interface a BME280 for temperature, humidity, and pressure on your OLED. Build: Mini weather station. Parts: BME280 module.

Lesson 8: ADC and Analog Signal Acquisition

ADC and Analog Signal Acquisition. Configure the 10-bit ADC: reference voltage selection, channel multiplexing, free-running vs single conversion, noise reduction techniques. Build a light-tracking indicator. Build: Light-tracking indicator. Parts: 2 LDRs, 2 resistors.

Lesson 9: Power Management and Watchdog

Power Management and Watchdog. Explore all sleep modes (Idle through Power-down), watchdog timer configuration, BOD disable, EEPROM storage, and wakeup sources. Build a battery-powered door open alert with persistent event counting. Build: Battery-powered door alert. Parts: 2xAA battery holder, reed switch.

Lesson 10: Integrated Stepper Motor and Motion Control

Integrated Stepper Motor and Motion Control. Combine every peripheral from the course into a single-axis CNC-style controller. Parse G-code over UART, generate step pulses with Timer1, home with a limit switch interrupt, jog with an ADC potentiometer, and store calibration in EEPROM. Build: Single-axis stepper controller. Parts: A4988 driver, NEMA 17 motor, limit switch, 10K pot, 12V supply.

Parts Kit

Everything you need for the entire course, beyond the Arduino Nano:

PartQuantityFirst UsedApproximate Cost
Arduino Nano (ATmega328P)1Lesson 13-5 USD
Breadboard + jumper wires1 setLesson 13-5 USD
LEDs (assorted colors)4Lesson 11 USD
220R resistors4Lesson 10.50 USD
10K resistors2Lesson 20.50 USD
Push button1Lesson 20.25 USD
Piezo buzzer1Lesson 30.50 USD
10K NTC thermistor1Lesson 50.50 USD
SSD1306 OLED display (SPI)1Lesson 63-4 USD
BME280 sensor module1Lesson 72-3 USD
LDR photoresistors2Lesson 80.50 USD
Reed switch1Lesson 90.50 USD
2xAA battery holder1Lesson 90.50 USD
A4988 stepper driver module1Lesson 101-2 USD
NEMA 17 stepper motor1Lesson 105-8 USD
Limit switch (microswitch)1Lesson 100.25 USD
12V power supply1Lesson 103-5 USD

Total estimated cost: 25-38 USD (including the Nano)

Skill Progression

SkillLessons 1-3Lessons 4-5Lessons 6-7Lessons 8-9Lesson 10
RegistersGPIO, Timer/CounterInterrupt vectors, USARTSPI, TWI (I2C)ADC, sleep/watchdog, EEPROMAll combined
PatternsPolling, bit manipulationISR design, ring buffersProtocol driversSignal processing, low powerG-code parsing, motion control
HardwareLEDs, buttons, buzzerThermistor, serialOLED display, BME280 sensorLDRs, battery powerStepper motor, driver, limit switch
Toolsavr-gcc, avrdude, makeSerial monitorLogic analyzer (optional)Multimeter for current drawSerial terminal, 12V supply

Prerequisites

No prior embedded experience needed

You should be comfortable with:

  • C programming basics (variables, functions, loops, bitwise operators)
  • Using a terminal/command line
  • Basic electronics (voltage, current, resistance, LEDs, resistors)

Helpful but not required:

  • Arduino experience (we deliberately move beyond it)
  • Our PCB Design with KiCad course (uses the same chip)

Getting Started

  1. Get an Arduino Nano (or any ATmega328P board) and a breadboard with jumper wires.

  2. Install the AVR toolchain: avr-gcc, avr-libc, avrdude. Lesson 1 walks through setup on Linux, macOS, and Windows.

  3. Download the ATmega328P datasheet from Microchip (product page, archive copy). You will reference it in every lesson.

  4. Start with Lesson 1. Each lesson builds on the previous one. Work through them in order.

  5. Build every project. Reading code is not the same as wiring a circuit and watching it work.

© 2021-2026 SiliconWit®. All rights reserved.

10 lessons, in order.

SiliconWit makes these courses. We also run siliconwit.io, a monitoring service.