Help Read GPIO pin in Python

Hello everyone. I have been attempting to read the analog signal from the GPIO pin of the MetamotionR board. I am using the pymetawear library on a raspberry pi.

This is the sesnor I am attempting to read:

Reading the sesnor from the GPIO pin with the MetaWear android app does work. However when I try to test it in python I am stuck.

I have been modifying the script in the pymetawear modules folder. Here is my code:

Note, this file is a copy of the the file from the modules folder within a clone of the pymetawear library, in which I am editing. I have been playing around with the code around line 100 but nothing seems to work.

There error message is as follows:

Traceback (most recent call last):
  File "", line 217, in <module>
  File "", line 197, in notifications
    super(GpioModule, self).notifications(data_handler(callback))
  File "/usr/local/lib/python3.7/dist-packages/pymetawear/modules/", line 135, in notifications
    data_signal = self.data_signal
  File "", line 100, in data_signal
    return libmetawear.mbl_mw_gpio_get_pin_monitor_data_signal(self.board, '0')
ctypes.ArgumentError: argument 2: <class 'TypeError'>: wrong type

Any help is welcome.


  • I have been looking through that example, thank you. I managed to get passed my initial issue, but still can't access the signal.

    This is my code:

    # usage: python [mac1] [mac2] ... [mac(n)]
    from __future__ import print_function
    from mbientlab.metawear import MetaWear, libmetawear, parse_value
    from mbientlab.metawear.cbindings import *
    from time import sleep
    from threading import Event
    from ctypes import byref, create_string_buffer
    import platform
    import sys
    if sys.version_info[0] == 2:
        range = xrange
    class State:
        def __init__(self, device):
            self.device = device
            self.samples = 0
            self.callback = FnVoid_VoidP_DataP(self.data_handler)
        def data_handler(self, ctx, data):
            print("%s -> %s" % (self.device.address, parse_value(data)))
            self.samples+= 1
    states = []
    d = MetaWear("CC:3E:36:3A:4B:50")
    print("Connected to " + d.address)
    for s in states:
        print("Configuring device")
        libmetawear.mbl_mw_settings_set_connection_parameters(s.device.board, 7.5, 7.5, 0, 6000)
        an_signal = libmetawear.mbl_mw_gpio_get_analog_input_data_signal(s.device.board, 2, GpioAnalogReadMode.ADC)
        libmetawear.mbl_mw_datasignal_subscribe(an_signal, None, s.callback)
        libmetawear.mbl_mw_gpio_start_pin_monitoring(s.device.board, 2)
    for s in states:
        libmetawear.mbl_mw_gpio_stop_pin_monitoring(s.device.board, 2)
        an_signal = libmetawear.mbl_mw_gpio_get_analog_input_data_signal(s.device.board, 2, GpioAnalogReadMode.ADC)
    print("Total Samples Received")
    for s in states:
        print("%s -> %d" % (s.device.address, s.samples))

    The output is:

    Services disconvered
    Characteristics discovered
    Descriptors found
    Connected to CC:3E:36:3A:4B:50
    Configuring device
    CC:3E:36:3A:4B:50 -> 401
    Total Samples Received
    CC:3E:36:3A:4B:50 -> 1

    There should be some output here. Any thoughts?

  • This looks all correct. You only read the signal once so you got one sample. Please read our tutorials, some signal need timers to be read more than once:

Sign In or Register to comment.