Low Level Arduino Programming — LED Blink (Part 1)

Required Tools

sudo apt install gcc-avr avrdude

Registers

Modifying Registers

PORTB |= (1 << PORTB5);
PORTB |= (1 << PORTB5) | (1 << PORTB2);
PORTB &= ~(1 << PORTB5);
PORTB &= ~((1 << PORTB5) | (1 << PORTB2));
PORTB |= _BV(PORTB5); // _BV(x) is the same as (1 << x)

Controlling I/O Pins

Pin Layout

The pins on the Arduino Uno.

The I/O Registers

DDRB

We will need to set DDB5 to 1 to configure the LED as an output.

PORTB

PINB

Bringing It All Together

#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB |= _BV(DDB5); // Set LED as an output
while (1)
{
PORTB |= _BV(PORTB5);
_delay_ms(500);
PORTB &= ~_BV(PORTB5);
_delay_ms(500);
}
return 0; // We will never get here!
}
while (1)
{
PINB |= _BV(PINB5);
_delay_ms(500);
}

Building and Programming

avr-gcc -Os -DF_CPU=16000000 -mmcu=atmega328p -o blink.elf blink.c
avr-objcopy -O ihex blink.elf blink.hex
avrdude -p atmega328p -c arduino -P /dev/ttyACM0 -b 115200 -U flash:w:blink.hex

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Build Infrastructure On AWS cloud with a single command using Terraform

Infrastructure as code using AWS and Terraform

Daily blog of the first-time founder

Technical Documentation Tips

<span>Photo by <a href=”https://unsplash.com/@nicolasthomas?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Nicolas Thomas</a> on <a href=”https://unsplash.com/s/photos/ink?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></span>

The Future of Mean Stack Technology

Integrating Ansible with Docker

PyTorch Inference Server on GKE

Basic Login Page using PHP and MySQLi API

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sam Munday-Hall

Sam Munday-Hall

More from Medium

Working with QR Codes in Embedded Linux and ARM-powered devices using the C++ language (static…

Deciphering C declarations

A Web Server from ESP-32

Libraries in C language