Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Configuring the Digispark ATtiny85 board for Arduino IDE and upgrading the bootloader

Configuring the Digispark ATTINY85 board for Arduino IDE and upgrading the bootloader

This note describes the configuration of an ATtiny85 based microcontroller development board named Digispark and similar to the Arduino line. It is available in many online marketplaces for roughly 1 dollar (e.g., Ebay, Amazon, AliExpress) and is shipped fully assembled, including a V-USB interface (a software-only implementation of a low-speed USB device for Atmel's AVR microcontrollers). Coding is similar to Arduino: it uses the familiar Arduino IDE and is already provided with a ready-to-use bootloader (fully integrated with Arduino), also allowing to be upgraded. Comparing it with the ATmega328P microcontroller, it is cheaper, smaller, and a bit less powerful. Digispark is copyrighted by Digistump LLC under the Creative Commons Attribution-ShareAlike 3.0.

Adding Digispark support to Arduino

The Arduino IDE needs to be installed first. Check Basic Arduino Installation and Install the Arduino Software (IDE) on Windows PCs for guided installation procedures.

A tutorial published at the Digispark wiki describes how to add the Digispark support to Arduino 1.6.X (1.6.4+) via the Arduino Boards Manager.

Notice the last message of the Preferences form: “More preferences can be edited directly in the file”. If clicking the file name, the related installation directory is opened (the digistump root directory can be found under the packages subdirectory).

To configure the Arduino IDE for the Digispark board, select your target device

Tools -> Boards -> Digispark (Default - 16.5MHz)

Select your programmer

Tools -> Programmer -> Micronucleus

Digispark comes with an old bootloader version (generally 1.06), anyway allowing upgrade.

Digispark Windows PC Driver installation

Three different Windows drivers can be installed for supporting the Digispark board:

  • the firmware upload driver, to upload Arduino sketches with the Arduino IDE via the V-USB virtual serial port included in the Micronucleus bootloader
  • the Micronucleus driver, to upload the bootloader via micronucleus.exe (e.g., for bootloader upgrade or bootloader customization)
  • optionally, DigiCDC - Digispark USB CDC Serial Library, requiring appropriate Arduino library as well as the V-USB driver on the PC.

The firmware upload driver is installed via the Arduino IDE and the Micronucleus bootloader upgrade driver can be set-up via https://github.com/digistump/DigistumpArduino.

To install the Micronucleus driver, expand DigistumpArduino-master.zip into DigistumpArduino-master. Select tools directory; expand micronucleus-2.0a4-win.zip, Select micronucleus-2.0a4.

Then run:

Install Digistump Drivers.bat

Upload the latest version of the bootloader

Micronucleus is a bootloader designed for AVR ATtiny microcontrollers; its latest version offers significant improvements over V1.x; upgrading the bootloader allows taking advantage of the benefits described in the Micronucleus README: mainly a significantly compact size.

Download https://github.com/micronucleus/micronucleus and expand it to a local Windows directory.

At the time of writing, the latest bootloader is Micronucleus V2.03; its file is /firmware/releases/t85_default.hex

The command to flash the bootloader is the following: https://github.com/micronucleus/micronucleus/tree/master/commandline/builds/Windows/micronucleus.exe

Notice that t85_default.hex bootloader cannot be loaded directly to the Digispark board, as it is too big.

The allow loading the bootloader, it needs to be converted into a compressed version; we will generate it with name upgrade.hex. Check https://github.com/micronucleus/micronucleus/issues/22 for further notes.

To generate upgrade.hex with Windows, open a CMD at the micronucleus directory. Check that micronucleus.exe can be executed:

micronucleus /?

Connect the Digispark USB to the PC and check that the PC can recognize it by assigning a virtual USB port.

Check the version of the bootloader (notice that 1.6 means 1.06).

The old micronucleus version v1.11 already comes with the upgrade releases for this version (and with the related upgrade directory):

  • micronucleus-1.11-entry_ext_reset.hex
  • micronucleus-1.11-entry_jumper_pb0.hex
  • micronucleus-1.11-entry_jumper_pb0.hex
  • micronucleus-1.11-ledpb1.hex
  • micronucleus-1.11.hex

The file bootloaderconfig.h describes these upgrade bootloader releases.

To load them:

micronucleus --run *<filename>*.hex

The default bootloader version 1.11 can be uploaded with the command

micronucleus --run micronucleus-1.11.hex

All option files are in the same directory. The default micronucleus-1.11.hex bootloader starts the device in firmware update option and keeps it waiting for software upload for 6 seconds, then runs the software. An interesting option allows initializing the Digispark to immediately run the software without the 6 seconds delay; the software upload can be accomplished via the reset pin:

micronucleus --run micronucleus-1.11-entry-ext-reset-upgrade.hex

Coming back to the last bootloader version, as mentioned, the reference file t85_default.hex (micronucleus-master/firmware/releases/t85_default.hex) cannot be directly uploaded as too big. Example:

micronucleus --run t85_default.hex

It should fail with an error like: "Program file is 1602 bytes too big for the bootloader!"

In order to compress the bootloader to allow flashing the microcontroller, it has to be recompiled.

Requirements: Cygwin, Ruby.

The upgrade directory is needed. As it is not included in the latest version, it needs to be downloaded from the v.1.11 old release:

Download https://github.com/micronucleus/micronucleus/tree/v1.11

Expand the zip file, select the upgrade directory and copy it to micronucleus-master\upgrade

Also copy the ruby directory to micronucleus-master\ruby

Keep all directory tree.

Then open a CMD to micronucleus-master:

cd upgrade
gem install libusb
ruby generate-data.rb t85_default.hex

It generates bootloader_data.c in the upgrade directory.

To compile it, the AVR compiler needs to be set in the path; it generally can be found at C:\Program Files\Arduino\hardware\tools\avr\bin.

Open a Cygwin Bash to the upgrade directory

export PATH=$PATH:/cygdrive/c/Program\ Files/Arduino/hardware/tools/avr/bin
make clean
make

Return to the CMD; run the following:

micronucleus --run upgrade.hex

The bootloader upgrade should succeed.

Check now the bootloader version:

micronucleus -?

Device should have bootloader version 2.3 (or newer).

How to also generate the bootloader hex file

Open cygwin to the firmware directory (it should include a Makefile and the configuration directory). Run

export PATH=$PATH:/cygdrive/c/Program\ Files/Arduino/hardware/tools/avr/bin

Note: to revise the configuration: firmware\configuration\t85_default\bootloaderconfig.h. This file includes comment notes describing the allowed configuration settings. For instance, edit:

#define AUTO_EXIT_NO_USB_MS    1

Then run the following:

make clean
make CONFIG=t85_default

A file named main.hex is generated

Move main.hex to the upgrade directory

You can then run ruby generate-data.rb main.hex; this can be also achieved with Windows: with a CMD run the following:

ruby generate-data.rb main.hex

Return to the cygwin shell; run the following:

cd ../upgrade
make clean
make

return to the CMD; run the following:

micronucleus --run upgrade.hex

Example:

..\commandline\builds\Windows\micronucleus.exe --run upgrade.hex

Notice that the Arduino IDE configuration has to be modified in order to change the maximum sketch size allowed to be upload.

Changing the maximum sketch size of the Arduino IDE

Open a CMD

Insert the Digispark to the PC USB. Run the following:

micronucleus /?

Check the max size of the executable program.

Open the directory mentioned in the last message of the Preferences form (Arduino IDE, File, Preferences): “More preferences can be edited directly in the file”. If clicking the file name, the related installation directory is opened. Move to the packages\digistump\hardware\avr<version> subdirectory.

Edit boards.txt with a UNIX editor. Set digispark-tiny.upload.maximum_size to the value returned by micronucleus /?. Example:

digispark-tiny.upload.maximum_size=6520

Configuring and generating the v1.11 micronucleus hex bootloader

Similarily to the latest bootloader version previously described, also version v1.11 can be configured and generated. Even if the hex files are ready available at the firmware/releases directory, the following procedure describes how to produce them.

Download micronucleus version v1.11.

It comes with the upgrade directory.

Edit micronucleus/firmware/bootloaderconfig.h

For instance, set:

#define AUTO_EXIT_NO_USB_MS    1

Open a cygwin bash to the micronucleus/firmware directory (it should include a Makefile)

export PATH=$PATH:/cygdrive/c/Program\ Files/Arduino/hardware/tools/avr/bin
make clean
make

A file named main.hex is generated

Move main.hex to the upgrade directory

You can then run ruby generate-data.rb main.hex; this can be also achieved with Windows: with a Windows CMD run the following:

cd upgrade
gem install libusb
ruby generate-data.rb main.hex

Return to the cygwin shell; run the following:

cd ../upgrade
make clean
make

return to the CMD; run the following:

micronucleus --run upgrade.hex

Example:

..\commandline\builds\Windows\micronucleus.exe --run upgrade.hex

The bootloader is flashed.

Schematic Diagram

A valid schematic diagram for the Digispark module is at this url: http://www.electroschematics.com/wp-content/uploads/2015/08/Circuit-Diagram-of-the-ATtiny85-USB-Mini-Development-Board.png

The LED between the USB and 5V shall have the cathode (LED +) connected to +5V and the anode (LED –) connected with USB V+. The Digispark schematic diagram incorrectly inverts this LED.

Pinout:

Digispark board power consumption

The power consumption of the Digispark board is influenced by:

  • the microcontroller (which support SLEEP mode);
  • the power LED (which can be removed);
  • the 78L05 linear regulator (which can be removed);
  • the LED connected to PB1 (only when the related output is set to HIGH);
  • the USB circuit including Zener LED (current permanently flowing between +5V, the D- pull-up resistor and the zener diode). This can be eliminated by disconnecting the D- pull-up resistor from +5V and connecting it to USB V+ instead; in this case, the power consumption only happens when the USB is connected.

The Digispark ATtiny85 device has to be modified as follows to save power:

  • remove the power led;
  • remove the linear voltage regulator of the Digispark (LM78L05 has a quiescent current of about 2.5 to 3 milliamperes) and in case substitute it with diodes in series (or zener) to reduce the voltage to about 3.2V (this contributes to reduce quiescent current to the minimum);
  • enable D- pull-up only when the USB is connected (it can be achieved by moving the pull-up resistor connection to take the positive voltage directly from the USB +5V and not from the Digispark internal +5V, which is after the schottky diode separating USB from the internal +5V; this configuration reduces power consumption by avoiding current flow throgh the D- zener during normal operation and in parallel preserving the D- pull-up when USB is connected, which is anyway needed by the USB hw interface);

Notice that after the modification of the D- pull-up resistor, the Digispark bootloader has to be flashed with "#define ENTRYMODE ENTRY_EXT_RESET" in micronucleus-master/firmware/configuration/t85_default/bootloaderconfig.h; then use the Digispark reset pin to enable the firmware upload. Setting ENTRYMODE to ENTRY_EXT_RESET avoids to hang the Digispark if D- is not pulled-up. A micro-switch between D5 and ground is appropriate, also pulling up D5 to VCC with a 22kohm resistor.

Notes on DigiCDC

DigiSpark (and ATtiny85) does not support a native serial monitor and needs a software implementation of the USB serial port to allow debugging data through the serial port. DigiCDC is based on the V-USB project; it uses a software interrupt and needs no other timers or interrupts to be enabled in parallel to correctly work. Besides, to avoid USB problems, DigiCDC shall be started just after the microcontroller boot and shall be kept always active. It is slow and if you don't call a DigiCDC function (write, print, read, available, etc) every 10ms or less then you must throw in some DigiCDC.refresh(); for the USB to keep alive - also replace your delays - ie. delay(100); with DigiCDC.delays ie. DigiCDC.delay(100);

Note on sleep mode

The following sample allows calling sleep() to enter sleep mode to the lowest power level. It puts the processor in quiescent mode and uses Pin 0 to resume from sleep.

#include <avr/sleep.h>
#include <avr/interrupt.h>

void sleep() {
#if defined (__AVR_ATtiny85__)
  cli();                                  // Disable interrupts
  GIMSK |= _BV(PCIE);                     // Enable Pin Change Interrupts
  PCMSK |= _BV(PCINT0);                   // Use Pin 0 interrupt pin to resume from sleep
 
  ADCSRA &= ~_BV(ADEN);                   // ADC off
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);    // set sleep to the lowest power mode

  sleep_enable();                         // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
  sei();                                  // Enable interrupts
  sleep_cpu();                            // sleep

  cli();                                  // Disable interrupts
  PCMSK &= ~_BV(PCINT0);                  // Turn off interrupt on pin 0
  GIMSK &= ~_BV(PCIE);                    // Turn off Pin Change Interrupts

  sleep_disable();                        // Clear SE bit
  ADCSRA &= ~_BV(ADEN);                   // ADC off (if needed...)

  sei();                                  // Enable interrupts
#endif
} // sleep

#if defined (__AVR_ATtiny85__)
ISR(PCINT0_vect) {
// This is called when the interrupt occurs
  TIMER_RESET;              // reset irrecv timer
  irrecv.resume();          // reset irrecv counter
  previousTime = millis();  // reset sleep timer
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment