This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Read GPIO pin status through a characteristic.

Hi,

I am new to nrf and trying to make some new services and characteristics for my device. I followed the advertising, services and characteristics tutorial and was successfully able to make a new service and new characteristic.

But now I want to read the status of my D5 pin(P0.28 in nrf51822) using the same characteristics. I tried a lot of things but still not able to do so.

I am not getting what to change and where to change so that the particular GPIO is readable from the characteristic. Please help me. Thanks.

  • Should the pin be input or output? I'm not aware of any way you can read the GPIO state from the characteristics, but you can run a periodic timer that updates the characteristics value with the state of the pin at a given interval.

  • Hi jorgen, my gpio pin is acting as an input. If I'm not wrong then it means it will update the value but i couldn't be able to read it through the characteristic? Please help me in finding the solution. If there are some examples available which can help me regarding this stuff please help me!

  • If you followed the characteristics tutorial to the end, you should have a characteristics reporting temperature gathered from the IC at a regular interval. If you want to change the application to report state of a pin on this charactersistics instead, this is the changes you need to make:

    • Define the pin: #define INPUT_PIN 28

    • Configure pin as input in main(): nrf_gpio_cfg_input(INPUT_PIN , NRF_GPIO_PIN_NOPULL);

    • Change timer_timeout_handler() function to read pin state and pass this to the characteristic update function:


    static void timer_timeout_handler(void * p_context)
    {
       int32_t pin_state = 0;
       pin_state = nrf_gpio_pin_read(INPUT_PIN);
       our_temperature_characteristic_update(&m_our_service, &pin_state);
       nrf_gpio_pins_toggle(BSP_LED_2);
    }
    

    The characteristics should now be updated with the pin state on any change.

    If you want the characteristics to be only 1 byte long, and also report the correct pin state on initialization, you also have to change the following:

    • In our_char_add(), change attr_char_value.max_len, attr_char_value.init_len, value and attr_char_value.p_value to:

    attr_char_value.max_len  = 1;
    attr_char_value.init_len = 1;
    uint8_t value            = nrf_gpio_pin_read(INPUT_PIN);
    attr_char_value.p_value  = &value;
    

    • In our_temperature_characteristic_update(), change len to 1.

    Best regards,

    Jørgen

  • Hi @jorgen, I am doing as accordingly you instructed me. but nothing changes. please help me what should i need to change? Have you tested..is this working?

  • Yes, I have tested it. Have you remembered to enable notifications after connecting to the device?

Related