Introduction: How to Create a Lightbox With Distance Sensor and Ardunio

You'll need:

1. Hollowed out shadow box
1. Ardunio
1. Prototyping Shield
1. HC-SR04 Ultrasonic Range Module
1. N+ Channel Mosfet
1. External Power Supply (12v 2a)
Some LED Strips

This is how you make an LED Lightbox that will light up as your viewer gets closer to your image. It will add that extra element of fun to art looking!

Step 1: Step 1

Get your shadow box. I got mine from Michaels Arts and Crafts supply store. Really cheap, about 10-20$ depending on size and sometimes quality.
Once you have your box, take everything out. Some of them come lined with felt or whatever. Don't throw away the glass and spacer bars we are gonna need those later.

Paint the back of the white for a more even light!

Step 2: Step 2

Take your LED strips and attach them to a piece of cardboard back to back. You don't want to place your LED strips directly to the back of the lightbox because you will have some really nasty hot spots in your print. The best way is to glue your LED strips back to back on some cardboard, then attach the cardboard to the back with the LED strips facing each other. Then wire them up! I used a hot glue gun to affix the led strips to the back, but its not a long term solution as glue tends to fail in the long run.

See Image.

Step 3: Step 3

So now we are going to build the prototyping shield for Arduino Uno.

1. figure out your pin configuration for your Ultrasonic Sensor. Mine was clearly printed on the front so it was easy to set up. I would check the data sheet as I've seen older models that don't have trig or echo as separate pins. One pin goes to 5v, other to GND. The Echo and Trig can go into which ever pin you would like. I put my Echo in pin 7, and Trig in pin 8.

I also wired up my n-channel mosfet. Again, its very important you check the data sheet and figure out which leads are your Gate, Drain, and Source. I would connect your Gate to which ever pin you wanted to use as the LED control. I used pin 6 for its PWM capability. Source goes to the same ground as your LED strip. And Drain will take your negative lead from the LED Strip. Please see the images below for an example of my shield (using a breadboard for clarity) and my schematic. The power plug is a 12volt 2amp and the LED represents the LED strip.   

Step 4: Step 4

The code used for this I found mostly online, but I had to tweak it a bit to make it more stable. There is some value adjusting to make sure it works for your need and space.
I would search for "distance = pulseTime" its default is 58, but I would play with it between 20 and 70 depending on your space and how reliable the ultrasonic sensor is.


const int numOfReadings = 1; // number of readings to take/ items in the array
int readings[numOfReadings]; // stores the distance readings in an array
int arrayIndex = 0; // arrayIndex of the current item in the array
int total = 0;  // stores the cumulative total
int averageDistance = 0; // stores the average value

// setup pins and variables for DYP-ME007 sonar device
int echoPin = 7;  // DYP-ME007 echo pin (digital 2)
int initPin = 8;  // DYP-ME007 trigger pin (digital 3)
unsigned long pulseTime = 0;   // stores the pulse in Micro Seconds
unsigned long distance = 0; // variable for storing the distance (cm)

// setup pins/values for LED
int LEDPin = 6; // LED, connected to digital PWMpin 6
int LEDValue = 0; // stores the value of brightness for the LED (0 = fully off, 255 = fully on)

void setup() {
pinMode(LEDPin, OUTPUT); // sets pin 6 as output
pinMode(initPin, OUTPUT);  // set init pin 8 as output
pinMode(echoPin, INPUT);  // set echo pin 7 as input

// create array loop to iterate over every item in the array
for (int thisReading = 0; thisReading < numOfReadings; thisReading++) {
readings[thisReading] = 0;

// initialize the serial port, lets you view the
// distances being pinged if connected to computer

// execute
void loop() {
digitalWrite(initPin, HIGH);  // send 10 microsecond pulse
delayMicroseconds(0); // wait 10 microseconds before turning off
digitalWrite(initPin, LOW);   // stop sending the pulse
pulseTime = pulseIn(echoPin, HIGH); // Look for a return pulse, it should be high as the pulse goes low-high-low
distance = pulseTime/58; // Distance = pulse time / 58 to convert to cm.
total= total - readings[arrayIndex]; // subtract the last distance
readings[arrayIndex] = distance;  // add distance reading to array
total= total + readings[arrayIndex];  // add the reading to the total
arrayIndex = arrayIndex + 1;   // go to the next item in the array

// At the end of the array (10 items) then start again
if (arrayIndex >= numOfReadings) {
arrayIndex = 0;
averageDistance = total / numOfReadings; // calculate the average distance

// if the distance is less than 255cm then change the brightness of the LED
if (averageDistance < 255) {
LEDValue = 255 - averageDistance; // this means the smaller the distance the brighterthe LED.

analogWrite(LEDPin, LEDValue); // Write current value to LED pins
Serial.println(averageDistance, DEC); // print out the average distance to the debugger
delay(500); // wait 100 milli seconds before looping again

Step 5: Final Assembly and Video

After you put it all together in the box (show below). Add your image into the box and test it out.