Friday Fun - Neopixel Camera Light
If you've been reading this blog for a while then you will know of my love for WS2811 individually addressable, controllable LEDs...Neopixels and for the ATTiny85 a stupidly cheap Arduino compatible board.
I've not done a hack with them for some time read that as since Christmas and I wanted to use them to light a scene for a photo. The goal being to create a rainbow "glow" across the subject / object that provides a surreal light to the shot (#sysadminGETTINGARTY). So I had a dig around my bits box and I found the following bits
- A WS2811 16 pixel ring
- An ATtiny85 soldered and ready for use
- 3 x Female to male jumper wires
- Soldering kit
- Wire strippers
- Camera / Phone
- Arduino app installed on your computer
Lets build the hardware
Note that the ATTiny85 used in this diagram is not the one I used, but it is all Fritzing had, the connections are still correct!
The circuit for our project is remarkably simple.
First we need to snip our 3 female to male jumper wires in half, put half away for a future project. The remaining wires we shall strip and tin using our soldering iron. Next we need to tin the VCC / 5V, IN and GND pads of our WS2811 neopixel. Lastly we need to join the tinned wires to the tinned pads on the neopixel. The colour coding used in the images is as follows
RED = 5V / VCC
BLUE = GND
WHITE = IN
Now connect the female headers to the ATTiny85 as so
RED = 5V
BLUE = GND
WHITE = P2
Hardware done! On to the software
Writing the code
The porg looks deep into your soul!
The code for the ATTiny85 is written using the Arduino IDE, so open the application.
Installing the Neopixel library
Once open, we next need to install the Adafruit Neopixel library. So click on Sketch >> Include Libraries >> Manage Libraries and in the new dialog box, there will be a search box in the top right, enter the word "neopixel" in there and it will search for that word.
Once you see the Adafruit Neopixel by Adafruit entry click on it and then Install. This will install the library and the example sketches for the Adafruit Neopiel library!
Installing the ATTiny85 board
The ATTiny85 is not supported by default with the Arduino IDE, but we can easily fix that.
To do that we need to go to File >> Preferences and look for the line Additional Boards Managers URLs: by default this should be empty. Click on the icon to the right of the text to open a new window. In this new window, copy/paste this URL and click Ok.
So now we can add the ATTiny85 to our list of boards. Go to Tools >> Board >> Boards Manager and wait for a few seconds until the list of boards has been updated. Once you have control, in the top right of the screen use the search box and search for digispark and then install the board. The final step before we start coding is to tell Arduino what board we are using, so go back to Tools >> Board and select Digispark (Default - 16.5mhz). Then go to Tools >> Port and the port for your board should be there and selectable.
It's coding time!
There is actually very little coding to this project, as we shall be using one of Adafruit's Neopixel library sketches, specifically Strand Test, used to...well..test the strand of LEDs. To open the file click on File >> Examples >> Adafruit Neopixel and then look for strandtest, open that example.
The strandtest file is 134 lines long, and to be honest it is damn scary when you first see it! But before we start delving into the code, lets first test that our neopixels are working. We need to make two small changes to make the code work with the ATTiny85, and they are...
#define PIN 6
#define PIN 2
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);
Click on the Upload button (An arrow pointing to the right, just under the menu) and via the console at the bottom of the Arduino IDE window you will be prompted to connect your ATTiny85 to the USB port. You have 60 seconds to do this, so don't worry, take your time. Once connected, the code will flash to the ATTiny85 and then you will be told that the flashing is completed, then after a few seconds the neopixel ring will come to life, changing colours, animations etc.
Be careful with neopixels, they are really bright and can cause headaches due to eye strain!
With the test complete, lets edit the strandtest code so that we only have a rainbow cycle running.
For this we need to edit the code between lines 35 and 49..in fact we need to edit only 3 of those lines!
colorWipe(strip.Color(255, 0, 0), 50); // Red
/* colorWipe(strip.Color(255, 0, 0), 50); // Red
Insert a new line after 46 and enter the following
What we have just created is a multi line comment, anything between the /* and */ is ignored by Arduino.
This is a single line comment, again the Arduino ignores this line of text.
Click on File >> Save and save your code before moving on. You cannot overwrite the strandtest file and the Arduino IDE will force you to create a new file.
That's it, upload the code to your ATTiny85!
Upload the code just as we did with the test earlier. But now instead of many different animations, the neopixel ring will just cycle through a soothing rainbow of colours, which we can now use to take interesting photographs of things!
But how can I stick it to my camera?
...blutack / modelling clay / double sided tape
I used blutack to attach the ATtiny85 and the neopixel ring to my phone case, but if you have a spare phone case, you could hot glue the kit to the case and just remove it when not needed.
But how can I power it?
I would reccomend an external USB power supply to power the ATTiny85 and the neopixels, but if you are brave / stupid then you could look to the bottom of this post....
So there we have it, a relatively simple, fun project that we can use to create arty shots for our blogs / Instagram / portrait photos.
Bonus Content! Powering the project from our phone!
Well..this is interesting...
Before we proceed to this next step, it is at your own risk...if you break your phone/tablet/micro USB device then sorry, you have been warned!
So I have a micro USB to full USB adapter (Aliexpress...50p I think) and I have one of the Digispark ATTiny85 boards with built in full USB. Usually these are not my favourite, I prefer those with the micro USB port, but they were cheap and usable. I thought
I wonder if a phone could power the project?
So I dug out an old phone and tested...and it worked... So I connected it to my main phone and it worked. This could be the start of an interesting add on!