-->

Saturday, March 30, 2013

We Didn't Start the Fire

...but a Digispark can sure light things up! A Digispark is a small, cheap Arduino board -- I bought a couple for $8.95 each -- which allows you to leave them in a project without feeling guilty about it or worrying too much about the cost. They are not as capable as a full-blown Arduino, but for smaller (and some medium sized) projects, they are powerful enough, and won't leave you with unused pins on the board, or have you worrying about underutilization of your hardware. Digispark was originally a Kickstarter project that I learned about through my friend 'Dillo, who has a great blog over at Roadknight Labs. I missed out on the Kickstarter, but managed to pick some up after the fact. You can order them directly here.

Incidentally, there are many other really cool Kickstarter projects out there, such as the Parallela, which is a massively parallel single board computer system (can't wait to get my hands on one!), and the 3Doodler, which allows you to draw 3D objects using the same plastics and technology as 3D printers such as the MakerBot and RepRaps. Ones I also have my eye on are the APOC Mini Radiation Detector, and the PowerPot, which allows you to produce electricity while camping by cooking food or boiling water.

What I decided to build with a Digispark was.. this glowy thing.





The basic components are a translucent light cover and a a shadow box I got at a thrift store...



...a Digispark Arduino board (which is about the size of a quarter), along with the Digispark RGB LED shield (which are shown here stacked together)...



...and a couple of switches.



The switch on the left is a numeric up/down switch, which goes from 0 to 6. The wiring on it is pretty simple -- there's a common terminal, and then three others, which indicate the current selection in binary. I couldn't find a datasheet for the switch, but a  little experimentation determined which was the common terminal.

The numeric switch with the leads attached.

From there, I wired up the Digispark directly to a USB cable. When it is being programmed, the device will be plugged directly into a computer, and under normal operation, it will be plugged into a 5v USB power supply.

The Digispark with a USB cable wired directly to it.
I found information for the wiring layout for USB here on Wikipedia. Wikipedia rocks! 

Here is a video of the digispark, wired up to the USB cable, running the demo RGB Arduino program.



From here, I soldered up the numeric switch to the Digispark board, using pins not used by the RGB shield. I used knots to indicate which line was which. The one with no knots is the common line (note -- in the picture, it is soldered to the wrong pin. I had it soldered to +5v, but it should have been connected to ground)



From here, the power switch got soldered to the board...

Adding the wires to the switch. Shrink tubing leaves everything so much better looking when you are done!


...and since the Digispark throws off so much of its power as waste heat, I decided to wire a 1K resistor in serial to the board. It works -- the board powers up with it and is still programmable, so I kept it. Here's hoping it will be a little more efficient!

I am really horrible at remembering how to decode resistor values, so, here's a nifty online calculator!

Putting it all together was fairly straightforward. One thing that needed to be considered, however, was that the Digispark board produces quite a lot of heat, and I needed to isolate it from the wood box I was mounting it on. For this, I just used a bolt and some zip ties. Holes were also cut for the two switches (and a power light), and hot glued from the back to keep them firmly in place.



From here, I decided I would rather have the base black than wood, so I used some chalkboard paint to spray paint it.



In order to not have wires dangling everywhere, I decided to close the box. For this, I repurposed a couple of coke cans...


...sealed up the seam with hot glue, and then added some rubber feet that I got at Vetco Electronics in Bellevue, WA to finish things off. (Vetco is an awesome store, by the way. Somewhat akin to Weirdstuff Warehouse in the San Francisco bay area -- both great places to go look through surplus electronics and find parts!)




And here's what we have so far, shown without the light cover:



Digisparks use a special version of the Arduino 1.x software, with addins for the board, such as the device programmer and libraries. You can download it from the wiki here, as well as find other good information about the board. One quirk I found that was, when reprogramming the board, you have to unplug the board and then plug it back in, at which point the Arduino plugin recognizes it and starts the programming process. Not a big deal, but not immediately obvious, either, and worth pointing out to anyone just getting started.

The code I wrote for it was a variation on the RGB Demo program, with other variations possible depending on the setting of the numeric switch. For instance, setting "0" is all white and as bright as it goes, "1" flickers between blue and green very rapidly, "2" is purple, and "3" is the default demo program, and so on. And the cool thing is, if I get tired of those options, it's very easy to reprogram it.

Here's the program I am currently running on it. Note that it's easily expandable using the switch() statement.




#include <DigisparkRGB.h>
#include <DigiUSB.h>

#define PIN1 5
#define PIN2 4
#define PIN3 3

byte RED = 0;
byte BLUE = 2;
byte GREEN = 1;
byte COLORS[] = {
  RED, 
  BLUE, 
  GREEN
};

// the setup routine runs once when you press reset:
void setup()  
{ 

  pinMode(PIN1, INPUT); 
  pinMode(PIN2, INPUT); 
  pinMode(PIN3, INPUT);

  digitalWrite(PIN1, HIGH);
  digitalWrite(PIN2, HIGH);
  digitalWrite(PIN3, HIGH);

  DigisparkRGBBegin();
  //DigiUSB.begin();
} 


void loop ()
{
  static bool flicker = false;
  byte val = 0;

  int pin1 = digitalRead(PIN1);
  int pin2 = digitalRead(PIN2);
  int pin3 = digitalRead(PIN3);

  flicker ^= true;

  //val = ((pin1==HIGH) ? 1 : 0) | ((pin2==HIGH) ? 2 : 0) | ((pin3==HIGH) ? 4 : 0);
  val = (pin1==HIGH ? 1 : 0)| ((pin2==HIGH) ? 2 : 0);

  //DigiUSB.println(val);
  //return;

  switch(val)
  {
  case 0:
    {
      DigisparkRGB(RED, 255);
      DigisparkRGB(GREEN, 255);
      DigisparkRGB(BLUE, 255);

      break;
    }

  case 1:
    {
      if(flicker) {
        DigisparkRGB(RED, 0);
        DigisparkRGB(GREEN, 255);
        DigisparkRGB(BLUE, 0);
      } 
      else 
      {
        DigisparkRGB(RED, 0);
        DigisparkRGB(GREEN, 255);
        DigisparkRGB(BLUE, 255);
      }

      break;
    }


  case 2:
    {
      DigisparkRGB(RED, 255);
      DigisparkRGB(GREEN, 0);
      DigisparkRGB(BLUE, 255);

      break;
    }
    
  case 3:
    {
      //DigisparkRGB(RED, 0);
      //DigisparkRGB(GREEN, 0);
      //DigisparkRGB(BLUE, 0);
      //DigisparkRGB(RED, 255);
      fade();

      break;
    }


  case 4:
    {
      if(flicker) 
      {
        DigisparkRGB(RED, 255);
        DigisparkRGB(GREEN, 255);
        DigisparkRGB(BLUE, 255);
      }
      else
      {
        DigisparkRGB(RED, 0);
        DigisparkRGB(GREEN, 0);
        DigisparkRGB(BLUE, 0);
      }
      break;
    }

  default: 
    //fade();
    break;
  }

}

void fade() 
{
  //direction: up = true, down = false
  static boolean dir = true;
  static int i = 0;

  //while(1)
  {
    fade(COLORS[i%3], dir);
    i++;
    dir = !dir;
  }
}

void fade(byte Led, boolean dir)
{
  int i;

  //if fading up
  if (dir)
  {
    for (i = 0; i < 256; i++) 
    {
      DigisparkRGB(Led, i);
      DigisparkRGBDelay(5);//1);
    }
  }
  else
  {
    for (i = 255; i >= 0; i--) 
    {
      DigisparkRGB(Led, i);
      DigisparkRGBDelay(5);//1);
    }
  }
}






And here it is, all put together.

Chalk heart drawing courtesy of my wonderful and artistic girlfriend, Charlotte.


So the veredict? Digisparks are awesome! I recommend picking up a few and playing around with them at your earliest convenience.

Here's a video of the glowy thing, running it through a couple of settings:


And another of it with the lights off (neither of the videos really do it complete justice)



And speaking of pretty lights  and glowing at night, here's a song by a group called "Pretty Lights", called "Gold Coast Hustle". It's good music to watch the glowy thing by.


1 comment:

  1. Very cool. I might use Digistump in my classes! It's nice to start small!

    ReplyDelete