Reading a Hall Sensor - worked in VS2010 but not VS2013 (C#)

Hello everyone,

I searched and couldn’t find anything relating to reading a Hall sensor (except for a very old post which won’t help me). This is the sensor I’m referring to: Hall High Sensitivity Sensor Switch -

My current setup (using a Hydra, Cerberus, and a couple other main boards around the house) have a very basic premise-read the voltage from a Hall sensor, and send a signal to an RFPipe if it drops below a certain number. My code worked in Visual Studio 2010; but the same code doesn’t work for VS 2013. The underlying SDK seems to have changed and I can’t find the documentation to help me.

Here’s my VS2010 code - any thoughts? This seems like an easy objective; but not easy enough for me. The guts of the program is when the ticker.tick event fires, that’s where it is supposed to read the input voltage.

As it stands, when massaged into VS 2013 it doesn’t know what the Socket declaration is, and also the “GTI.AnalogInput” doesn’t exist. (because the GTI.Gadteteer.Interface doesn’t exist)

Any help?

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Hardware;

using Gadgeteer;
using Gadgeteer.Networking; 
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using GTI = Gadgeteer.Interfaces;
using Gadgeteer.Modules.GHIElectronics;

using Gadgeteer.Modules;

namespace FrontDoorModule
    public partial class Program

        private GT.Timer _pollingTimer;
        private GTI.AnalogInput analogInput;
        private bool disarmButton;

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
            disarmButton = false;

            Socket socket = Gadgeteer.Socket.GetSocket(3, true, null, null);
            analogInput = new GTI.AnalogInput(socket, Socket.Pin.Three, null);

            _pollingTimer = new GT.Timer(1000);
            _pollingTimer.Tick += new GT.Timer.TickEventHandler(_pollingTimer_Tick);



            button.ButtonPressed += new GTM.GHIElectronics.Button.ButtonEventHandler(button_ButtonPressed);
            rfPipe.DataReceived +=new GTM.IngenuityMicro.RfPipe.RfPipeReceivedHandler(rfPipe_DataReceived);


        void button_ButtonPressed(GTM.GHIElectronics.Button sender, GTM.GHIElectronics.Button.ButtonState state)
            // activate disarm variable
            disarmButton = true;

            // play disarm tone sequence
            Tunes.MusicNote note = new Tunes.MusicNote(Tunes.Tone.C5, 200);
            note = new Tunes.MusicNote(Tunes.Tone.C5, 200);

            // if button is pressed begin egress sequence

            // sleep for 2 minutes giving the person time to leave

            disarmButton = false;


        void _pollingTimer_Tick(GT.Timer timer)
            if (disarmButton == false)
                // check for voltage that is higher than 3.0
                if (analogInput.ReadVoltage() < 3)
                    // sleep for 250 milliseconds

                    // check voltage again - this was added as a failsafe to prevent false positives
                    if (analogInput.ReadVoltage() < 3)
                        rfPipe.SendData("fd opened");

                        Tunes.MusicNote note = new Tunes.MusicNote(Tunes.Tone.C5, 300);


                    } // end if second voltage read

                }  // end if first voltage read

            } // end if disarm is false

@ andre.m - Correct, went from 4.2 to 4.3. Thanks for pointing me in the right direction…found the doc and will examine.

Read over the doc and now I have more questions than I originally started with. This axiom seems to apply even in normal C# programming: Anything simple is hard to do; anything hard is easy to do.

I think GHI.Pins is the new way to do what I need to do (the old namespace I used is gone); and I found a bunch of posts related to that so I’ll have to spend a lot of time reading through them. I’m surprised I haven’t found anyone doing exactly what I’ve already done (with 4.3). But such is the path to learning.

There are a couple of changes in the Gadgeteer framework as well that you need to take into account.

Can I ask why you use an analog input for a digital signal ?

If you change to a digital input you can do away with this analog checking.


@ Dave McLaughlin -
Thanks Dave, I’m not partial to one way or another. At this point I’m just trying to get the idea to work.

I’m using a Break out module - with pin #5 hooked into the sensor’s signal pin. There wasn’t any documentation in the GHI Catalog part of the Break out module; I’m just looking to read from that pin. How would I achieve this basic task?

The current code you showed is just out of date for 4.3. The changes aren’t major on their own.

Socket socket = Gadgeteer.Socket.GetSocket(3, true, null, null);
analogInput = new GTI.AnalogInput(socket, Socket.Pin.Three, null);

Here you create a representation of the Gadgeteer “socket” and then use that to create an AnalogInput from pi3 of that socket.

To change that to 4.3’s methodology, you can refer to another post (like ) that highlights the “factory” approach:

GT.Socket yourSocket = GT.Socket.GetSocket(3, true, null, null);
GT.SocketInterfaces.DigitalInput yourButton = GT.SocketInterfaces.DigitalInputFactory.Create(yourSocket, GT.Socket.Pin.Three, GT.SocketInterfaces.GlitchFilterMode.On, GT.SocketInterfaces.ResistorMode.Disabled, null);  // or parameters as appropriate

(edited in text here, not QC’d or checked for syntax errors :slight_smile: )

Remember, the breakout module is almost an anti-Gadgeteer module, as you as an end user aren’t expected to code the above, an actual module designer would build that into their module driver. In the BoM’s scenario you want the flexibility to connect what you want, where, which is good but also bad in that you need to write more code.

hope that helps

@ Brett -
I understand now; the Break out Module is more of a “Here’s the Pins, use them as you see fit” - so how can there be documentation on something that is a DIY module? So it makes sense that there IS none.

I’ll be messing with this tonight and see what I can put together. Thanks!

1 Like