.NET Gadgeteer Relay Circuit using DigitalOutput Interface

The relay scenario uses a low-voltage logic circuit to control another circuit, usually of higher voltage.  The relay in this example is a 5 volt DC single pole, single throw reed relay. It is used to switch a battery-driven motor on and off.  The switch is controlled by the Gadgeteer.DigitalOutput interface in a .NET Gadgeteer Visual Studio application.

The schematic for this scenario is shown in the following illustration.

DigitalOutput Relay Circuit

DigitalOutput Relay Circuit

5 volts is supplied by pin 2 of a type X or Y socket on the GHI Electronics Spider mainboard.  The connection in this case is made on a GHI Electronics Extender module.  When the application using the DigitalOutput circuit drives V logic in high, the switch in the relay closes, allowing current to flow from the battery circuit through the motor, M.

This example is based on the same scenario as a previous post; for more information, see Using the .NET Gadgeteer DigitalOutput Interface.

The GT.Interfaces.DigitalOutput object is initialized in the following lines of code.

GT.Socket socket = GT.Socket.GetSocket(4, true, null, "DigitalOutControl");
    control = new GT.Interfaces.DigitalOutput(socket, GT.Socket.Pin.Four, false, null);

Two methods activate the logic high state.

In one case the ButtonEventHandler of a button module starts a timer.  When the GT.Timer.TickEventHandler is raised, an if block writes either true or false in the Gadgeteer.Interfaces.DigitalOutput.Write method. The status of the DigitalOutput object is also shown on the Display_T35 module by the Gadgeteer.Modules.Module.DisplayModule.SimpleGraphicsInterface.DisplayText method.

    void timer_Tick(GT.Timer timer)
    {
        if (control.Read() == true)
        {
            control.Write(false);
            led.TurnGreen();

        }
        else
        {
            control.Write(true);
            led.TurnRed();
        }

        display.SimpleGraphics.Clear();
display.SimpleGraphics.DisplayText("DigitalOutControl: " + control.Read().ToString(),
                Resources.GetFont(Resources.FontResources.small), GT.Color.Green, 50, 50);

    }

The second way to activate the logic high state is the joystick_JoystickPressed event handler, as shown in the following code block. Again the logic state of the DigitalOutput interface is displayed by the Display_T35 module.
Some of the buttons in circulation are glitchy, and I’ve had a few problems with them, so I used the Joystick in this segment.

void joystick_JoystickPressed(Joystick sender, Joystick.JoystickState state)
    {
        if (control.Read()==false)
        {
            control.Write(true);
            led.TurnRed();

            display.SimpleGraphics.Clear();
            display.SimpleGraphics.DisplayText("Timer: " + timer.IsRunning.ToString(),
                Resources.GetFont(Resources.FontResources.small), GT.Color.Green, 50, 75);

        }
        else
        {
            control.Write(false);
            led.TurnGreen();

            display.SimpleGraphics.Clear();
            display.SimpleGraphics.DisplayText("Timer: " + timer.IsRunning.ToString(),
                Resources.GetFont(Resources.FontResources.small), GT.Color.Green, 50, 75);

        }
    }

The running application is shown in the following video segment. First the motor is started by pushing the button to start the timer.  It runs through two ticks before I turn it off.  Next I start the motor by pressing the joystick.

The complete application code follows.

using Microsoft.SPOT;

using GT = Gadgeteer;
using Gadgeteer.Modules.GHIElectronics;

namespace RelayControl
{
    public partial class Program
    {
        GT.Timer timer;
        GT.Interfaces.DigitalOutput control;

        void ProgramStarted()
        {
            GT.Socket socket = GT.Socket.GetSocket(4, true, null, "DigitalOutControl");
            control = new GT.Interfaces.DigitalOutput(socket, GT.Socket.Pin.Four, false, null);

            joystick.JoystickPressed += new Joystick.JoystickEventHandler(joystick_JoystickPressed);
            button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);

            timer = new GT.Timer(5000);
            timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);

            Debug.Print("Program Started");
        }

        void button_ButtonPressed(Button sender, Button.ButtonState state)
        {
            if (control.Read() == true)
                return;

            if (timer.IsRunning)
            {
                timer.Stop();

                display.SimpleGraphics.Clear();
                display.SimpleGraphics.DisplayText("Timer: " + timer.IsRunning.ToString(),
                    Resources.GetFont(Resources.FontResources.small), GT.Color.Green, 50, 75);

            }
            else
            {
                timer.Start();

                display.SimpleGraphics.Clear();
                display.SimpleGraphics.DisplayText("Timer: " + timer.IsRunning.ToString(),
                    Resources.GetFont(Resources.FontResources.small), GT.Color.Green, 50, 75);

            }
        }

        void joystick_JoystickPressed(Joystick sender, Joystick.JoystickState state)
        {
            if (control.Read()==false)
            {
                control.Write(true);
                led.TurnRed();

                display.SimpleGraphics.Clear();
                display.SimpleGraphics.DisplayText("Timer: " + timer.IsRunning.ToString(),
                    Resources.GetFont(Resources.FontResources.small), GT.Color.Green, 50, 75);

            }
            else
            {
                control.Write(false);
                led.TurnGreen();

                display.SimpleGraphics.Clear();
                display.SimpleGraphics.DisplayText("Timer: " + timer.IsRunning.ToString(),
                    Resources.GetFont(Resources.FontResources.small), GT.Color.Green, 50, 75);

            }
        }

        void timer_Tick(GT.Timer timer)
        {
            if (control.Read() == true)
            {
                control.Write(false);
                led.TurnGreen();

            }
            else
            {
                control.Write(true);
                led.TurnRed();
            }

            display.SimpleGraphics.Clear();
            display.SimpleGraphics.DisplayText("DigitalOutControl: " + control.Read().ToString(),
                    Resources.GetFont(Resources.FontResources.small), GT.Color.Green, 50, 50);

        }
    }
}

Advertisements

,

  1. #1 by Harry Brown on January 5, 2012 - 10:54 AM

    Hi, when I try to use the code:

    joystick.JoystickPressed += new Joystick.JoystickEventHandler(joystick_JoystickPressed);

    I get errors saying that I should include it in the references, even though all other Gadgeteer components work. What should I do?

  2. #2 by Michael Dodaro on January 5, 2012 - 11:19 AM

    If you created the application with the Visual Studio Designer, the reference to the Joystick libraries will be added automatically, and the declaration will be in the Program.generated.cs file. It should contain something like this:

    public partial class Program : Gadgeteer.Program
    {
    // GTM.Module defintions
    Gadgeteer.Modules.GHIElectronics.MulticolorLed led;
    Gadgeteer.Modules.GHIElectronics.Extender extender;
    Gadgeteer.Modules.GHIElectronics.Display_T35 display;
    Gadgeteer.Modules.GHIElectronics.Joystick joystick;
    Gadgeteer.Modules.GHIElectronics.Button button;

    For more information about using the Designer, see this post: http://wp.me/p1TEdE-1c
    The Program.generated.cs file is created by the Designer and should not be edited directly. If the Joystick library is not showing in references, go back to the Designer and drag the Joystick module from the toolbox and connect it to socket 9. Then save the Designer changes.

    • #3 by Harry Brown on January 5, 2012 - 11:50 AM

      I’ve still got the problem. Here’s the error as copied from Visual Studio:

      Error 1 ‘Gadgeteer.Modules.GHIElectronics.Joystick’ does not contain a definition for ‘JoystickPressed’ and no extension method ‘JoystickPressed’ accepting a first argument of type ‘Gadgeteer.Modules.GHIElectronics.Joystick’ could be found (are you missing a using directive or an assembly reference?)

      Thanks for your help!!

  3. #4 by Michael Dodaro on January 5, 2012 - 11:52 AM

    Does the Joystick show in the Designer?

  4. #5 by Harry Brown on January 5, 2012 - 11:58 AM

    Yeah, but there’s little I can do with it. If I just type ‘joystick. ‘… the options are DebugPrintEnabled, Equals, GetHashCode, GetJoystickPosition, GetType and ToString.
    As I mentioned before, something like a button is working and has all available options (ButtonPressed, ButtonReleased etc)

  5. #6 by Michael Dodaro on January 5, 2012 - 12:06 PM

    It sounds like a versioning problem. I’d suggest asking on the GHI Electronics forum: http://www.tinyclr.com/forum/

    • #7 by Harry Brown on January 5, 2012 - 1:25 PM

      Ok, will do. Thanks for your help – I’ll let you know if I find a solution!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: