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

Controlling GPIO via UART on BLE

Hello

I am looking to make a program on nrf52 in which i send various values from nrf UART app on a mobile, and each value controls a LED on a GPIO. For example, sending 1 turns on the LED, 2 turns off, 3 blinks. Can you please provide me of a reference link which can help me in making this program.

Basically on UART BLE program, i want to include something like if the value in UART buffer=1, set gpio pin x= 1.

thanks in advance and for your time and help

  • For turning LED ON/OFF you can use something like that:

    ...   
      #define LED1 17
      #define LED2 18
    
      int main(void)
      {
    
          nrf_gpio_cfg_output(LED1);
          nrf_gpio_cfg_output(LED2);
    
          while (true)
          {
             nrf_gpio_pin_clear(LED1); //LED ON
             nrf_gpio_pin_set(LED2);    //LED OFF
             nrf_delay_ms(500);
             nrf_gpio_pin_set(LED1);     //LED ON
             nrf_gpio_pin_clear(LED2);   //LED OFF
             nrf_delay_ms(500);
          }
      }
    

    For blinking LED with BLE UART: use example ...\SDK12.2_full\examples\ble_peripheral\ble_app_uart\

    And modify this function:

    static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
    {
        #define LED1 17
        for (uint32_t i = 0; i < length; i++)
        {
            if (p_data[i]=='1')
            {
               nrf_gpio_cfg_output(LED1);
               nrf_gpio_pin_clear(LED1);
            }
            if (p_data[i]=='0')
            {
               nrf_gpio_cfg_output(LED1);
               nrf_gpio_pin_set(LED1);
            }
            while (app_uart_put(p_data[i]) != NRF_SUCCESS);
        }
        while (app_uart_put('\r') != NRF_SUCCESS);
        while (app_uart_put('\n') != NRF_SUCCESS);
    }
    

    Then when you send 1 the LED turn ON and when you send 0 the LED turn OFF

  • this is a general led blink program, i want to link it with BLE-UART, which is quite complicated. Do you know how to link it with value received from UART via BLE?

  • I've just tested the example in my updated answer, it works, then you can rewrite to have something clean. In Android I use nRF UART 2.0 from nordicsemi.

  • Why don't you just send the pinmap as binary as a single character then access the hardware directly

    Hint, look at the source code for nrf_gpio_pin_set() and see how it writes to the GPIO hardware reg

  • thanks for your reply

    the program works!!

    But I have some questions:

    1. p_data[i]=='1'  accepts only single digit value, what to do if i want more options than just 0-9? 
      
    2. In my project, i want to use some of the 0-255 values sent from a mobile app, to choose and change the intensity of 3 leds of different colors. how to do that? I guess, i have to use PWM function from the examples and embedd it in this program of BLE UART? Please let me know if it is the correct approach or any other thing should be done instead of UART.?
      

    thanks again for your help

Related