GPIO with C#


I just got my CPro metawear board, and I want to thank you guys for your amazing work.
I am using the C# wrapper to get accelerometer and gyroscope data, but I have some trouble when I tried to connect a push button to the GPIO pins and read its changes, so I was wondering if anyone have tried something like and could help me?



    1. How are you connecting the button to the board?
    2. What kind of trouble are you having with your setup?
    3. What have you tried so far?
    Assuming everything is connected properly, did you set a pull mode on the data pin?
  • Well, I have no problems in connecting the button to the board, but I have problems in making the C# wrapper of the gpio class (in C++ 0.3 API). I've tried to make the wrapper manually and adding it to the existing C# wrappers, but I've lost it before I could finish it.

    Do you know any tweak just to make this work, until you finish the .net library ?
  • edited March 2016
    I would define the wrappers in the C# class that is using them.  The provided C# wrapper files define the enums and structs for the various library functions.

    For example, if my MainPage class were using a gpio function, I would place the wrapper in the MainPage class.

    public sealed partial class MainPage : Page
        [DllImport("MetaWear.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr mbl_mw_gpio_start_pin_monitoring(IntPtr board, byte pin);
        public MainPage()

            this.NavigationCacheMode = NavigationCacheMode.Required;
  • Thanks for your response Eric, Now I able to send the GPIO commands to the board, but I am not able to read input from the pins. I've tried to use these function to read the input from the pin "mbl_mw_gpio_read_digital_input" and "mbl_mw_gpio_start_pin_monitoring" after setting the pull mode to up. 

    Could you please tell me how get the data, I'd be grateful if you could post a complete example on GPIO with C++ or C# (preferably) ?

  • You need to tell the board to stream data and setup a handler for the data.  If you haven't already, please read the section on sensor data on the C++ api wiki:

    You can retrieve signals for the digital data from functions in the gpio.h header file
  • I am able to retrieve the data using "mbl_mw_gpio_read_digital_input", but the monitor function does not respond to any change, I am using the CppAPI_v0.3 directly, and I've tried a few settings (pin_change_type and pull_mode) before start monitoring the data.

    So, is there any special settings for using "mbl_mw_gpio_start_pin_monitoring" ?
  • Nope, nothing special for mbl_mw_gpio_start_pin_monitoring.  I'll double check the implementation to see if anything is out of place.
  • I pushed a bug fix to address the pin monitoring, commit 3648fbf0.
  • I am sorry Eric, but it still does not responding to changes. I am using the latest firmware 1.2.1. I managed to get a similar functionality of pin monitoring using a timer and reading digital input as in the C++ API wiki, I am planning to go with the timer, but I don't know if it will consume the battery more than standard pin monitoring?
    Thanks for your effort and I hope you fix it soon.
  • @Eric

    I just want to let you know that I've been able to use pin monitoring, and there no issue in using it anymore.

    Thank You!

  • Good to hear!
This discussion has been closed.