Netduino Transistor Switch

A transistor switch allows you to use a smaller flow of electrons to control a much larger flow of electrons. In this case, we are using the Netduino’s 3.3V output to control turning on and off a 9v supply.

It took me a while to figure out how this should be wired up, but after a lot of looking around I found some arduino examples. This one in particular was very helpful

My take away is that you should have your circuit set up like this. Effectively your transistor is blocking the current from going to ground until some input to the base is given from the Netduino.

Some notes on the switch: There is a formula for figuring out the value of the resistor going to base is, however it is outside the scope of this tutorial. I’ve just used 1k and it works fine. Also be sure to connect both the ground from your power, and the ground from your Netduino. For the longest time I didn’t have my Netduino grounded like in the images above and could not figure out why it was not working!

Netduino Pull up and Pull down Resistor

The idea of a pull up and pull down resistor initially confused me. After reading around the web a bit I found a description that helped me. Pull up and pull down resistors are used when you have something on your board that is not connected between the Vcc and ground (this is also known as an open circuit). Say for example when we wire up a motion sensor to our circuit, there will be a period of time when it is an open circuit or open collector. Whilst something is not connected directly between Vcc and ground it is susceptible to external electrical noise – which means you might get random erratic readings. For this reason – we use the pull up or pull down resistor.

Pull Up Resistor Example on the Netduino

The pull up resistor will make the input read as high/Vcc and when triggered go low to 0/ground.

Pull Up – Not Connected

Here we have the input port d0 being pulled up to Vcc by the 10k resistor. If we didn’t have the resistor you might get the event handler being randomly fired by electric fluctuations in the circuit.

Pull Up – Connected

Now when we complete the circuit by connecting to ground, we will fire the event handler. Our pin was being held at Vcc (3.3v) and now will be pulled to ground (0v). So our second parameter in the event handler will read as a low of 0.

Pull Up – Source Code

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace _3_Digital_Input
{
    public class Program
    {
        public static void Main()
        {
            InterruptPort input = new InterruptPort(Pins.GPIO_PIN_D0, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
            input.OnInterrupt += new NativeEventHandler(input_OnInterrupt);

            Thread.Sleep(Timeout.Infinite);
        }

        static void input_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            string s = "";
        }
    }
}

Pull Down Resistor Example on the Netduino

The pull down resistor will make the input read as 0. When it is triggered it will go high to Vcc (3.3v).

Pull Down – Not Connected

Here we now are connected via the 10k ohm resistor to ground, bringing the pin to 0v.

Pull Down – Connected

When I complete the circuit the event handler fires. The pin was being held at 0v and now has been pulled high to 3.3v. If you breakpoint on the event handler and inspect the second parameter (data2) you should see the value come in as 1.

Pull Down – Source Code

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace _3_Digital_Input
{
    public class Program
    {
        public static void Main()
        {
            InterruptPort input = new InterruptPort(Pins.GPIO_PIN_D0, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh);
            input.OnInterrupt += new NativeEventHandler(input_OnInterrupt);

            Thread.Sleep(Timeout.Infinite);
        }

        static void input_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            string s = "";
        }
    }
}

Side note: The Netduino does also have an inbuilt Pull up resistor that you can turn on and off by the software. This is an enumeration that you can see as one of the parameters when creating the interrupt port. I didn’t include it on this example, as I wanted to show mirror symmetry between the pull up and pull down circuits.

The Netduino does not have an inbuilt Pull down resistor. So you will always have to wire it like the example above.

Building a DIY Home Alarm Part 2 (Netduino Plus WebServer)

For this second part in the series I wanted to prototype the network connectivity of the Netduino. Why? Because I want the alarm to message me some how if some one breaks in AND I want to be able to interact with it from my phone.

Prototype Features

  • Run a threaded web server, capable of returning 200 OK with the HTTP body of “Hello World”
  • Flash the onboard LED whenever the webserver services an inbound request
  • Trigger a sound (I’ll cover this sound in more detail later in my posts). I was using a transistor switch to trigger a 555 timer which makes a 3khz sound to the speaker

Approach

I started off using any old network cable to connect to the Netduino. After some thought and discussion on the NetDuino chat I realized I should be using a Crossover cable. Luckily I had one to hand. (Good thing I hoard my electronics stuff :P). I made sure the network cable was connected before I turned the NetDuino on. I think I read somewhere that with certain versions of the flashed software, it can be troublesome connecting the network cable afterwards.

I discovered that the Netduino’s network configuration can be configured programatically or through the tool that comes with the microframework MFDeploy.exe (I found it in C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.1\Tools). Through a lot of experimentation I figured the easiest way of me connecting the pc and the netduino would be to manually assign ip addresses on the same subnet mask. My laptop was initially setup for DHCP assigned ip on the physical network connection and the netduino was pre-wired with an ip. I set the pc to be a manually assigned IP of 10.5.0.1 and the netduino to 10.5.0.2 and both on a subnet mask of 255.0.0.0.

Using Fiddler I sent HTTP Get Requests to the web server and received 200 OK messages back with the content “Hello World”.

Demo

Source Code

Note: I used the .Net Micro Framework Networking Toolkit to run the web server.
PS: Make sure you use the assembles found in the folders prefixed with “Micro” as they are compiled for the micro framework.
PPS: I used port 8080 as my machine is running IIS on port 80.

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using MFToolkit.Net.Web;
using Microsoft.SPOT.Net.NetworkInformation;

namespace WebServer
{
    public class Program
    {
        public static void Main()
        {
            HttpServer httpServer = new HttpServer(8080, new MyHttpHandler());
            httpServer.Start();
        }
    }

    public class MyHttpHandler : IHttpHandler
    {
        private OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
        private OutputPort sound = new OutputPort(Pins.GPIO_PIN_D5, false);
        private bool playSound = false;

        public void ProcessRequest(HttpContext context)
        {
            led.Write(true);
            BuildHttpResponse(context.Response);
            led.Write(false);

            if (playSound == false)
            {
                playSound = true;
            }
            else
            {
                playSound = false;
            }

            sound.Write(playSound);
        }

        public void BuildHttpResponse(HttpResponse httpResponse)
        {
            httpResponse.HttpStatus = HttpStatusCode.OK;
            httpResponse.WriteLine("Hello World");
        }
    }
}

Building a DIY Home Alarm Part 1 (Learning electronics)

Background

After listening to Scott Hanselman’s podcast on the NetDuino I was really interested in the idea of building my own hobby projects and learning electronics. I’ve got two projects that I want to build, both of them rather ambitious 😛

The first idea was to build a Snowbot (snow blowing robot) and the other idea was to build a home alarm system. I’m starting with the home alarm system as it in theory should be easier to build.

This is what I would ideally have in a home alarm system:

  • Networked, so that I can arm/disarm and check the status over the web
  • Email alerts
  • Sensors on all the doors and windows
  • 2x Motion sensors (main floor and basement)
  • Bloody loud alarm system

Starting Point

So looking at the best places to start learning how to do all of this I decided it would be best to start learning electronics. The best resources I have found so far are:

I tried using software to model circuits but found them really confusing. I tried Tina-TI, while it is really good for certain learning, I have found that actual hands on with components is easier to learn from.

I decided to buy some basic electronics components. Looking through the Make electronics book and searching on the web i found this product, a starter electronics kit for the first few chapters of the MAKE: Electronics book. I noted down what i thought were the key components and went to the local electronics shop and bought them for around $150 including digital multimeter.

The main components I bought were the multimeter, a $15 dollar pack of assorted resistors (definitely worth the money), various capacitors, some NPN transistors, breadboard and 9v supply, assorted LEDs and wires.

After reading up on DC theory at allaboutcircuits.com I started building some really simple circuits, with the home alarm system in mind.

I tried to visualize what components the alarm would need to have to make this alarm system. At its simplest it would need the following:

  • Blinking LED
  • Siren
  • Circuit that would break when a window/door was opened
  • Some sort of keypad interface (ideally touchscreen)
  • Netduino as the microcontroller

The next post in this series will cover the alarm’s blinking LED.