# Light Painting with Raspberry Pi

## Overview

_Light painting_ is an artistic photographic technique combining long exposure times with lights in motion. Traditionally these images have been hand-painted with a penlight…but more recently, affordable microcontrollers and addressable RGB LEDs have brought a new high-tech twist to the idea.  
  
A few such projects have been featured on tech blogs, most notably [the](https://sites.google.com/site/mechatronicsguy/lightscythe)_[LightScythe](https://sites.google.com/site/mechatronicsguy/lightscythe)_[by Gavin “The Mechatronics Guy,”](https://sites.google.com/site/mechatronicsguy/lightscythe) which uses an Arduino and a two meter bar of LEDs. The resulting photos, with their intense colors and mid-air suspended images, have been super popular, and rightfully so.  
![](https://cdn-learn.adafruit.com/assets/assets/000/001/619/medium800/raspberry_pi_lightscythe.jpg?1396774414)

Large, colorful images require a&nbsp;<u>lot</u>&nbsp;of memory…and that’s one area where we'll need more memory than an&nbsp;Arduino can provide. We had a hunch that the Raspberry Pi could make this process easier. But even we weren’t prepared for what a cakewalk this would be…

![](https://cdn-learn.adafruit.com/assets/assets/000/001/620/medium800/raspberry_pi_ID859top_LRG.jpg?1396774421)

# Light Painting with Raspberry Pi

## Hardware

Interfacing Adafruit’s [Digital Addressable RGB LED](http://learn.adafruit.com/digital-led-strip) strip (aka “LPD8806 strip”) to the Raspberry Pi is super simple, requiring just a few connections between the board, strip and a [DC power jack](http://adafruit.com/products/368).  
  
The board’s&nbsp;MOSI pin connects to the DI pin on the LED strip, and SCLK connects to the CI pin.

![](https://cdn-learn.adafruit.com/assets/assets/000/001/589/medium800/raspberry_pi_diagram.png?1396774138)

Instead of supplying power to the Raspberry Pi’s&nbsp;Micro USB connector, a&nbsp;5 Volt DC power supply is required because the LED strip&nbsp;draws&nbsp;significantly more current than the USB input can provide.&nbsp;A&nbsp;[2 Amp power supply](http://adafruit.com/products/276)&nbsp;is sufficient for a 1 meter LED strip, while our larger&nbsp;[10 Amp supply](http://adafruit.com/products/658)&nbsp;can power up to 5 meters of LED strip (plus the Raspberry Pi board, in both situations).

+5V and ground from the power supply&nbsp;connect to the 5V and GND pins on both the LED strip&nbsp;<u>and</u>&nbsp;the Raspberry Pi GPIO header.

In the above diagram, we’re directly connecting **3.3V** logic output from the Raspberry Pi to the **5V** logic input of the addressable LED strip. Strictly speaking, this is _not_ Good and Proper. Wildcards like actual power supply voltage (they’re never _precisely_ 5.0000V) or even temperature may contribute to whether this functions reliably. It did very well here, but your mileage may vary. If your LEDs _almost_ work but are glitchy, it’s time for a _logic level shifter_, [explained in this guide](https://learn.adafruit.com/neopixel-levelshifter) (which is about NeoPixels, but the principle is the same — just that we’ve got _two_ wires to level-shift here, rather than NeoPixels’ one).

![](https://cdn-learn.adafruit.com/assets/assets/000/001/669/medium800/raspberry_pi_ID658_LRG.jpg?1396774937)

An initial prototype was assembled using a [Pi Cobbler](http://adafruit.com/products/914) breakout kit. Because the finished project would be moving around a lot, and because breadboards aren’t the most robust thing, a 26-pin IDC cable was sacrificed for science to create a purpose-built cable between the Raspberry Pi GPIO header, LED strip and power supply. This is much more resilient to vibration and careless fingers.

![](https://cdn-learn.adafruit.com/assets/assets/000/001/621/medium800/raspberry_pi_cable.jpg?1396774427)

To make connections easy to make/break for setup and take down, we also used two JST 4-pin inline cables ([plug](http://www.adafruit.com/products/578)and [receptacle](http://www.adafruit.com/products/579))&nbsp;and&nbsp;&nbsp;to attach and detach the LED strip. The connectors are polarized so they can't be plugged backwards!

![](https://cdn-learn.adafruit.com/assets/assets/000/001/634/medium800/raspberry_pi_jstsm4plug_LRG.jpg?1396774579)

![](https://cdn-learn.adafruit.com/assets/assets/000/001/635/medium800/raspberry_pi_jstsm4receptacle_LRG.jpg?1396774588)

# Light Painting with Raspberry Pi

## Software

Danger: 

On the software front, we started with “Occidentalis,” the [Adafruit Raspberry Pi Educational Linux Distro](http://learn.adafruit.com/adafruit-raspberry-pi-educational-linux-distro/overview). This has a few features baked in that make this project a lot simpler, including hardware SPI support…it’s super fast and easy, with no bitbang kludges required. [Alternatively you can install SPI support yourself](../../../../adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-spi)  
  
Our light painting script relies on the Python Image module, which is not installed by default. With your Raspberry Pi connected to the internet, log in and type:  
  
**sudo apt-get install python-imaging**   
  
Occidentalis also has _sshd_ built in, making it easy to get images from a desktop machine to the Raspberry Pi. For example, in a Terminal window on my Mac I could type:  
  
**scp image.png pi@raspberrypi.local:**  
  
Or use a secure FTP client:

![](https://cdn-learn.adafruit.com/assets/assets/000/001/590/medium800/raspberry_pi_sftp.jpg?1396774145)

Here’s the complete Python script for the light painting project. Even with lots of comments, it’s not a large program at all:

```auto
#!/usr/bin/python

# Light painting / POV demo for Raspberry Pi using
# Adafruit Digital Addressable RGB LED flex strip.
# ----&gt; http://adafruit.com/products/306

import RPi.GPIO as GPIO, Image, time

# Configurable values
filename  = "hello.png"
dev       = "/dev/spidev0.0"

# Open SPI device, load image in RGB format and get dimensions:
spidev    = file(dev, "wb")
print "Loading..."
img       = Image.open(filename).convert("RGB")
pixels    = img.load()
width     = img.size[0]
height    = img.size[1]
print "%dx%d pixels" % img.size
# To do: add resize here if image is not desired height

# Calculate gamma correction table.  This includes
# LPD8806-specific conversion (7-bit color w/high bit set).
gamma = bytearray(256)
for i in range(256):
	gamma[i] = 0x80 | int(pow(float(i) / 255.0, 2.5) * 127.0 + 0.5)

# Create list of bytearrays, one for each column of image.
# R, G, B byte per pixel, plus extra '0' byte at end for latch.
print "Allocating..."
column = [0 for x in range(width)]
for x in range(width):
	column[x] = bytearray(height * 3 + 1)

# Convert 8-bit RGB image into column-wise GRB bytearray list.
print "Converting..."
for x in range(width):
	for y in range(height):
		value = pixels[x, y]
		y3 = y * 3
		column[x][y3]     = gamma[value[1]]
		column[x][y3 + 1] = gamma[value[0]]
		column[x][y3 + 2] = gamma[value[2]]


# Then it's a trivial matter of writing each column to the SPI port.
print "Displaying..."
while True:
	for x in range(width):
                spidev.write(column[x])
                spidev.flush()
		time.sleep(0.001)
	time.sleep(0.5)
```

The script needs to be run as “root” because it accesses the GPIO hardware, i.e.:  
  
**sudo python lightpaint.py**  
  
After opening the SPI device (for communicating with the LED strip), the&nbsp;script then loads an image using the Python Image module, converting it to RGB format if necessary.&nbsp;As an interpreted language, Python isn’t always the quickest thing…rather than repeatedly process each row or column of an image on the fly, the entire image is pre-processed once&nbsp;from the native RGB format into the hardware-specific format required of the LED strip, held in a list of bytearrays. We can then quickly dump each of these arrays directly to the SPI port without any further work.&nbsp;Decoding and holding&nbsp;all this intermediate data would be inconceivable on Arduino!  
  
The software is smart enough that it will use the height of the image as the number of pixels to address. So if you have 64 pixels, make sure your images are 64 pixels tall, etc!

This runs in an infinite loop. Hit **CTRL+C** or locate and kill the process to make it stop.

### 

Common causes, in decreasing order of likelihood:

- The clock and data lines might be switched. Try the other way, and if no difference, put them back and double-check against the wiring diagram and the pin labels on the strip.
- Might’ve accidentally connected to the output end of the strip rather than the input. Make sure you soldered to “CI” and “DI,” not “CO” and “DO.” Some strips may have arrows showing the data direction from in to out.
- Did you remember the ground wire between the LED strip and Raspberry Pi?
- You might need a logic-level shifter, explained a bit on the “Hardware” page.

# Light Painting with Raspberry Pi

## Motion Rig

Handheld, straight LED bars have been _done._ With the code so quickly out of the way, we wanted to take this to the next level.&nbsp;First, the light bar would be replaced with a circle, in order to give the finished pictures an interesting three-dimensional quality. Second, it would be attached to a bicycle to provide smooth motion and to cover&nbsp;much longer distances.&nbsp;Riding this through the darkness during a long-exposure photograph should then create an extruded tube in 3D space. The bicycle “disappears” in the image because it doesn’t sit still long enough for the camera to expose.  
  
A contraption was quickly assembled from PVC pipe and a hula hoop, then spray painted matte black to be stealthy for photos. This rig would attach with zip ties to the rack over the back wheel of the bike.

![](https://cdn-learn.adafruit.com/assets/assets/000/001/625/medium800/raspberry_pi_pvc.jpg?1396774498)

For power to both the LEDs and computer, a camping battery/inverter was used.&nbsp;The absurdity of this is not lost on us — converting 12V DC to 110V AC back down to 5V DC — but time was of the essence and the portable power unit was already on-hand (and when installed on the bike it looks like a Mr. Fusion…cool!). For something less hack-ish, a DC-to-DC converter would make a lot of sense.  
  
A strip of 96 LEDs was used to go around the hoop   
  
Generous application of zip ties and tape complete the mad science project look.&nbsp;This won’t be surviving a trip to Mars any time soon.

![](https://cdn-learn.adafruit.com/assets/assets/000/001/627/medium800/raspberry_pi_junkinthetrunk.jpg?1396774519)

Yes, that’s my bike. Er, trike. What? Why are you looking at me funny?

![](https://cdn-learn.adafruit.com/assets/assets/000/001/628/medium800/raspberry_pi_trike.jpg?1396774527)

# Light Painting with Raspberry Pi

## Results

These first images were&nbsp;cobbled together pretty quickly,&nbsp;and we didn’t go out of our way for a good, dark location.&nbsp;With some planning and refinement the results can potentially be _much_ nicer than this.  
  
These are all 10- to 15-second exposures.  
  
The ring of fire:

![](https://cdn-learn.adafruit.com/assets/assets/000/001/629/medium800/raspberry_pi_fire.jpg?1396774535)

Cola wars:

![](https://cdn-learn.adafruit.com/assets/assets/000/001/630/medium800/raspberry_pi_cans.jpg?1396774542)

Big snake. Ostensibly a python:

![](https://cdn-learn.adafruit.com/assets/assets/000/001/631/medium800/raspberry_pi_snake.jpg?1396774550)

_Tron Legacy_ light cycle:![](https://cdn-learn.adafruit.com/assets/assets/000/001/632/medium800/raspberry_pi_tron.jpg?1396774557)

Total project time, starting from zero Python experience to having a working demo, photographs and a tutorial, was about two days. Great things are afoot!

![](https://cdn-learn.adafruit.com/assets/assets/000/001/633/medium800/raspberry_pi_IMG_0922.jpg?1396774571)

Update: Here's a quick test Ladyada did in Adafruit warehouse last night, shot with a phone!


## Featured Products

### Adafruit Assembled Pi Cobbler Breakout + Cable for Raspberry Pi

[Adafruit Assembled Pi Cobbler Breakout + Cable for Raspberry Pi](https://www.adafruit.com/product/914)
Now that you've finally got your hands on a [Raspberry Pi® Model B](http://www.raspberrypi.org/), you're probably itching to make some fun embedded computer projects with it. What you need is an add on prototyping Pi Cobbler from Adafruit, which can break out all those...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/914)
[Related Guides to the Product](https://learn.adafruit.com/products/914/guides)
### Digital RGB LED Weatherproof Strip - LPD8806 32 LED 5m

[Digital RGB LED Weatherproof Strip - LPD8806 32 LED 5m](https://www.adafruit.com/product/306)
These LED strips are fun and glowy. There are 32 RGB LEDs per meter, and you can control each LED individually! Yes, that's right, this is the digitally-addressable type of LED strip. You can set the color of each LED's red, green and blue component with 7-bit PWM precision (so 21-bit...

In Stock
[Buy Now](https://www.adafruit.com/product/306)
[Related Guides to the Product](https://learn.adafruit.com/products/306/guides)
### 4-pin JST SM Plug + Receptacle Cable Set

[4-pin JST SM Plug + Receptacle Cable Set](https://www.adafruit.com/product/578)
These 4-wire cables are 15cm long and come as a set, one side has a JST SM type connector plug on the end. The other side has a matching JST SM type receptacle connector. They are good for whenever you have 4 wires you want to be able to plug and unplug. We like the solid and compact nature of...

In Stock
[Buy Now](https://www.adafruit.com/product/578)
[Related Guides to the Product](https://learn.adafruit.com/products/578/guides)
### 5V 10A switching power supply

[5V 10A switching power supply](https://www.adafruit.com/product/658)
This is a beefy switching supply, for when you need a lot of power! It can supply 5V DC up to 10 Amps, running from 110V or 220V power (the plug it comes with is for US/Canada/Japan but you can use any plug adapter for your country, or just replace the cable with a standard computer/appliance...

In Stock
[Buy Now](https://www.adafruit.com/product/658)
[Related Guides to the Product](https://learn.adafruit.com/products/658/guides)
### 5V 2A (2000mA) switching power supply - UL Listed

[5V 2A (2000mA) switching power supply - UL Listed](https://www.adafruit.com/product/276)
This is an FCC/CE certified and UL listed power supply. Need a lot of 5V power? This switching supply gives a clean regulated 5V output at up to 2000mA. 110 or 240 input, so it works in any country. The plugs are "US 2-prong" style so you may need a plug adapter, but you can pick one...

In Stock
[Buy Now](https://www.adafruit.com/product/276)
[Related Guides to the Product](https://learn.adafruit.com/products/276/guides)
### Female DC Power adapter - 2.1mm jack to screw terminal block

[Female DC Power adapter - 2.1mm jack to screw terminal block](https://www.adafruit.com/product/368)
If you need to connect a DC power wall wart to a board that doesn't have a DC jack - this adapter will come in very handy! There is a 2.1mm DC jack on one end, and a screw terminal block on the other. The terminals are labeled with positive/negative assuming a positive-tip configuration...

In Stock
[Buy Now](https://www.adafruit.com/product/368)
[Related Guides to the Product](https://learn.adafruit.com/products/368/guides)

## Related Guides

- [LPD8806 Digital RGB LED Strip](https://learn.adafruit.com/digital-led-strip.md)
- [Raspberry Pi Physical Dashboard](https://learn.adafruit.com/raspberry-pi-physical-dashboard.md)
- [OctoPrint - Open Source Host Software](https://learn.adafruit.com/octoprint-open-source-host-software.md)
- [NeoPixel Bracelet](https://learn.adafruit.com/neopixel-bracelet.md)
- [Adafruit's Raspberry Pi Lesson 8. Using a Servo Motor](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-8-using-a-servo-motor.md)
- [Trellis Python Library](https://learn.adafruit.com/trellis-python-library.md)
- [Monitor Your Home With the Raspberry Pi B+](https://learn.adafruit.com/monitor-your-home-with-the-raspberry-pi-b-plus.md)
- [Reverse Engineering a Bluetooth Low Energy Light Bulb](https://learn.adafruit.com/reverse-engineering-a-bluetooth-low-energy-light-bulb.md)
- [MCP9808 Temperature Sensor Python Library](https://learn.adafruit.com/mcp9808-temperature-sensor-python-library.md)
- [Infinity Mirror Collar](https://learn.adafruit.com/infinity-mirror-collar.md)
- [Processing on the Raspberry Pi & PiTFT](https://learn.adafruit.com/processing-on-the-raspberry-pi-and-pitft.md)
- [Make a Zelda Master Sword with the RP2040 Prop-Maker Feather](https://learn.adafruit.com/master-sword-rp2040.md)
- [Knobby Sequencer](https://learn.adafruit.com/knobby-sequencer.md)
- [Halloween Skeleton Transformation Illusion Prop](https://learn.adafruit.com/halloween-skeleton-transformation-illusion-prop.md)
- [Pocket PiGRRL](https://learn.adafruit.com/pocket-pigrrl.md)
- [Cosplay Glow Fur Raver Bandolier](https://learn.adafruit.com/cosplay-glow-fur-raver-bandolier.md)
