You can use the classes of the Gadgeteer.Interfaces namespace directly in code, instead of inside a module driver. This example uses the Gadgeteer.Interfaces.DigitalOutput interface. The voltage on this logic circuit, in the high state, is enough to light a LED. Later we’ll use a relay in the same circuit to implement a more interesting application. (See also the Light Sensor, which uses the Gadgeteer.InterruptInput interface in a module driver.)
This Gadgeteer.Interfaces.DigitalOutput circuit can be tested on a breadboard connected to the GHI Electronics Fez Spider mainboard by an Extender module. The Extender module is used only to connect the logic circuit to the mainboard. We use socket 10, which supports type X functionality with 3.3 volts on pin 1, ground on pin 10, and the digital output on pin 4. The Seattle Robotics Society has some good descriptions of the kinds of circuits used in this scenario. In this example the digital state of the logic pin is written when the user pushes a .NET Gadgeteer button. The following video shows the breadboard circuit with the LED and pull-up resistor. The LED turns green during the ButtonPressed event and turns of during the ButtonReleased event.
Using the Gadgeteer.Interfaces.DigitalOutput Interface in Code
The following code segment shows declaration and instantiation of the GT.Interfaces.DigitalOutput interface and the Gadgeteer.Socket.
GT.Interfaces.DigitalOutput ledControl; GT.Socket socket = GT.Socket.GetSocket(10, true, null, null); ledControl = new GT.Interfaces.DigitalOutput(socket, GT.Socket.Pin.Four, false, null);
The interface requires a socket connection; this example uses socket number 10. The second Boolean parameter of the Gadgteteer.Socket.GetSocket method specifies that a socket error will be thrown if there is an incompatible module on the socket or the socket doesn’t exist. The interface is instantiated by the last line of code, using the previously created socket and assigning the logic pin enumeration: Gadgeteer.Socket.Pin.Four. The third parameter, false, indicates the initial state of the logic circuit as low. The last parameter is null because there is no module used; we’re using the interface directly instead of in a module driver.
The state of the ledControl interface indicates whether the logic circuit is high (true) or low (false). Use the the Gadgeteer.Interfaces.DigitalOutput.Write method to set the state, as in the following examples. The method is called in the ButtonPressed and ButtonReleased event handlers, first to set the state to high and light the LED, and then to set the state to low and turn off the LED.
void button_ButtonPressed(Button sender, Button.ButtonState state) { ledControl.Write(true); } void button_ButtonReleased(Button sender, Button.ButtonState state) { ledControl.Write(false); }
That’s it. The the following code block includes the complete Program.cs file.
using Microsoft.SPOT; using Microsoft.SPOT.Presentation; using Microsoft.SPOT.Presentation.Controls; using Microsoft.SPOT.Presentation.Media; using GT = Gadgeteer; using GTM = Gadgeteer.Modules; using Gadgeteer.Modules.GHIElectronics; namespace DigitalOut { public partial class Program { GT.Interfaces.DigitalOutput ledControl; void ProgramStarted() { GT.Socket socket = GT.Socket.GetSocket(10, true, null, null); ledControl = new GT.Interfaces.DigitalOutput(socket, GT.Socket.Pin.Four, false, null); button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed); button.ButtonReleased += new Button.ButtonEventHandler(button_ButtonReleased); Debug.Print("Program Started"); } void button_ButtonPressed(Button sender, Button.ButtonState state) { ledControl.Write(true); } void button_ButtonReleased(Button sender, Button.ButtonState state) { ledControl.Write(false); } } }
#1 by dumbledad on February 7, 2013 - 3:02 AM
Why are there three wires coming from your breadboard to the extender module and not just two?
#2 by Michael Dodaro on February 7, 2013 - 5:23 AM
3.3 volts on pin 1, ground on pin 10, and the digital output on pin 4
#3 by dumbledad on February 7, 2013 - 6:24 AM
I guess I’m confused about the breadboard layout. Is that a pull-up resistor? And if so is it needed? I think 3.3 volts -> resister -> LED -> pin4 should work as you can initialize the pin in code, and the Gadgeteer will hold it at 0 or 1.
#4 by Michael Dodaro on February 7, 2013 - 8:07 AM
No, it isn’t a pull-up. It is just a cludgy jumper.