Blinking a DigiSpark without Arduino IDE

Arduino IDE is great, however, some times I enjoy a simple C program and a simple Makefile. Here is a simple application skeleton allowing to blink a LED of course.

All information and files below are freely inspired from the following readings :

Requirements :

  • AVR gcc (Already installed if you have installed Arduino IDE from packages)
    sudo apt-get install gcc-avr

Source code :

#include <avr/io.h>
#include <util/delay.h>

// Digispark Internal LED
#define PIN_LED     PB1
#define DELAY_MS    500

int main(void) {
  // Init LED pin as output
  DDRB |= (1 << PIN_LED);
  // Light up LED
  PORTB |= (1 << PIN_LED);
  
  // Blink !
  for (;;) {
    PORTB ^= (1 << PIN_LED);
    _delay_ms(DELAY_MS);
  }

  return 0;
}

Makefile

DEVICE     = attiny85           # See avr-help for all possible devices
CLOCK      = 16000000           # 16Mhz
OBJECTS    = main.o             # Add more objects for each .c file here

UPLOAD = micronucleus --run
COMPILE = avr-gcc -Wall -Os -DF_CPU=$(CLOCK) -mmcu=$(DEVICE)

# symbolic targets:
all:	main.hex

.c.o:
	$(COMPILE) -c $< -o $@

flash:	all
	$(UPLOAD) main.hex

clean:
	rm -f main.hex main.elf $(OBJECTS)

main.elf: $(OBJECTS)
	$(COMPILE) -o main.elf $(OBJECTS)

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

 

2 Replies to “Blinking a DigiSpark without Arduino IDE”

  1. Awesome, thanks 🙂
    One thing to add: your dependency install should read “sudo apt-get install gcc-avr avr-libc” for the example to compile successfully.

  2. Thanks! I actually used these steps in raspberry pi and they work.

    Just a note: Before firmware install (i.e. running micronucleus) switch off power to attiny85. Run the program and then switch on the power.

    Also, micronucleus should be run as root (“sudo micronucleus…”) unless steps have been followed to make it work without root. I point this out specifically because otherwise nothing happens and one keeps wondering whether code is wrong.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.