Netduino: Let There Be Light

This is the 3rd in a series of projects for the Netduino and Seeed Studio’s Grove Starter Bundle.

This installment is intended to create a plant light that automatically switches on and off at a pre-set time of the day (well, more usefully, the night).  It is inspired by the “Garduino” project, which was published in Make Magazine and can also be found on Instructables.

This project builds on the foundation set by the earlier two articles. If you want to actually get the project working yourself, you’ll probably need to browse the earlier articles, especially the part about the serial connection to the PC.

The first two articles  in the series were:

Netduino Enters the Grove

and

Netduino: Time and Weather

As the project develops it will soon be necessary to move beyond the Grove’s components (that 8×2 display, in particular, is cramping my style).  First, though, we’re going to bring another Grove component into the act: the Relay twig.

The Relay twig is used to switch the plant light off and on.   To connect the relay to the plant light, we first need to splice open an AC extension cord.

Now, I know that Edison fellow has been spreading the word about AC power being dangerous and what-not.  Sounds scary, don’t it?  Well, it’s true!  Ask Topsy the Elephant.

Seriously, though, various unpleasant things can happen if you hook the relay up incorrectly, but fortunately the risk is mainly to the relay, and your self-esteem.  However, since you don’t want your relay Twig to end up like poor Topsy,  I’ll spend much of this article covering the process of  making that connection.

You actually have a couple of options: build or buy.

Option 1 – Modify an Extension Cord

Here’s the plan: The Relay twig acts as a on/off switch for the extension cord.  The twig has two little green screw connectors.  The “live” wire of the extension cord needs to be cut open, and the 2 exposed ends need to be inserted into those 2 green connectors.  Sounds simple, and it is.

The following photo shows a modified 2-prong extension.  It also shows the connection to the Relay twig, but don’t worry about that for now – it is explained in the “Connecting the Relay Twig” section, below.

Two-pronged extension cord connected to Grove Relay
Two-pronged extension cord connected to Grove Relay

The process of modifying the extension cord isn’t complicated or dangerous. Anything you do wrong will be evident before you get to the stage where you’ve got the extension cord plugged into the wall. However, it does requires some soldering and some common sense.  If you’re not confident about your abilities at either of these, consider Option 2, below.

The extension cord you use doesn’t need to be anything special – the cheapest one at Wal-Mart is good enough.  If your plant light has a three prong plug then, of course, you need to buy a three prong extension cord.

The best guide to what’s required (and the one I originally followed) is the one in the Make magazine Garduino article. The Instructables version of Garduino refers you to 2 other sites.  Personally, I found the Hobby Robotics article to be confusing (so many wires!) and the Sparkfun article to be downright intimidating (you don’t have to pull a power receptacle out of the wall for this project).  As far as I know the Make Magazine article isn’t available to non-subscribers, but it just so happens that someone (not me) has posted a PDF of this article here.  To modify the extension cord for use with Relay twig, follows steps 7a-7c.

The Make magazine article shows how to modify a two-prong extension cord.  If your plant light requires a 3-prong cord, then the only difference is the process of selecting the right wire to cut and splice.  

It must be the “live” one, which for both 2- and 3-prong cords is the one connected to the narrower blade of the plug.  Generally, the corresponding wire in the cord is aligned with the blade of the plug (i.e. if the blade is on the left, you need to cut the cord on the left).   However, to make sure that you’ve selected the right one before cutting it, nick the cord to expose the copper underneath, then use a multimeter to check connectivity with the narrower blade of the plug.  (If you picked the wrong cord, it’s safe to cover the nick with electrical tape, as I did with the 3-prong extension cord in the photo below.)

Three-pronged extension cord connected to Seeed Relay Brick
Three-pronged extension cord connected to Seeed Relay Brick

Option 2 – Buy a Netduino-Friendly Extension Cord

A product called the “PowerSwitch Tail” recently became available.  It is specifically intended to be hooked up to microcontroller circuits, such as the Arduino and Netduino.  It contains a 5V relay similar to the one in the Grove starter kit – if you go this route, you can ignore the Grove relay.  You’ll have to connect a wire from the Powerswitch to the D0 pin of the Netduino (or modify my code to use whichever pin you’d like).  Then, just skip ahead to the “First Test” section below.  You can read more about the PowerSwitch on the Adafruit product page and in Adafruit’s forums.

I haven’t tried a PowerSwitch, but I’m 99.9% sure it will work fine with the Netduino and this project.

Connecting the Relay Twig

