A Spark of Life: More DigiSpark fun

WP_20151019_22_42_59_Pro__highresI recently wrote about the cool little DigiSpark ATTiny 85 controller board. This little USB wonder, like the AdaFruit Trinket, is based on the ATTiny85 Microcontroller and has limited I/O, memory and is cheap, very cheap.

One thing it is not short on, however, is uses. This little thing can be used for a variety of things, including games and control applications.  For any use, though, you will need some kind of input and some kind of output.  In a previous post, I postulated about porting my ‘Battlestar Galactica’ game to the DigiSpark from the Trinket.  So, here are the results of that endeavor and some other things as well.

For the backstory of the game and why I wrote it, check out this post. I’ll wait.

Read it?  Cool.WP_20151019_22_44_34_Pro__highres

Ok, so there are few differences between the Trinket and the DigiSpark, other than size.  The way you program it is very similar and the pins are nearly identical.  The way you talk to things, is, however, a little different.

The Trinket needed SoftwareSerial. We don’t here, we just use DigitalRead and DigitalWrite and Serial.write to read the button and write to the screen.

The display is a 16×2 Serial LCD from Parallax.  The button is actually a ‘controller’ I built a couple of years ago for my game console. I did not use it for that, opting for the Wii Nunchuck instead.  So, the controller has three buttons that would be used for left, right and ‘fire’ or action.  Here, they just return a 0 if pressed and a 1 when ‘open’.  The code periodically polls the controller for a press and then acts on the press to evaluate a hit or miss.


Now, one of the problems I had was the lack of GROUND pins. So, I made a ground bus, a four pin strip with a wire connecting all four pins. I then connected this to ground on the DS and the button and the LCD as well are connected to this bus. I have one free ground pin.  Well, what I did not realize was that you can use any pin for ground.  While I did not change my connections, I find this pretty cool.

TIP! To use a free pin as ground:

(From the wiki)

You can use an I/O pin as a ground/+5V if you have some left and the current that flow in it is low (a few milliamps if you want the pin to stay around 0V/+5V. If you don’t care about the exact voltage you can go up to 40mA per pin according to the datasheet but it’s good to keep a safety margin).

Configure the pin as an output and set its value to either LOW or HIGH (for ground or +5V

#define button 0
#define gnd 2
void setup() {                
  pinMode (button, INPUT);
  digitalWrite (button, HIGH); // enable pullup
  pinMode(gnd, OUTPUT);
  digitalWrite (gnd, LOW); // use this pin as a ground

For much more information and tips, go to the wiki, located here.

The game code is below.

Come back for more on this cool little device.  Since it is capable of serial i/o, I’m thinking of using the Bluetooth Serial module and connecting the DS to one of my HalfByte Consoles running the graphical serial software for video display from the DS.


Game Code:

#include <TinyPinChange.h>

 // globals
 int  cylonpos;
 int  oldcylonpos;
 int  cyDirection;
 int  button;
 int  posStart;
 int  lin2Start;
 int  bsgpos;
 // special characters
 int  bsg;
 int  cylon;
 // total number of torpedos the ship has left
 int  tShots=10;
 // the setup routine runs once when you press reset:
 void waitforbutton();
 void moveCylon();
 void setup() {  // initialize the digital pin as an output.  
   //define the ship character
   // baddies

   Serial.write(uint8_t(0x0));//to handle a goofy issue with '0'
 // the button is on pin 0  
 // the initial direction is right
 } // the loop routine runs over and over again forever:
 void loop() {
  int score;
  Serial.write(12); // clear the screen and show the instructions
  Serial.println("Shoot the Cylon\n");
  Serial.println("press btn strt");
  // wait for the button
  // define somethings before we start the game loop
  lin2Start=148; // the baddies appear on line 1
  posStart=128;  // the good guys on line 2
  randomSeed(analogRead(0)); // gen a random seed
  bsgpos = 8;  // our ship starts in the middle of the sector
  cylonpos=random(0,15); // baddies warp in at a random spot
  Serial.write(12);  // clear screen
  Serial.write(17);  // turn on backlight so we can see
  Serial.write(posStart + cylonpos);  // uh oh...the Cylon appeared!
  Serial.write(bsg); // and here we are
  // begin the game 
    /* Game loop */
    int button;
    Serial.write(22); // kill the cursor
    button=digitalRead(0);  // check the button
    if(button==0){ // torpedo fired?
      if(cylonpos==bsgpos){ // Hit?
        Serial.write(132); // center 'Hit' on screen
        Serial.print("** HIT **");
        score ++;  // update score
        Serial.write(213); // noise (C at half note, 3rd scale)
        Serial.write(215); // play the note
        if(score==10){  // did we win?
           Serial.write(12); // clear screen
           Serial.write(128);// start at beginning
           Serial.print("You have saved\nthe Galactica.");
           loop();  // start game again
        delay(3000); // did not win yet
        Serial.write(12); // clear screen
        Serial.write(lin2Start); // move cursor to beginning of line
        Serial.write("Score:");  // put up score
        Serial.write(lin2Start+bsgpos); // Since we shot the Cylon, we have to leave the sector, so...
        Serial.write(0x20); // go into hyperdrive and erase the BSG
        bsgpos=random(9,15);  // calculate new position
        Serial.write(lin2Start+bsgpos); //...and, we are here
        cylonpos=random(0,15); // Damn, they found us!
      tShots --; // decrement our torpedo inventory
      if(tShots<1){ // out of ammo?
        Serial.write(12);  // clear screen
        Serial.write(posStart); // start at beginning of line
        Serial.print("The Cylons win.\n");
        int count=1;
        // show animation of our ship exploding
        while (count<10){
        loop(); // Play again
void moveCylon(){
  // sneaky Cylons...they show up anywhere!
  if (random(0,6)>3){
      //Cylon warped
  cylonpos += cyDirection; // figure out which way to move
  if (cylonpos>15){
  if (cylonpos<0){
  // erase from old spot

void waitforbutton(){
  int button;
  while(button==1){     //wait for button press

The Mighty ATTiny85 and DigiSpark

Microcontrollers are lots of fun to play with as well as to build useful things.  They come in all sizes, shapes and varieties.  I am always, however, intrigued with the tiny controllers, like AdaFruit’s Trinket.  Well, the latest one I’ve started to tackle is the DigiSpark.  The DigiSpark is small and comes in several types. I am using the USB flavor. This little guy is on a very small board, about the size of a nickel, and is, itself, a USB dongle.

WP_20150928_23_59_09_Pro (2)

It features six I/O lines, plus power and ground pins.  This thing is a little bit more versatile than the original Trinket, but not much more. The USB version has the added advantage of being a USB dongle itself, meaning you can build a project that plugs into a USB port and act as an external controller. You don’t need USB, once you have programmed it, the power and ground pins can be used to power the device.

The nice thing about the DigiSpark is that you can use the Arduino IDE and SOME of the libraries to program the device.  There is a nice tutorial on setting up a newer release of the IDE and add DigiSpark support.

In a nutshell:

  1. Start the IDE (Install 1.6.x if you don’t already have it installed)
  2. Click FILE, then PREFERENCES
  3. in the Additional Boards Manager URL’s box, enter: http://digistump.com/package_digistump_index.json
  4. Click OK
  5. Click Tools, then BOARDS
  6. Select Board Manager and then Contributed
  7. In the dialog box that pops up, select DIGISTUMP AVR BOARDS
  8. Click Install
  9. Close the Board Manager
  10. You should now see the DigiSpark boards in the IDE

The thing that tripped me up was uploading a sketch.  I didn’t realize that you don’t need to have the device plugged in prior to uploading (you are told this, but it didn’t sink in.)  Once I unplugged the device, clicked upload and THEN plugged it in, it worked like a charm.

So, now I have to figure out what I want to do with this cool little device. 

But, wait…I still have not really said anything about the device, what it has on it, etc.

It is based on the ATTiny 85 and features six I/O pins:

Pin outs:

    • All pins can be used as Digital I/O

    • Pin 0 → I2C SDA, PWM (LED on Model B)
    • Pin 1 → PWM (LED on Model A)
    • Pin 2 → I2C SCK, Analog
    • Pin 3 → Analog In (also used for USB+ when USB is in use)
    • Pin 4 → PWM, Analog (also used for USB- when USB is in use)
    • Pin 5 → Analog In

The ATTiny 85 specs:

  • 8 Bit Data Bus
  • 20 MHz Max Clock Frequency ( w/ external crystal )
  • 8 kB Program Memory Size
  • 2.7 V to 5.5 V Operating Supply Voltage
  • 6 I/O Pins
  • 512 bytes of RAM


Two things you cannot do from the IDE: burn the bootloader and use the serial monitor. Some libraries will work, most will not.  There is no short circuit or reverse polarity protection, so be careful or you will certainly destroy the board, and, if connected via USB (which it really should not be if using external power) the computer could be damaged as well.


The IDE, as with other Arduino’s, displays the compiler and upload progress in the text area below the coding window.  Several rather bothersome messages can show, but, not to worry, it is normal. See my sample output below.

Sketch uses 668 bytes (11%) of program storage space. Maximum is 6,012 bytes.
Global variables use 9 bytes of dynamic memory.
Running Digispark Uploader…
Plug in device now… (will timeout in 60 seconds)
> Please plug in the device …
> Press CTRL+C to terminate the program.
> Device is found!
connecting: 16% complete
connecting: 22% complete
connecting: 28% complete
connecting: 33% complete
> Device has firmware version 1.6
> Available space for user applications: 6012 bytes
> Suggested sleep time between sending pages: 8ms
> Whole page count: 94  page size: 64
> Erase function sleep duration: 752ms
parsing: 50% complete
> Erasing the memory …
erasing: 55% complete
erasing: 60% complete
erasing: 65% complete
>> Eep! Connection to device lost during erase! Not to worry
>> This happens on some computers – reconnecting…
>> Reconnected! Continuing upload sequence…
> Starting to upload …
writing: 70% complete
writing: 75% complete
writing: 80% complete
> Starting the user app …
running: 100% complete
>> Micronucleus done. Thank you!

Micronucleus is the name of the bootloader.  The disconnect message was a little disturbing, but not a problem.

Maybe I’ll adapt my Battlestar Galactica LCD game to this thing…

Here is a link to the Trinket posts on the blog. It is very similar to the DigiSpark.

digiStump is the maker of the device and they have other products as well. Take a gander around their site. There is a nice Wiki page and forums.

Finally, you can purchase one from Amazon for $4.88 each.  Of course, you can support digiStump by purchasing the device from them, they sell it for $8.95, a little higher than Amazon, but you will be supporting the makers and encourage further development of the product.