Quantcast

Worklog: Arduino-Controlled Pantry LED Lighting

By Will Smith

Will's dark pantry has forced him to build a simple, Arduino-controlled LED strip lighting setup--to save his sanity.

I have a pantry that isn’t particularly well lit. In fact, it would be safe to say that it’s very poorly lit. It’s deep and narrow, which makes it really hard to see the stuff in the back and even harder to reach back there. Over the years, we’ve tried adding those cheap stick on LED lights to the underside of the shelves—you know, the ones that turn on automatically when they detect light--but the pantry is so dark that those lights don’t turn on reliably. Even when they do turn on, they don't add enough light to be useful.

Enter LED strip lighting. For about $15, you can buy a strip of warm white LEDs on a long strip that’s part conductor, part mounting surface. It would have been pretty easy to wire up a few strips of LED lights, and hook them up to a simple circuit controlled by a microswitch that was triggered when my pantry door opens or closes. But once I looked into adding a microcontroller to the mix, I figured it was time to learn more about Arduino.

I didn't need anything as complex as the individually addressable RGB strip lights Alex showed us a month or so ago, but I did want more than I could do with a simple switch. Instead of using a mechanical switch that would require installation and wear out over time, I wanted something that would work through the floor. Power isn’t a problem—I happen to have a handful of power outlets in the crawlspace directly under the pantry (the entry to my house’s crawlspace and the home run for my home network are both beneath the pantry, so I have power for the Ethernet switch that lives down there). Once I decided to use a microcontroller, I wanted the lights to gradually brighten when the door opens, instead of just blasting on at full power. I didn’t say I had anything vital to do with the microcontroller, sometimes it's the small things.

To get started, I purchased 5m of warm, white LED strip lights from Amazon ($15), an Arduino Uno v3 ($25), an Arduino Proto Shield ($5), some wire, a couple of power supplies (9V for the Arduino, 12V for the lights), and a MOSFET to switch the higher voltage circuit (more on that later). I was able to piece together the lighting part of the circuit pretty quickly from a handful of online how-tos (don't worry, specifics are down below), but I needed to figure out the sensor portion. I bought a couple of different types of sensors to experiment, but I ended up using a Hall effect sensor. Hall effect sensors detect magnetic fields, which meant I should be able to mount the microcontroller and sensor below the floor, hot glue a magnet or two to the bottom of the pantry door, and everything should work exactly the way I want it to. In theory. I haven’t tested this through the floor, which contains iron nails, yet.

With a plan in place, it was time to learn how to make the Arduino do what I wanted.

Remembering Skills Lost for 20 Years

As Alex mentioned in his Arduino primer last month, Arduino sketches—the programs that run on a board to determine its behavior—use C or C++ syntax. The last time I regularly wrote C code was in a CompSci 101 class I took during my sophomore year in college, roughly twenty years ago. I was not a good programmer, the fact that I escaped my intro to C class with a C+ was a significant moral victory to 20-year-old me.

I quickly realized that learning C in the context of a microcontroller was much easier for me to understand than writing command-line interface software.

With my very basic understanding of C, I realized that my copy of Getting Started with Arduino was a bit too basic for me. I already understood about loops and if statements, declaring variables and the like and the basic C syntax came back quickly (if you don’t know that stuff, Getting Started seems great). I put the book down, loaded up the reference section at the Arduino website, and dove into the IDE and started experimenting with switches and lights and buzzers.

I quickly realized that learning C in the context of a microcontroller, which let me change things in the real world using software I wrote, was much easier for me to understand than writing command-line interface software. It was also much more fun. I’m pretty results driven, so when a semester spent writing useless code solely to learn programming concepts culminated in writing an awkward CD cataloging program I was pretty bummed.

Contrast that with the Arduino. I wrote a basic program, built a circuit using a breadboard and an Uno, uploaded it to the Uno, and could see the results of my work in moments. Then I was able to tweak the program to see what happened. Using the combination of hardware and software, I could iterate very quickly. Using a combination of the Uno’s serial interface and the lights I hooked up to the Uno to diagnose problems gave me better insight into the inner workings of my program than I ever had in class. I was able to get the LED strip to turn on and off based on button presses in an hour or so. However, the LED strip wasn’t particularly bright, and the instant on nature of the strip was too jarring for my taste.

The brightness problem was relatively easy to fix—I needed to supply more power to the LEDs than the Arduino could deliver through the Arduino. My LED strip was optimized for 12V power, which the Arduino could theoretically handle, but I’d already torched my MacBook by pumping too much power back down the USB port, so I wanted to play it a little safe. I found a guide that explained how to use an Arduino with a MOSFET to switch more power than the Arduino could handle. Put simply, the Arduino tells the MOSFET when to open or close—the Arduino is essentially a switch that controls a larger switch, capable of handling more current. I'm probably going to experiment with running the Arduino using the 12V supply this week, just to reduce the number of cables that will live under my house.

In progress: The lighting side is mostly complete, but there's no sensor or switch depicted here.

From the same how-to, I lifted the code needed to cycle the brightness of the lights using one of the Uno’s PWM outputs. Using the PWM output let me mimic a simple analog control by cycling power to the LEDs on and off very quickly. I tweaked the code to get timing on the fade up right, and called it a day. If you're interested in my code, there's a completed version here.

I'm relatively new to Arduino programming, constructing my own electronics circuits and all this. I'm trying to say it's possible, and even likely, that I messed something up in my circuit diagram, or am not following best practices on my code. The thing I built works, but learning how to communicate this stuff is tricky. If I've messed something up, I'd love your help and suggestions. Please leave constructive feedback. I'm figuring this out as I go along, I don't pretend to be an expert on this!

NEXT WEEK: Figuring out the Hall effect sensor, writing a program that reliably worked, more experiments with magnetism, moving the circuit from a breadboard to its permanent home, and putting this whole contraption in the closet.