You should have two wires with exposed tips coming from your extension cord.  As you probably figured out, you need to connect each wire into one of the screw connectors on the Relay twig.  Ah, but which goes into which?

Unfortunately, Seeed didn’t put any markings on the v0.9 Relay twig to help you out.  The Datasheet and tutorial on Seeed’s web site don’t offer any guidance on this either.  (“If in doubt contact a professional such as a licensed electrician for help“.  Uh, yeah, good luck with that.)

However, the HLS8L-DC5V relay happens to be the exact same model as used in Seeed’s Electronic Brick relay, and the Brick relay is well documented in this Instructables article and in this Seeed Forum post.

The Grove relay has removed one of the Brick’s 3 screw connectors, meaning that the only way you can hook it up is “normally open”.  This means that the extension cord won’t (yeah, “open” means “off” – go figure) conduct electricity when your Netduino project is powered off.

The 2 screw connectors are, then, are the “Common” and “NO” connectors shown in the Instructables article.  As with its big brother, the Seeed Relay Brick, the twig’s Common connector is on the left (when the screw connectors are facing you) and Normally Open is on the right.  See the photos in this post.

Important Note: this is true for the “0.9″ version of the twig, which is the first version that was sold.  If your twig doesn’t say 0.9 on it, then I’d strongly suggest you double-check this information, either by using a multimeter or by checking the Seeed product page or forum.  Seeed recently posted an article about version 1.0 of the Grove Starter Bundle.  The Relay twig photo on that page looks more like a Relay forest, so if your twig is bristling with relays then all bets are off!

To check continuity with a multimeter, you want to confirm that the “Common” left-hand screw connector is connected to the middle pin at one end of the black relay box, and the other screw connector is connected to one of the 2 pins at the other end of the black relay box. I think the best illustration of the orientation of the pins for this relay is the one in the Seeed forum thread.

Grove Relay Brick - hooked up and ready to rock
Grove Relay Brick - hooked up and ready to rock

OK, so at this point you should have an UNPLUGGED extension cord that is connected to an unplugged relay twig.   This is the time to ensure that you’ve firmly and securely connected the extension cord to the relay.  Tug  hard on the relay twig to make sure that it doesn’t come loose from the extension cord.  

Carefully inspect the points at which the wires from the extension cord are plugged into the screw connectors of the Grove twig.  There must not be any exposed metal from the wire.  If there is, insert the wire further into the screw connector or, if the exposed metal is simply too long, snip it shorter.

That cord-to-relay connection is the only dangerous (to you) part of the setup, and you definitely don’t want to have a wire come loose after you plug it in!

Incidentally, if you Googled for information about the HLS8L-DC5V relay, you may have been unsettled to find that there isn’t much out there, aside from a bunch of pretend-datasheet-sites gaming Googling search engine.  However, I’ve had 2 of the Seeed bricks with this relay in daily service for about 6 months now, and never had a problem with either of them.  So, for what its worth, I’m pretty comfortable with this relay, safety-wise.

Initial Test

I’ve written a small test project, RelayTester, that you can use to test turning the relay off and on.  Pressing the green button (connected to D8) toggles the relay.

Start by unplugging everything from the Grove shield.  Reconnect to the Grove shield just 2 twigs: the one with the red and green buttons (connected to plug D7/D8) and the Relay twig (connected to D0, and to an UNPLUGGED extension cord).  Yes, there, is such a thing as D0 on the Grove shield, off to one end, as shown in the photo in the “Final Configuration” section, below.

Deploy the test project to the Netduino.  For this part of the test, you can leave the Netduino plugged into the USB port of the PC.

The red light on the relay may briefly go on and then off when the Netduino boots up.  Press the green button on the button twig, and the red light should come on and stay on.  Press the green button again to turn it off.

Do you hear a fairly loud clicking noise from the black relay box each time the red light goes on or off?  If not, something is wrong.

If the red light on the relay works, but there is no click, try using a different cable between the Relay twig and the Netduino.  If that doesn’t help, try a different connector than D0 (you’ll have to change the test program code accordingly, of course).

If the red light doesn’t come on at all, check that the connectors are plugged into the Grove shield correctly (buttons in D7/D8, relay in D0, and nothing else connected), and that the project was deployed to the Netduino correctly.  (It will display messages to the Visual Studio Debug window when turning the relay on or off).

If that’s not the problem, then you might have a bad relay twig and should check the Seeed forums or e-mail them.

Live Test

