We here at SparkFun have been known to use our products to play pranks on each other, and we're always on the lookout for ways to make life at the office more fun. While riding our elevator one day, it occurred to me that the ride could be made more interesting…but how? Then I realized: not “how” but “Who!”

The Doctor to the rescue

If you’re not already a fan of the British science-fiction show “Doctor Who,” then this project won’t make much sense to you. “Doctor Who” was first broadcast in 1963 (it is the longest-running science fiction TV show in history), and from the beginning, the show has made clever use of low-budget but creative special effects.

For generations of fans, the signature sound of The Doctor’s time and space machine, the TARDIS, is instantly recognizable, which provides a perfect opportunity to bring it into existence in our humble lift. The cyclic, ethereal grinding sound of the TARDIS was originally created by BBC sound technician Brian Hodgson by dragging a set of house keys along the strings of a piano and electronically processing the result. (Yes, I’m a giant nerd for knowing that.)

Goals for the project

1. Play the TARDIS sound in the elevator while it’s going up or down (blinking blue light a bonus).

2. ???

3. Profit!

Note that the core of this project (play a sound when a sensor is activated) is applicable to all sorts of areas (Halloween, art installations, scaring your little brother, etc.), so read this with an eye toward applications you'd like to develop.

Recon

My first thought was to hack straight into the elevator’s up and down buttons, but I quickly came to my senses. Building managers do not want you messing with critical infrastructure (for good reason), and I personally don’t want to get on the bad side of anyone who gets stuck in an elevator because of me. (Around here, the winner has been known to choose the loser’s tattoo.)

However, it occurred to me that it might be possible to make a completely self-contained unit, discreetly placed above the elevator’s drop ceiling, that could detect when the elevator starts moving all by itself. How? By using an accelerometer.

An accelerometer measures, logically enough, acceleration. This is different from velocity; while a car (or an elevator) is accelerating from stopped to cruising speed, the accelerometer will read >0. But once the car (or elevator) has reached cruising speed and the velocity is constant, the accelerometer will read 0. When you decelerate, the opposite happens: The accelerometer will read < 0 until the car (or elevator) has stopped, at which point the accelerometer will again read 0. To see what kind of numbers we were dealing with, I hooked up an Arduino, an ADXL345, and an Openlog. (Tip: The Openlog is a great tool for early development work like this. It stores all the data you can generate, making the data easy to analyze later). I took a trip up and down in the elevator, popped out the SD card, loaded the data into a spreadsheet, and made a quick graph:

In the graph, the approximate elevator position is the red line, and the Z-axis data out of the accelerometer is the noisy blue line. You can see spikes in the accelerometer data when the elevator starts and stops, and at other times the readings hang around 230 (1g). These spikes aren't huge—the peaks aren’t far outside the 1g noise—but they look clear enough to pick up with a little coding. Let’s try it!

Shopping list

Now’s the time to divide the project into subsystems, or modules we can connect together to do all the things we want.

The main requirement for the project is to play back the TARDIS sound. SparkFun carries several products that could do this, but we’ll balance cost and ease of development and go with a combination of 3.3V Arduino Pro and MP3 Shield. We’ll use the Arduino to read the accelerometer and decide when to play the sound, and we’ll use the MP3 shield to play the TARDIS sound directly off an microSD card. We’ll also grab two sets of Stackable Headers to connect the boards together.

SparkFun carries a number of accelerometers that would work in this application. I went with an ADXL345 for its ease of use and extra features like threshold interrupts that we could potentially use to wake up everything when the elevator is moving.

The MP3 Shield has line-level audio outputs. To produce a suitably loud noise, we’ll need an amplifier and a speaker. The amplifier is a prototype of a board we’ll soon be carrying. The speaker, a cheap desktop-computer model, came from our recycling bin.

We’ll need a battery. The reason I chose a 3.3V Arduino Pro is that it runs very nicely on our 3.7V LiPo batteries. I chose the 6Ah battery for maximum life, but it's admittedly expensive. You could certainly get away with a smaller one depending on your lifetime requirements (see the “Enhancements” section at the end of this article for battery-life tips).Finally, it occurred to me that along with the sound, it would be nice to simulate the blinking blue light on top of the TARDIS, so throw in a couple of ultra-bright blue LEDs and resistors. These LEDs use more power (80 mA x 2) than an Arduino I/O pin can provide (20 mA), so we’ll use one of Nate’s little MOSFET boards to drive them. We only need one board to drive as many LEDs as we like, up to 4A worth.

Since we'll be powering the LEDs directly off the battery (to avoid overtaxing the 3.3V regulator on the Arduino Pro board), we should think about the maximum voltage the LEDs will see and choose the current-limiting resistor accordingly. The flat part of a LiPo battery's discharge curve is around 3.7V, but the voltage could be as high as 4.2V when it's fully charged. To prevent burning out the LEDs, we'll use the worst-case numbers: 4.2V from the fully charged battery and, from the LED product page, Vf = 3.0V and If = 80 mA. Plug these into the handy LED resistor calculator of your choice, and the recommendation is 18Ω. We'll use one resistor per LED (so we can use lower-power resistors), and power the LEDs in parallel, but you can run the numbers for other resistor configurations if you wish.

Apart from some headers, jumper wires and traditional red cardboard box, we’re ready to put everything together! Thanks to the folks in IT, here’s a handy shopping list for all the parts you’ll need.

Hardware

