Digispark – the convenient ATtiny85 alternative

In my last post I described how to upload your sketches to an ATtiny85 (or 84, 45, 44, 25, 24). To do this, you have to set up your Arduino as a programmer or buy another programmer, such as the USBtinyISP. A more convenient alternative in some respects, which I would like to introduce here, is the Digispark, which originates from the company Digistump. 

This is a module that is based on an ATtiny85 and that can be programmed directly via the USB port. So neither SPI wiring is necessary for uploading nor a programmer. Since the power supply can also be provided via the USB port, the circuits become very clear.

Another advantage of the Digispark over the ATtiny85 is that it masters I2C with the Wire library. Then it should be mentioned that the (original!) Digispark offers six I/O pins. The ATtiny85 has only five. More on that below. 

In terms of price, there is no big difference to the ATtiny85. Depending on the source and the quantity of purchase, you have to pay between two and four, sometimes five euros.

Disadvantages of the Digispark are the larger space requirement and a slightly smaller storage for sketches. Of the 8k flash memory, about 2k are used for the bootloader. In addition, it requires a very long boot time of several seconds, which is ages in the world of microcontrollers.

The Digispark is very well described here on the Digistump pages. So what is the point of this post? On the one hand it shall serve as a step-by-step introduction and overview, on the other hand I would like to share my very positive experience with this mini-board. In addition, there is some more specific information on the various clones, which by its very nature is not documented by the manufacturer.

Pinout of the Digispark

The Digispark has six programmable pins, named as P0 to P5. In addition to their role as digital inputs and outputs, the pins have the following additional functions:
 
  • P0: I2C SDA, PWM (“without Rev:” connected to on-board LED)
  • P1: PWM (Rev2, 3, 4: connected to on-board LED)
  • P2: I2C SCK, analog In
  • P3: analog in, USB+
  • P4: PWM, analog in, USB
  • P5: analog in (this pin delivers 3V if it is HIGH!)

This is very similar to the ATtiny85 Pinout, which is not surprising, since the Digispark is based on this one.

For comparison: pinout of the ATtiny85
For comparison: pinout of the ATtiny85

Different versions of the Digispark

There are different versions of the Digispark. Revision 2, 3 and 4 have a corresponding marking on the USB connector (revx). These versions have the on-board LED connected to P1, i.e. it lights up when P1 is HIGH. For the boards without revision number, P0 is connected to the on-board LED. If you have this version, you need to desolder the LED or cut the connection to the LED if you want to work with I2C. More details can be found here

According to Digistump, revision 3 is a fake, which should not be bought for this reason, even though it works fine. On the other hand, Revision 3 is so widespread that you hardly get another one. And so I bought this version myself. It has only one drawback: P5 is configured as a reset pin in this model and therefore cannot be easily used as an I/O pin. Try it if you have a “rev3”. Start a sketch and connect P5 briefly to GND – the Digispark restarts. Accordingly, the blink sketch below will not work with P5. In the last paragraph of the article I explain how to make the P5 an I/O pin. 

To complete the confusion, there are also versions without revision number that are not designed by Digistump. I also had such a board, and it worked like the “rev3” model. 

Setting up the Arduino IDE for Digispark

Even if working with the Digispark is very easy, you have to make some preparations first. But that’s done in a few minutes. I assume that you are using an Arduino IDE version > = 1.6.6. 

Step 1: Driver installation

You can download the driver installer here. If you use Windows (like me), then select the yellow marked link: 

Download Digispark driver from Github.

Unpack the zip file and run “Install Drivers.exe” for a 32-bit system or DPInst64.exe for a 64-bit system:

Driver selection in the zip file.

Step 2: Enter board manager URL

Then you go to the Arduino IDE and enter the board manager URL

“http://digistump.com/package_digistump_index.json”

in the preferences:

Enter board manager URL for the Digispark in the preferences.

Step 3: Install the Digistump package

Then go to

Tools – > Board: xxxx – > Boards Manager 

The way to the boards manager menu.

… and search for “digistump”, install the package and restart the Arduino IDE. That’s it.

Find and install the right package for Digispark.

Upload the first sketch

For testing I have chosen a simple blink sketch. It is no different from a blink sketch for Arduino boards. When using digitalWrite the pins are addressed according to their “P number”. That means: 0 is P0, 1 is P1, 2 is P2, etc. Sounds trivial at this point, but is different with analogRead (!). 

int ledPin = 1;

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

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

 

As far as the circuit is concerned, it can’t be easier. The LED is connected to GND and P1. The Digispark is connected to the PC via USB, but only after being requested when uploading (this becomes clearer soon).

Wiring for the blink sketch
Setup for blink sketch

In the Arduino IDE, choose “Digispark (Default – 16.5mhz)”. The variants with “no USB” and different clock also work. “Port” is grayed out. Don’t worry, that’s the way it is, even if the Digispark is plugged in. 

Choosing the right board.

If you upload the sketch now, you will receive the following message:

"Plug in device now" message

Now you connect the Digispark via USB and – if all goes well – you’ll get the following message:

Upload completed

The LED should now flash. If you have a Digispark model revision 2, 3 or 4, the on-board LED also flashes, because we have chosen P1 as the ledPin. 

Likewise, you can use the five other pins for this sketch, with the caveat that P5 delivers three volts instead of five volts or doesn’t work at all because you have revision 3 or other clones.

Slow booting

It is astonishing that the Digispark takes a good five seconds to start the sketch after switching on the power supply. An ATtiny85 with the standard bootloader is much faster. In my post about radio sockets and hand transmitters, I had presented a self-made hand transmitter based on an ATtiny85. The radio button was a pushbutton that powered the ATtiny85 and thus started the radio sketch. If I were to build the same with the Digispark, I would have to press my radio button for at least five seconds, which would not be particularly user-friendly. Admittedly, such an application is rather the exception, so in most cases this boot time should not be an issue. 

Analog Read with the Digispark

Analog Read is possible with pins 2, 3, 4 and 5. What is confusing, is that, unlike the Digital Read, the physical number of the pin does not match the analogRead number. This property was inherited by the Digispark from the ATtiny85, where it behaves the same. The assignment is:

  • P2: analogRead(1)
  • P3: analogRead(3)
  • P4: analogRead(2)
  • P5: analogRead(0)

In my small example, the voltage on P2 is checked. If it exceeds a certain value, an LED lights up on P4. The sketch looks like this: 

int ledPin = 4;
int analogInPin2 = 1; // analogRead(1) takes place at P2

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

void loop() {
  if(analogRead(analogInPin2)>100){
    digitalWrite(ledPin, HIGH);   
  }
  else{                     
    digitalWrite(ledPin, LOW);    
  }                     
}

 

And here is the simple circuit:

Circuit: Example for using analogRead with the Digispark

I2C with the Digispark

As already mentioned, the control of I2C devices works directly with the Wire library, which is an advantage over the ATtiny85. As an example, I chose the control of the port expander MCP23017. The yellow line at P0 is SDA, the green line at P2 is SCL. These are connected to the corresponding SDA / SCL pins at the MCP23017. The 4.7 kOhm pull-up resistors for the I2C lines are essential. 

Controlling the MCP23017 with the Digispark - an example of an I2C application
Controlling the MCP23017 with the Digispark – an example of an I2C application

For the sake of completeness, here is an example sketch (if you want to read more about the MCP23017, you find a post from me here).  

#define MCP_ADDRESS 0x20 // (A2/A1/A0 = LOW) 
#include <Wire.h>
#include <MCP23017.h> // my MCP23017 library
MCP23017 myMCP(MCP_ADDRESS,5); // 5: Reset - not needed here
int wT = 1000; // wT = waiting time

void setup(){ 
  Wire.begin();
  myMCP.Init();  
  myMCP.setPortMode(B11111111, A); 
  delay(wT);
}

void loop(){ 
  myMCP.setAllPins(A, ON); 
  delay(wT);
  myMCP.setAllPins(A, OFF);
  delay(wT);
  myMCP.setPin(0, A, ON); 
  delay(wT); 
  myMCP.setPin(4, A, ON);
  delay(wT);
  myMCP.setPin(7, A, ON); 
  delay(wT);
} 

 

Turn P5 into an I/O Pin

With the ATtiny85, only 5 pins can be used as I/O pins. Pin 1 (PB5, RESET) of the ATtiny85 can only be used as an I/O pin via detours. The problem is that you lose the reset function and the ATtiny85 can no longer be easily reprogrammed. You lock yourself out, so to speak. This can also be fixed, but only with programmers that have a high-voltage function. 

With Digispark you don’t need the reset function of P5, because the programming is done via USB. Thus, you have 6 I/O pins available. The developers of revision 3 and other clones, on the other hand, have retained the reset function of P5. If you only need 5 pins or maybe want to use the reset function, then leave everything as it is. If, on the other hand, you want to use all pins as I/O pins, then you can change that with this guide. 

For the activation of the P5 as an I/O pin or the deactivation of the reset function, the corresponding fuse-bit (RSTDISBL) must be set. I would like to present two ways in which this can be done.  

