Playing around with ATtiny85 microcontroller using Arduino IDE.

In this post, I want to show you how to setup your Arduino IDE to program an ATtiny85 using the DigiSpark development board. I have divided the content into five subsections:

  1. Brief overview of Digispark development board.
  2. Add Digispark board to Arduino IDE.
  3. Write the classic ‘blinky’ program using board’s led.
  4. Micronucleus troubleshooting.
  5. Upgrading Digispark firmware (optional).
  1. The Digispark development board

This board provides full access to the ATtiny85 board through USB (!). It comes in a small form factor that integrates a USB connector as part of the PCB, so, no cables needed. This tiny microcontroller (uC) comes with 8k, 512 and 512 bytes of flash, EEPROM and SRAM respectively. It is equipped with several timers, 10-bits ADC, programmable watchdog timer and USI (Universal Serial Interface), which enables hardware SPI and I2C communication protocols. Surprisingly powerful for such a tiny 8-pins controller wit if you ask me.

Its price: $7.95 USD if you get an original board from Digistump or a bit more than $1 USD for a clone version on AliExpress.; not bad.

Why this little dev board is interesting? Mainly, its USB interface.

As you might have noticed, I did not mention USB as part of the ATtiny85 capabilities, and the reason is because it does not have native USB support. The USB protocol is bit-banged using micronucleus—a lightweight firmware running on the device. Micronucleous is an small (less than 2k) bootloader with a minimal USB interface that allow us to program the device directly through USB: neither USB-to-UART converter nor AVR programmer is required.

Digispark can be programmed using Arduino IDE—no need to run the flashing tools from the command line. Of course, there is no such a thing as a free lunch: we are left with ~6k of flash memory for our program instead of 8k (due to bootloader size)—an acceptable trade-off considering the benefits.

(Clone) Digispark dev board
Digitspark board pinout

  1. Adding digispark board to Arduino IDE.

I’m using Arduino IDE 1.8.13 (the latest version at the moment of writing this) on a Linux computer (Manajaro distro).

First, we need to add Digistump’s URL to Arduino IDE. Go to File->Preferences, and add http://digistump.com/package_digistump_index.json as an additional Board Manager URL (see image below).

Adding Digistump (Digispark board designers) URL to Arduino IDE Boards Manager.

Then, go to Tools-> Board -> Boards Manager and install Digitstump AVR boards.

Digistump AVR Boards

Finally, we need to select Digispark Default (16.5 mhz) board (available in the recently added Digistump AVR Boards section. We’re all set to write our first application.

2. Our first program

As it is customary in the embedded software world, our first program will make a led blink—the embedded equivalent of the classic “Hello World!” program.

Connect the digispark after pressing upload. If the board is already connected, unplug and plug it again. Depending on the bootloader version, upon power-on, the bootloader waits up to 6 seconds for a new program to be loaded, then starts the loaded user application (if any). Some bootloader versions make the on-board led flash rapidly during those 6 seconds. Please be aware that some boards connects this led to pin 0 instead of pin 1.

/*
 * Blink example on Digispark board.
 */

// Some board versions connect the led to pin 0 instead of 1.
int led_pin = 1;

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

void loop(){
  digitalWrite(led_pin,HIGH);
  delay(1000);
  digitalWrite(led_pin,LOW);
  delay(1000);
}

If everything goes well, you will now see the on-board led change state every second. The mapping to the board’s GPIOs is straightforward: there are 6 pins on the board [PB0 – PB5], PBx is mapped to pin x. In some boards (like the one I’m using) PB5 is configured as RESET pin and thus, it cannot be used as an standard GPIO. I will probably show how to change that in a different post.

3. Micronucleus troubleshooting

If everything went well for you, you don’t need need to read this section. If uploading fails for you—as it happened to me–some troubleshooting is required.

I got the error shown below, which means that the firmware loaded in the Digispark is not recognized by the programming tool.

We have described micronucleus as a firmware loaded in our thingy which does all the magic; well, that’s only half of the story. The other half is a set of tools running on the host computer (i.e. the computer you are using to program the uC). One of these tools is the micronucleus command line application. This tool it is used in the backend by Arduino IDE to “talk” to the firmware running on the device and load an user application. Apparently, the command line application is too old, and does not recognize the firmware version running on my device. Because the tool does not recognize the firmware version, it does not know how (or it does not even try) to load our blinky app.

Since I’m running the latest Arduino IDE version I would expect to have the latest micronucleus tool version, for some reason, that’s not the case. The fix is fairly simple, but requires two manual steps: first, build the command line tool using the latest micronucleus release and then replace it in in your Arduino installation directory.

The latest release can be downloaded from here: https://github.com/micronucleus/micronucleus/releases/tag/2.04

Download and unpack the release file. Go to micronucleus-2.04/commandline/ and run the makefile to build the Linux binary (Windows executable is already there, lucky you Windows guys). This step is trivial, but you might need to install libusb libraries if they aren’t already present in your system. The procedure to do that changes from distro to distro, so I will leave it up to you to figure that out.

Now, replace the micronucleus binary in /home/{YOUR_USERNAME}/.arduino15/packages/digistump/tools/micronucleus/2.0a4/ with the one you built and try to upload an arduino sketch again. If successful, you should see something like:

4. Upgrading Digispark firmware (optional)

As shown in the image above, my board is running micronucleus v2.2; a decently recent version, but not the latest one. So, let’s upgrade it.

Within the micronucleus release folder, you can find a firmware and an upgrade folder. The firmware folder contains the source code needed to build the bootloader running on the device. The upgrade folder contains all the files to build a self-upgradable version of such firmware. This version can be used to load a new bootloader using the one running on the device, no AVR programmer is needed—take a look at readme.txt and technical details.txt if you want to know how this is accomplished. We are going to use the last version to get the latest firmware installed.

To make things easier for us, both firmware binaries versions are provided as part of the release, so that we don’t need to compile them. For our board, we need micronucleus-2.04/upgrade/releases/upgrade-t85_default.hex

To upgrade the firmware, run the following command:

./micronucleus --run upgrade-t85_default.hex

If everything works fine, you will the output shown in the image above. You can now load a new sketch and the firmware version should be 2.4.

Well, it is not. The version shown on my device is 2.3 (!).

I thought I made a mistake, but after comparing the differences between release 2.3 and 2.4 in the micronucleus git repository, I found that they forgot to updated the binaries inside the upgrade folder. If you want to load the latest version, you need to build it yourself. Alternatively, you can find both the micronucleus and the self-upgradable firmware at the end of this post.

Conclusions

We’ve learned how to setup the Arduino IDE (in Linux) to use the digispark board and load a simple test program. We also showed how to fix basic compatibility issues with micronucleus and upgrade the firmware running on the ATtiny to the latest version. In future posts, we are going to explore what useful things we can do using this tiny development board.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s