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

How do I control an external LED using nRf52832 ble board?

Hey,

I am trying to control an external LED using two nRF52832 boards. I am using "nRF5_SDK_13.0.0_04a0bfd\examples\ble_peripheral\experimental_ble_app_blinky" code for enabling & disabling LED 3 on the peripheral board. I have connected an external LED to board one which is acting as a peripheral device. I am trying to read the status of LED-3 (P0.02) and write that status on to pin P0.11. I used the pullup configuration for input from LED-3(P0.02).

#include "nrf_drv_gpiote.h"

void gpio_init(void) {

nrf_gpio_cfg_output(11);
nrf_gpio_cfg_input(2, NRF_GPIO_PIN_PULLUP);
	

}

int main(void) { gpio_init();

if(nrf_gpio_pin_read(2) == 0)
    {
        nrf_gpio_pin_write(11,1);
    }

}

These are the code lines I have added to the "experimental_ble_app_blinky".

The problem is the "nrf_gpio_pin_read" command reads only once.

I am able to enable & disable the external LED when I use the arduino Uno board (controlled through arduino).

void setup() { pinMode(0, INPUT); pinMode(8, OUTPUT); }

void loop() { if(digitalRead(0) == HIGH){ digitalWrite(8, LOW); } }

This is the code I used for the arduino. And I have shorted the pins P0.02 & P0.19 on the nordic board.

How do I enable the pins on the nRf52832 board wirelessly using bluetooth from another bluetooth connected device?

Thank you in advance.

  • Hi,

    This can be solved in several ways. One solution can be to set-up a app-timer, with a timeout-handler that reads the LED-pin, and then writes the status to pin 11 at fixed intervals. When you mentioned LED-3, is this the LED3 on the DK ? If so, that LED is connected to P0.19 on the DK. Also note that you don't need to initialize or configure the LEDs on the DK yourself, this is done in the function called leds_init().

    Here is one solution:

    Let’s define the timer and timeout interval we want:

    APP_TIMER_DEF(m_my_led_timer_id);                        /**< My LED timer. */
    #define MY_LED_TIMER_INTERVAL      APP_TIMER_TICKS(100)  /**< MY_LED_TIMER_INTERVAL interval (ticks). */
    

    The timout handler can look something like this:

    static void my_led_timeout_handler(void * p_context)
    {
        UNUSED_PARAMETER(p_context);
        if(nrf_gpio_pin_out_read(19) == 0)
        {
            nrf_gpio_pin_write(11,1);
        }
        else
        {
            nrf_gpio_pin_write(11,0);
        }
        
    }
    

    In main(),

    Lets first config pin 11 to be a output:

    nrf_gpio_cfg_output(11);
    

    and we can then create and start the timer like this:

    uint32_t err_code;
    
    err_code = app_timer_create(&m_my_led_timer_id,
                                APP_TIMER_MODE_REPEATED,
                                my_led_timeout_handler);
    APP_ERROR_CHECK(err_code);
    
     // Start application timers.
    err_code = app_timer_start(m_my_led_timer_id, MY_LED_TIMER_INTERVAL, NULL);
    APP_ERROR_CHECK(err_code);
    
Related