Search

Friday, October 31, 2014

Halloween Costume with Arduino

This Halloween me and my daughter decided to add some dazzle to her fairy costume. Since we were anyway learning to code on Arduino we decided to dip our hands in wearables.

The basic idea is to build a costume that glows when someone comes close. The project was intended to teach a 9 year old to code and is hence simple enough for her to grasp. We used the following

Parts

  1. Arduino UNO board
  2. TIP120 transistor
  3. Diode 1N4004
  4. 1K Resistor
  5. HC-SR04 Ultrasonic Range Finder

Circuit

It’s best to consider the circuit as two separate pieces. One to acquire the distance of someone approaching using the HC-SR04 ultrasound range finder. The second is to actually make the LED strip glow.

The first part consists of connecting the 4 pins of the HC-SR04 as follows

image

We cannot simply drive the LED strip using an output pin of Arduino because the strip drains way more current than that can be supplied by the Arduino chip. So we use a TIP120 or TIP121 chip as shown below

image

There is a nice explanation of this whole setup at http://www.instructables.com/id/Use-Arduino-with-TIP120-transistor-to-control-moto/. The same principles hold, but instead of a fan we use a LED strip in our case.

Code

The entire code is available on GitHub at https://github.com/bonggeek/GlowDress/ (I cleaned up the code a tiny bit after my daughter wrote it). This is how it looks

#include <ultrasonicranging.h>

#define ECHO_PIN 2 // ECHO pin of HC-SR04
#define TRIG_PIN 3 // Trigger pin of HC-SR04
#define LED_OUT 5 // Drive LED (Base pin of TIP120

const int space = 125; // Distance in cm in which to trigger LED

void setup()
{
Serial.begin (9600);
pinMode(TRIG_PIN, OUTPUT); // trigger pin of US range finder
pinMode(ECHO_PIN, INPUT); // Echo pin of US range finder
pinMode(LED_OUT, OUTPUT); // base of TIP120 to drive LED

analogWrite(LED_OUT, 0);
}

void GlowLed()
{
// Slowly get from LED strip off to full bright (glow-in)
for (int brightness = 0; brightness < 255; brightness++)
{
analogWrite(LED_OUT, brightness);
delay(3);
}

// Slowly get from LED strip on to full off (glow-out)
for (int brightness = 255; brightness >= 0; brightness--)
{
analogWrite(LED_OUT, brightness);
delay(3);
}
}

void loop()
{
int distance = GetDistanceInCm(TRIG_PIN, ECHO_PIN);
Serial.println(distance);

if (distance <= 0 || distance > space)
{
analogWrite(LED_OUT, 0);
delay(500);
return;
}

if (distance <= space)
{
GlowLed();
}
}

Here to abstract away the intricacies of how distance is received from the ranger, I have used GetDistanceInCm. The source for this library is at  https://github.com/bonggeek/GlowDress/tree/master/UltraSonicRanging.


Once we tested out the circuit we went ahead and soldered it on a board. My daughter did receive a battle scar (a small burn from iron) but we battled on.

IMG_0565This is how it looks partially done 

IMG_0574


With my wife’s help we sewed it underneath her fairy dress. It was pretty well concealed other than the sensor sticking out a bit.


IMG_0578