Digispark: Getting Started - Blink
Difficulty Level | Beginner |
Note: This documentation is for programming the Digispark with
avr-gcc
andMakefiles
directly, not with the Arduino IDE.
Now that we've got micronucleus
up and running on our Digispark (Micronucleus Getting Started) it's time for the hello world of microcontrollers: the blink program!
Repo: https://github.com/matthew-macgregor/digispark-attiny85-experiments/tree/main/1-blink
Our code looks like so:
#include <avr/io.h>
#include <util/delay.h>
// Digispark LED is on Pin 1 for newer versions
#define LED PB1
#define DELAY_MS 1000
int main(void) {
DDRB |= (1 << LED); // Set pin to output
PORTB |= (1 << LED); // Set pin to high
for (;;) {
PORTB ^= (1 << LED);
_delay_ms(DELAY_MS);
}
return 0;
}
We don't need a circuit diagram or a breadboard for this one because we'll just blink the onboard LED. We can use the Makefile
below.
- To build:
make
- To flash:
make flash
Peeking under the hood, the command to actually load the hex
onto the board with micronucleus
is incredibly simple:
micronucleus --run main.hex
Note: The Digispark programs a bit differently than other boards. Run the command above, then plug in the board (it will prompt you). A trick I found was to connect a jumper wire to
P5 RESET
and connect it to ground, and then disconnect it. This seems to trigger programming.
# Depends:
# gcc-avr avr-libc micronucleus
DEVICE = attiny85
CLOCK = 16000000 # 16mhz
OBJ = main.o
# Make sure micronucleus is in PATH or change LOADER
LOADER = micronucleus
GCC = avr-gcc -Wall -Os -DF_CPU=$(CLOCK) -mmcu=$(DEVICE)
.PHONY: clean all
all: main.hex
.c.o:
$(GCC) -c $< -o $@
flash: all
$(LOADER) --run main.hex
clean:
rm -f main.hex main.elf $(OBJ)
main.elf: $(OBJ)
$(GCC) -o main.elf $(OBJ)
main.hex: main.elf
rm -f main.hex
avr-objcopy -j .text -j .data -O ihex main.elf main.hex
avr-size --format=avr --mcu=$(DEVICE) main.elf