Assembly of the unit is straightforward. Solder the 6- and 8-pin headers to the Arduino Pro and the MP3 Shield, and stack them together. The battery plugs into the JST connector on the Pro. The rest of the circuitry connects the various boards. The following schematic shows the connections:

I threw everything into one of our famous red boxes, but you can do whatever works best for your project. The only critical part is to mount the accelerometer board securely and horizontally (Z-axis pointing down), so that it will properly pick up the motion of the elevator.

Software

As you’re probably aware, the great thing about the Arduino is the vast amount of example code available. On the MP3 Shield product page is a complete playback example sketch, and a little Googling turned up this page with ADXL345 interface code. Combining these two pieces of code resulted in very close to the final product (thank you, Internet!).

It’s usually not difficult to mash several pieces of code together. You only get one setup() and one loop() per sketch, so merge everything into one set of functions. Make sure there aren't any pin conflicts, and reallocate one of the pins if there is.

Download the whole sketch here.

Not much is different between the sketch and the two pieces of code merged together, but we did change a few things:



The MP3 chip has a volume-control register; we made it as loud as it can go (0 dB).

The main loop() has been altered to read the accelerometer and check if the value is outside a window for more than 10 consecutive readings (100 ms, to avoid momentary spikes) and, if it is, to start playing the TARDIS sound.

Normally when playMP3() is called, it plays the MP3 all the way to the end before control is returned to the main loop. But Nate put some code into the MP3 example sketch that shows you can periodically do other things between MP3 data transfers. We put a hook in that spot that calls another function, Mp3whilePlaying(). We use this new function to light the super-bright blue LEDs according to a sine wave, which gives them a nice slow pulsing action like the real TARDIS. When the MP3 ends and we're back in the main loop, the LED might still be on so we take a moment to finish fading it out. See our Pulse a LED tutorial for more information on this technique.



To improve the power-consumption situation, we turn the amplifier board (coming soon from SparkFun) and MP3 chip off when we're not using them. The amplifier has an active-low “shutdown” input, so we make it low when we want to turn the amplifier off. The MP3 chip has a reset line that, when active, keeps the chip in shutdown mode. Unfortunately, every time you turn it on, you'll need to set up several registers (memory locations inside the MP3 chip) with values necessary for it to play MP3s. So, we pulled out the section of setup() where this is done and put it in its own function so we can call it anytime we want.



And note again that you can use any sensor, button press, serial input, etc. as a trigger to play a sound. Your imagination is the limit.Reception

A lot of people LOLd and a few people WTFd. One anonymous fan added more blue LEDs to the box to increase the effect!

Enhancements

“Better is the enemy of good enough ” – Sergey Gorshkov, CIC Soviet Navy during the Cold War

“Anything worth doing is worth overdoing ” – Mick Jagger

As it stands, the contraption works well enough (much like the TARDIS itself). But for those looking for perfection, here are a few improvements you could try:

Improve battery life using sleep mode. Right now, the Arduino is on all the time, using about 20 mA of current continuously. We’re got a pretty big battery attached to it (6000 mAh), which gives it a lifetime of about a week, but the battery could last for months if the project went to sleep between playings. The Arduino can indeed be programmed to go to sleep, waiting for an interrupt signal (a pin-changing state) to wake it up. And the ADXL345 accelerometer can be configured so that it sends an interrupt when an acceleration threshold is reached, so this shouldn't be difficult to do. In practice, you can’t get down to microamps with a full Arduino board, since it will always be burning some current in the voltage regulator, power LED, and so forth. But with a bit of software and hardware hacking, sleep mode would definitely improve the lifetime.



Right now, the Arduino is on all the time, using about 20 mA of current continuously. We’re got a pretty big battery attached to it (6000 mAh), which gives it a lifetime of about a week, but the battery could last for months if the project went to sleep between playings. The Arduino can indeed be programmed to go to sleep, waiting for an interrupt signal (a pin-changing state) to wake it up. And the ADXL345 accelerometer can be configured so that it sends an interrupt when an acceleration threshold is reached, so this shouldn't be difficult to do. In practice, you can’t get down to microamps with a full Arduino board, since it will always be burning some current in the voltage regulator, power LED, and so forth. But with a bit of software and hardware hacking, sleep mode would definitely improve the lifetime. Stop playing when the elevator stops. Currently, the TARDIS MP3 was edited so that it is approximately the length of an elevator ride, and the code plays the MP3 to the end before listening for further accelerometer bumps. But you could also end a longer MP3 when you detect that the elevator stops. Or, since the MP3 chip has a volume-control command, you could even fade it out!



Currently, the TARDIS MP3 was edited so that it is approximately the length of an elevator ride, and the code plays the MP3 to the end before listening for further accelerometer bumps. But you could also end a longer MP3 when you detect that the elevator stops. Or, since the MP3 chip has a volume-control command, you could even fade it out! Bigger and better! Bigger speakers, disco lights and music, black lights…you can really go in any direction. Make us proud. But remember…

A final word of caution

SparkFun is one of a very few companies where one can put a scary-looking jumble of circuit boards in the roof of an elevator without someone immediately calling Homeland Security. Even so, it’s dicey: What would the elevator-repair people do if they ran across this? There have been several high-profile incidents where makers have done things they thought were perfectly innocent, but that resulted in a disproportionately large response from the law-enforcement community. Unfortunately, this is the environment we live in, and until it changes, think three times before deploying smart pranks like this. Then, have fun!

“To the rational mind, nothing is inexplicable, only unexplained .” – The Doctor.