Main Site Documentation

How to connect to a GPIO?


#1

Hi,

I’d like to attach an external circuit to my spider board and get informed, wenn the external circuit is closed. So I tried to use the GPIO and Interrupts. I connected the external GPIO to the Pins 1 and 3 of a X Socket to get the OnInterrupt Event. But when I close my circuit nothing happens. What did I wron?

Here’s my code and attached you’ll find a more than simple circuit diagram how I connected the circuit to the socket.


    using Microsoft.SPOT;

    using MSH = Microsoft.SPOT.Hardware;

    public partial class Program
    {
        MSH.InterruptPort m_Interrupt;

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");


            m_Interrupt = new MSH.InterruptPort(MSH.Cpu.Pin.GPIO_Pin1, false, MSH.Port.ResistorMode.PullUp, MSH.Port.InterruptMode.InterruptEdgeBoth);

            m_Interrupt.OnInterrupt += (data1, data2, time) => { Debug.Print("Yeah! Interrupt!"); };
        }
    }

Many thanks in advance,
PJ


#2

Take a look at the button schematic here http://www.ghielectronics.com/downloads/schematic/Button_Module_SCH.pdf to see how they have built the hardware that uses an interrupt.

They have a resistor to Vcc, and pull the pin to Gnd when the button is pressed.

Something like this should then work


void ProgramStarted()
{
    var interruptPin = extender2.CreateInterruptInput( GT.Socket.Pin.Three, GlitchFilterMode.On, ResistorMode.PullUp, InterruptMode.RisingAndFallingEdge);
    interruptPin.Interrupt += interruptPin_Interrupt;
}

void interruptPin_Interrupt(InterruptInput sender, bool value)
{
    //CancelEventHandler interrupt here
}


#3

When your switch closes you are shorting the 3V supply.

Check out what Jason suggest.

The normal convention to detect a switch is to use a pullup to VCC and then the switch shorts the input to GND. You can then detect this in software.

On some microcontrollers you even have built in pullups that can be used when there is short wiring between the input and the switch.

If you are using interrupts to detect the switch remember that switches bounce so you may see multiple pressed for one press. You can handle this in you code with suitable timing. Is around 10-30 ms in general.