ASCIMathML.js

Tuesday 17 January 2012

555-Timer Hello World

I want to build a device that can measure the capacitance of a bunch of capacitors I have of unknown value.  I realised I had a 555 Timer in my box of random IC's and, looking at how one is fitted together it seems that (in Monostable mode) the output pulse is proportional to the resistor and capacitor (C1 and R1 in the diagram below)

555 Timer (Generated with Livewire)

The length of time of the output pulse is given by the following equation:

R = R1 in Ohms
C = C1 in Farads


Given that in our circuit R1 = 15.4 * 10^3 and C1 = 10 * 10^-6 we can calculate that the timespan of the pulse should be about 0.17 seconds. 

Now that I have a flashing LED, record this pulse width and test the circuit.


NB:  If you're thinking "How does he know the value of R1?", will, resistance is easy to measure, capacitance is not!

I'll update the blog when I've done this!


Sunday 15 January 2012

Digital IO Using Interupt Events

The previous post showed that we can read the value of D0 by connecting it to D1, setting D1 as an InputPort and using the Read() method.  Whilst this is find for a Hello World demo proving that it works, it's not much good for the real world.  What if we want D1 to report when the value changes?

For this we need to use the InteruptPort, and the OnInterupt event.  Even though the InputPort exposes this event, when you try to wire it up the framework throws one of its ever useful InvalidOperation exceptions.

So, we've got the Netduino wired up just like before, with D0 connected directly to D1, as so:

Pin D0 connected to pin D1

The code for our sample program is then:

namespace DigitalIOHelloWorld
{
    public class Program
    {
        public static void Main()
        {
            OutputPort pinD0 = new OutputPort(Pins.GPIO_PIN_D0, false);
            InterruptPort pinD1 = new InterruptPort(Pins.GPIO_PIN_D1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);
            pinD1.OnInterrupt += new NativeEventHandler(pinD1_OnInterrupt);
            while (true)
            {
                Thread.Sleep(1000);
                Debug.Print("D0 = " + pinD0.Read());
                pinD0.Write(true);
                Thread.Sleep(1000);
                Debug.Print("D0 = " + pinD0.Read());
                pinD0.Write(false);
                Debug.Print("Reading D1 = " + pinD1.Read());
            }
        }

        static void pinD1_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            Debug.Print("D1: data1 = " + data1 + "\tdata2 = " + data2 + "\ttime = " + time.ToLocalTime());
        }
    }
}


Now, when the input signal to D1 changes (i.e. when the output from D0 changes) the event is triggered and the Debug statement prints this out.  We can see it's working as every time the we change the D0 output, we see the new state of D1.


The statement gives us three pieces of information.  Look at data2 now though, a value of 1 means that the voltage is high, 0 means it is low.

The Debug information appears in the Output Window, and will look like this:

D0 = False
Turning D0 on
Reading D1 = True
D1: data1 = 28    data2 = 1    time = 01/01/2009 00:13:24
D0 = True
Turning D0 off
Reading D1 = False
D1: data1 = 28    data2 = 0    time = 01/01/2009 00:13:25
D0 = False
Turning D0 on
Reading D1 = True
D1: data1 = 28    data2 = 1    time = 01/01/2009 00:13:26

Digital IO - Hello World

I've just got my Netduino Plus and so some Hello World apps are needed.

There are many examples on how to get your on-board LED to flash, things really get interesting when you can accept input from the pins.  This is a Hello World on reading off the digital pins.

The plan is a simple one.
  1. Connect the D0 to D1.
  2. Have D0 turn off and on.
  3. Read off D1.
  4. See is the value read from D1 matches the value of D0
 I've connected the pins as shown in this picture:

Digital Pins 0 and 1 connected

In order to print out these values, we'll just use the Debug.Print() method, so you'll see it in the Output window of the IDE.  The code used looks like this:

namespace DigitalIOHelloWorld
{
    public class Program
    {
        public static void Main()
        {
            OutputPort pinD0 = new OutputPort(Pins.GPIO_PIN_D0, false);
            InputPort pinD1 = new InputPort(Pins.GPIO_PIN_D1, false, Port.ResistorMode.Disabled);

            while (true)
            {
                Thread.Sleep(1000);
                Debug.Print("D0 = " + pinD0.Read());
                Debug.Print("Turning D0 on");
                pinD0.Write(true);
                Debug.Print("Reading D1 = " + pinD1.Read());
                Thread.Sleep(1000);
                Debug.Print("D0 = " + pinD0.Read());
                Debug.Print("Turning D0 off");
                pinD0.Write(false);
                Debug.Print("Reading D1 = " + pinD1.Read());
            }
        }
    }
}

 Deploying this and running it will produce this output in the IDE:

D0 = False
Turning D0 on
Reading D1 = True
D0 = True
Turning D0 off
Reading D1 = False
D0 = False
Turning D0 on
Reading D1 = True
D0 = True
...


As you can see, the read value on D1 matched the state of D0.

Hope this helps.