I don’t think there is much risk to the Netduino if things aren’t hooked up correctly.  The Netduino isn’t exposed to the AC current – that’s the whole idea of the relay.  The biggest risk is to the relay itself, and possibly to whatever you have plugged into your extension cord.  However, as an extra precaution, I’d suggest you have as little as possible connected to the Netduino: the Grove shield, the button twig and the relay twig.

Also, I’d suggest you power the Netduino from a DC adapter rather than the PC’s USB port.  The concern isn’t that the relay is power-hungry, but that you want to protect your PC’s USB port if things go wrong.    (If you haven’t run the Netduino with that DC adapter before, rerun the above “Initial Test” with the extension cord still unplugged from the wall.)

OK, so it’s all been fun and games so far, but no fooling around for this test. Remember Topsy!

  1. Plug the plant light (or a cheap test light) into the extension cord.
  2. Plug the extension cord into the wall.
  3. Plug the DC adapter into the Netduino.
  4. Press the green button.  Let there be light!
  5. Leave the light turned on for awhile, periodically touching the extension cord, the wires leading from the extension cord to the relay, and black relay box to make sure it isn’t getting hot.  The cord and wires shouldn’t get warm at all; the relay box will be very slightly warm.

One last (I promise!) comment about safety: ideally you should never make any adjustment to the relay/extension cord/light setup with the extension cord plugged in.  When hooking things up, and when adjusting the setup, unplug the extension cord first.  Keep an eye on those 2 screw terminals on the Relay twig and the wires inserted into them — that’s the danger zone.  Don’t pile anything on top of them, don’t let those wires get snagged on something.

Final Configuration

OK, let’s bring the rest of the Grove twigs back on stage.  (Not you, Prototype Twig, we aren’t ready for you yet.  Oh, and Tilt Switch twig, um, you can run along home.  We’ll keep you in our files, and we’ll call if we need to do any tilting).

The configuration is the same as in the previous project, Time and Weather, with the addition of the Relay twig:

  • D0/D1 – Relay
  • D2 – jumper to TX pin of serial header on breadboard
  • D3 – jumper to RX pin of serial header on breadboard
  • D4/D5 – right side of LCD stem, lower connector
  • D6/D7 – piezo buffer twig (only D6 is used)
  • D7/D8 – button twig
  • D9/D10 – bottom of LCD stem, right connector
  • D11/D12 – bottom of LCD stem, left connector
  • D13 – right side of LCD stem, upper connector
  • A0 – modified potentiometer (unused for Grove LCD, or any other LCD without a software backlight control)
  • A2 – temperature twig
Final Configuration of the Netduino and Grove components.  Can't see the forest for the trees!
Final Configuration of the Netduino and Grove components. Can't see the forest for the trees!

By now, you’ve probably noticed that I have a strange aversion to pin D1.  What’s up with that?

Well, I have 2 Netduinos, one for development and one for testing.  My test Netduino suffered an unfortunate accident, resulting in it being, um, “differently abled” on the D1 pin.

Wait, what?   The guy who’s  giving you safety tips electrocuted his Netduino?

Well, not on this project – honest!  I’m actually not sure at what point it stopped working, but I suspect it occurred while experimenting with the not-particularly-dangerous-but-not-Netduino-friendly Danger Shield.  (See my “Spiderman: The Musical” project for details).

The source code for the LightController project can be downloaded here.

As with the test project, press (and, this time, hold) the green button to manually turn the relay (and light) on and off.  You can also program it to turn off and on at a certain time, using the handy serial port connection from the previous project.  (Stop groaning, we’ll get rid of the serial port in a later project, I promise.)

The 2 new commands are:

+:hh:mm

Sets the time at which the light will come on
-:hh:mm

Sets the time at which the light will go out.

There is no default on and off time – you have to set them manually for now.  (Since you have to set the Netduino’s clock manually, there wouldn’t be much point to having default timer settings).

I’ve also added some new button features, pretty much exhausting the possibilities for the 2-button twig:

  • Press the red button: Toggles LCD between the regular time/temperature display, and a display of the  Light On and Light Off times.   If all you see is “+” and “-”, with no times, then you need to set the times using the serial terminal “+” and “-” commands, as described above.
  • Press and hold red button: Toggles the serial port off and on, as described in the “Time and Weather” article.
  • Press the green button: toggles the first row of the LCD between the current time and a countdown to the next light “event”.  That is, if the light is currently on, it shows a countdown to when the light will go off.  And vice versa.
  • Press and hold green button: toggles the light off and on.
  • Press and hold red and green button: clears the light on and light off timer.  Also makes your fingers hurt.

