How to program the LEDs and the buttons onboard of Arduino Primo

Description

In the Arduino Primo there are three Leds (L9, USER2, BLE) and two button (USER1 and USER2) onboard that can be programmed and managed from users.
In particular in this example will be shown how to command the USER2 and BLE LEDs using the USER1 and USER2 buttons onboard of the Arduino Primo

Hardware

Circuit

Connect the board to the PC via an USB data cable
Arduino Primo Leds and buttons

Code

In the Arduino Primo are been defined the LEDs and the Buttons onboards.
Look the table:

DEFINE

Pin

Note

LED_BUILTIN 

9

LED_BUILTIN is the L9 Led onboard

BUZZER 

35

Manage the buzzer onboard

USER1_BUTTON

34

used also in some Low Power library functionalities

USER2_BUTTON

44

It isn’t possible to attach an Interrupt

USER2_LED

38

User2 Led onboard

BLE_LED

40

BLE led onboard


Now upload the below code to command the BLE led with the USER1 button and the USER2 Led with the USER2 button.

Warning:
Use the Arduino IDE 1.8.x and check that you have already downloaded the corresponding platform (Arduino NRF52 Boards) from the Board Manager
 
// variables:
int button1State = 0; // variable for reading the USER1 button status
int button2State = 0; // variable for reading the USER2 button status

void setup() {
// initialize the USER2 and BLE_LED as an output:
pinMode(USER2_LED, OUTPUT);
pinMode(BLE_LED, OUTPUT);
// turn off the LEDs:
digitalWrite(USER2_LED, LOW);
digitalWrite(BLE_LED, LOW);
// initialize the USER1 and USER2 as an input:
pinMode(USER1_BUTTON, INPUT);
pinMode(USER2_BUTTON, INPUT);
}

void loop() {
// read the states of the buttons:
button1State = digitalRead(USER1_BUTTON);
button2State = digitalRead(USER2_BUTTON);
// check if the buttons are pressed.
// if the button1State is LOW (push the USER1 button):
if (button1State == LOW) {
// turn BLE_LED on:
digitalWrite(BLE_LED, HIGH);
} else {
// turn BLE_LED off:
digitalWrite(BLE_LED, LOW);
}
// if the button2State is LOW (push the USER2 button):
if (button2State == LOW) {
// turn USER2_LED on:
digitalWrite(USER2_LED, HIGH);
} else {
// turn USER2_LED off:
digitalWrite(USER2_LED, LOW);
}
}

Output

Push the USER1 button onboard of Arduino Primo to turn on the BLE Led:

BLE LED on

Instead press the USER2 button to turn on the USER2 Led.

USER2 Led on