Option 1: With WinAVR and Arduino UNO

The beauty of this method is that most of you have an Arduino UNO (Nano, Pro Mini, etc. also work) and the required program WinAVR is free. 

First of all, download the program WinAVR here and install it. 

In the next step, you upload the ArduinoISP Sketch from the examples to the Arduino. This measure turns the Arduino into a programmer. 

Choose ArduinoISP from the examples
Choose ArduinoISP from the examples

Then you connect the Digispark to the Arduino as follows:

  • P0 — > 11
  • P1 — > 12
  • P2 — > 13
  • P5 — > 10

Finally, you connect GND to GND, 5V to 5V and place a 10 µF capacitor between the Arduino Reset and GND (note polarity: minus of the capacitor to GND).

Circuit for programming the Digispark using Arduino

Then you open a prompt window on the PC. Depending on whether you have set paths or not, you may need to switch to the directory in which you installed WinAVR. You enter the following, replacing COM14 with the right port (check in device manager or in the Arduino IDE):

avrdude -P COM14 -b 19200 -c avrisp -p attiny85 -n

You confirm with enter, enter the next line and confirm again with enter:

avrdude -P COM14 -b 19200 -p attiny85 -c avrisp  -U hfuse:w:0x5F:m 

If all goes well, you should get messages similar to this one:

Feedback from avrdude when setting the fuse bit in Digispark

Now you can upload the Blink Sketch from earlier with P5 as ledPin – now the LED flashes. Just a little less bright than on the other pins, as P5 only delivers 3 volts. 

Option 2: with Atmel Studio and suitable programmer

What you need

This method is less cryptic, but requires the installation of the rather large but also free software package Atmel Studio (currently version 7). In addition, you need a compatible programmer. Atmel Studio 7 can be downloaded here. If you want to get deeper into the programming of Atmel microcontrollers at register level and in “C”, you should deal with this program anyway. To change just one fuse bit, it’s a little exaggerated to install this powerful tool on your hard drive. In a later article I will go into more detail about Atmel Studio.

The programmers for Atmel Studio are relatively expensive. One of the cheaper ones is the USB ISP programmer from Tremex/Diamex, which you get for about 20 euros at e.g. here at Amazon. I tried it and it works absolutely smoothly with Atmel Studio. You don’t even need to install a driver when you’re using Windows 10.

Better, but more expensive are the AVR Dragon or the Atmel-ICE. The former is for 50-80 euros, the latter from 120 euros. Debugging is also possible with these two models. In addition, the Dragon masters HVSP (High Voltage Serial Programming) with which you can undo the procedure on the P5. The Atmel-ICE cannot do this, but is delivered in a nice housing. 

Tremex/Diamex's AVR Dragon, Atmel-ICE and USB ISP programmer
From left to right: AVR Dragon, Atmel-ICE, USB ISP programmer (Tremex/Diamex)

If you invest in a programmer, I recommend you to get a bread board adapter like this one here:

Breadboard adapter - useful for programming the Digispark

It makes your work easier and avoids errors in the circuit. You can buy such a part e.g. here or with 2 x 3 pin headers here.

Wiring

The connection scheme of the programmer and the Digispark looks like this:

Circuit for Digispark with programmer

Note that the AVR Dragon cannot power the Digispark. You need a separate power source. The low-cost USB ISP programmer, on the other hand, can do this, provided the dip switches 1 and 2 are on “ON”. 

Using Atmel Studio 7

If you are using a Dragon or Atmel-ICE, Atmel Studio should automatically detect it. You must first integrate the Tremex/Diamex USB ISP programmer. To do this, you can go to the menu

Tools — > Add Target… — > Select Tool 

Select ST500 and the port, then confirm with “Apply”.

In the menu bar you will find a small icon for “Device Programming”. Click on it.  

Menu of Atmel Studio 7

If the tool is not yet selected in the next window, select it here in the drop-down menu. As a device, you choose the ATtiny85. Interface is ISP. Then click on “Apply”.

Device programming window - selecting tool and device

You can then check the supply voltage and see if the correct signature is displayed. It should be 0x1E930B. Now go to the menu item “Fuses”.

Device Programming Window - more selections

Tick the box behind “HIGH.RSTDISBL”, click on “Program” and on “Continue” in the dialog window that pops up. If everything worked out you should see a message at the bottom telling you “Verify Registers …. OK”.

Device programming window - selection of fuse bits

P5 should now be operational as an I/O pin. 

If you would like to learn more about Atmel Studio 7, keep your eyes open – in one of the next posts I will provide an introduction. 

Leave a Reply

Your email address will not be published.