Programmer Show And Tell

The new code has a few features worth mentioning.

The light on/off timers are implemented using the ExtendedTimer class.  This is the lesser known of the 2 .Net Framework timers.  It is better suited than the Timer class for time events that occur at specific times of day, rather than at intervals.

For example, the code to set the “light on” timer is:

// set the timer to go off at this date, and every 24 hours after that
tmrLightOn = new ExtendedTimer(new TimerCallback(SetLightState), true, datAlarm, new TimeSpan(1, 0, 0, 0));

Since we want the light to come on at the same time each day, the ExtendedTimer constructor does all the work. There is no need to reset the timer for the next day when the timer event fires.

The countdown timer takes advantage of the fact that, in .Net, subtracting one date from another returns a TimeSpan that gives the difference between them:

// relay is off, display time to next light-off
string strCountdown;
if (datNextOn == DateTime.MinValue)
{
    strCountdown = "Not set";
} else
{
   TimeSpan timeTo = datNextOn.Subtract(DateTime.Now);
   strCountdown = timeTo.ToString();
}
lcd.SelectLine(1, true);
lcd.WriteStringToLCD(strCountdown);

Note that DateTime (and TimeSpan) objects can’t be null – an unset DateTime has a value of DateTime.MinValue.

And lastly, this projects makes extensive use of the 2 pushbuttons. I really like the fact that all the button features can be implemented in the .Net MF without polling.  

Buttons can be configured as InterruptPorts.  The InterruptPort constructor has a parm that lets you specify when the event should be triggered — when you press the button, when you release the button, or both. The following code specifies both:

buttonRed = new InterruptPort(Pins.GPIO_PIN_D7, true,
   SecretLabs.NETMF.Hardware.Netduino.ResistorModes.Disabled,
   SecretLabs.NETMF.Hardware.Netduino.InterruptModes.InterruptEdgeBoth);

The event handler needs to handle 2 cases: a short push and a push-and-hold.  The handler can’t know which it is until the button is released.  On the other hand, the user would like to have some confirmation of the push-and-hold action before letting go.  The answer is to set a timer (the regular kind) on the button press.  If the button is released before the timer goes off, that’s a short push.  If the timer expires first, then that’s a push-and-hold.  I use a 2-second timer.

Here’s the button’s event handler. The same event handler is fired when the button is pressed and when it is released. The state parm tells you which event it is.

static void buttonRed_OnInterrupt(uint port, uint state, DateTime time)
{

  if (state == 1)
  {
     // start the button hold timer
    timerButtonRed = new Timer(new TimerCallback(buttonHold),
       buttonRed, BUTTON_DELAY, BUTTON_DELAY);
  }
  else
  {
     // cancel the button hold timer
     if (timerButtonRed != null)
     {
        timerButtonRed.Dispose();
      }
      if (blnButtonHeld)
      {
         // timer went off, so ignore the release
         blnButtonHeld = false;
         return;
      }

      // normal button press - handle it

      . . .

Note that .Net MF timers are turned off using the .Dispose method – there is no property for setting a timer’s active state.

Last, here’s the timer’s event handler.  I decided to have the red and green button share the same button timer, so a parm is passed to indicate which button it is.  

A boolean variable is used to tell the button’s event handler to ignore the button’s release event when the user eventually lets go.  

Note that, in order to implement the double-button-hold feature for clearing the timers, the code checks the state of the other button using the .Read method, same as you would if you used polling to check the buttons.

private static void buttonHold(object data)
{

  // tell the button release event to ignore it - we're handling it
  blnButtonHeld = true;

  if ((InterruptPort)data == buttonRed)
  {
     // cancel the button hold timer - otherwise, this event will go off again if user keeps button pressed
     timerButtonRed.Dispose();

     // if both buttons are held down, clear the light timers
     if (buttonGreen.Read())
     {
        // cancel the green button's timer
        timerButtonGreen.Dispose();

        clearTimers();
     }
     . . .

So what’s next for this project?  Needless to say, this project is badly in need of an improved user interface, so a menu system can be used to replace (or supplement) the serial port commands.  This will require that we say bye-bye to the tiny 8×2 Grove LCD.

But first, we’ll let Mother Nature handle one of the settings.
This entry was posted in .Net, Electronics, Programming and tagged , , . Bookmark the permalink.

Comments are closed.