When your starting off with a new embedded system, particularly the Arduino and Netduino the usual thing to do is make the LED flash, when you see the light you know you've gone from source code on the PC to a running micro controller.
Naturally I did exactly that, but I wanted to take it a little further, so the PowerLED project was born.
The idea of the PowerLED project is a home automation unit, controlled over a wired ethernet connection, hopefully drawing power from a POE switch via the same ethernet cable and powering a number (3+) of high power (1+ Watt) LEDs.
So, a simple start, get an LED to light up. Then extend that to power a serious LED.
I tried two variations for driving the LED. The initial design was to use a power MOSFET switched from an IO line. This didn't go as simply as I had hoped. My choice of FET was dictated by what I had to hand and that was dictated by what Maplin had in stock many months ago when I thought of the project.
The FET I used was a IRF540A, that beast can switch something in the region of 28 Amps, whilst that would be useful for halogen lamp switching, it wasn't exactly what I needed for a LED drawing about 300mA. The big problem with the device was that the 3v3 voltage drive from the micro controller was not enough to get the FET fully conducting, so I needed to drive a simple NPN transistor from the micro which then connected the gate of the FET to ground (it was held high with a 10k resistor).
After getting the FET solution working I decided to try another way, a ULN2803A octal driver, this is a chip that will switch up to 500mA per channel (and you can parallel up the channels), it was very simple to use, connect the ground pin to ground, the common pin to the drive voltage for the LED (not really needed but should the LED be replaced with a relay then this will prevent any reverse EMF from destroying the chip), then connect the input to one of the micro GPIO pins and the output to the ground of the LED. Nice and simple.
For good measure I also paralleled up a low power LED on the gate output so I didn't have to use the high power LED.
I also wanted a way to measure the light levels in the room so the lights didn't need to be switched on if the room was already bright enough. Fortunately I had to hand a TEMT6000 breakout board from Sparkfun to hand. This gives out a voltage based on the light level. To make this work that voltage just needs to be fed into one of the Analog In connections.
One Gotcha worth noting here is that on the Netduino the Analog Reference isn't connected, so you need to supply a reference voltage, otherwise all your analog reads will be 1023 (Analog max for a 10bit ADC). In this case the Aref pin is connected to the 3v3 connector, and the TEMT6000 is also powered from the 3v3.
So here are our first important lessons.
- Use prototype block board for initial prototyping, not an expensive (£5-10) prototype shield that doesn't take well to having lots of change happing on it.
- The Arm processor at the heart of the Micro Framework uses 3v3, not 5v like the Arduino. Now the chip is 5v tolerant, so you can stuff 5v down the GPIO line without killing it, however not all hardware works with 3v3 logic, in this case a simple FET, so that needs to be taken care of.
- To use the Analog ports a Aref needs to be supplied.
This project is going to be more than just a simple LED driver so I wanted to make a good start to the code. A critical part of this project is going to be the ethernet connection and the Netduino doesn't currently support that (the Netduino Plus should do and that's in Beta) I wanted to use my Meridian/P board, however that doesn't have an analog input for the light sensor, so I also want to use the Netduino and in the future move the project to the Netduino Plus.
The project references both the Netduino and Meridian/P SDK's and I have introduced hardware provider and controller base classes that are subclassed for the specific board implementation. At startup some logic is used to tell which board the code is running on and select the appropriate controller. This controller is then used to control the hardware, by using Liskov substitution principle and only calling the methods and properties on the abstract base controller we can switch the hardware without having to change any code or recompile.
I've set up a public repository on GitHub where the code will live. Don't worry if you don't do git you should be able to use GitHub to download a zip of the latest code from the website.
git://github.com/BookSwapSteve/PowerLED.git – Readonly access
The software is over complicated for a simple flashing LED example, but it's a good start for encapsulating away various aspects of the hardware.
The controller class defined some outputs which LEDs will be connected to, some inputs for a switch and the light sensor and some code to handle configuration so the light levels for dark and light can be defined.
The main program defines a timer which on each tick reads the current light level and if it's too dard switches on the LED. Fairly simple at this stage.
If your are use to app development one thing to watch out for is to not allow the Main() method to exit. If it does thats it, program finished. So here the Thread.Sleep(Timeout.Infinite) is used to keep the app running whilst events are used from the controller and timer to fire off some work.
Here's a couple of video's showing the unit working. I set the timer to a fast interval, then by pointing the light at the sensor it toggles the LED on each tick of the timer. This demonstrates one of the great advantages of the LED light, it can be switched really quickly (actually you can switch LEDs at MHz but I'll save why that's interesting for another blog post, but not the type of light used here as it has a constant current driver built in and that doesn't handle the switching all that well).
I wouldn't dare to try and switch a halogen lamp as quickly as I was doing with the LED, and had I use a relay for switching it would have been making a terrible noise.
This first video shows the LED being switched on as I block the light to the sensor and then switching off again as the light level is restored:
The second video shows the rapid switching of the LED as the light is reflected off my hand onto the sensor switching off the LED: