Skip to content

Blinking onboard LED

The SODAQ Mbili board has two user programmable on-board LEDs. These are LED1 (green) and LED2 (red). The onboard position of these can be seen here: SODAQ Mbili Features.

These two LEDs can be controlled using the output from the digital pins they are connected to. In this example we will demonstrate how to switch on and off these LEDs using the same methods used for digital I/O (output only). The same methods can be used for controlling the output to components attached to other digital pins.

Note

LED1 & LED2 are the constants which define which digital pin each LED is attached to.

Required Components

  • SODAQÐ’ Mbili Board

Required Libraries

  • none

Hardware Setup

Turn on the SODAQ Mbili board, compile and upload the following sketch from the Arduino IDE onto the device. You should see the green and red LEDs alternately light up for one second at a time.

Sketch

//How long to activate each LED
#define DELAY_TIME 1000

void setup()
{
    //LED1
    pinMode(LED1, OUTPUT);
    digitalWrite(LED1, LOW);

    //LED2
    pinMode(LED2, OUTPUT);
    digitalWrite(LED2, LOW);
}

void loop()
{
    //Switch LED1 on then off again after DELAY_TIME (ms)
    digitalWrite(LED1, HIGH);
    delay(DELAY_TIME);
    digitalWrite(LED1, LOW);

    //Repeat for LED2
    digitalWrite(LED2, HIGH);
    delay(DELAY_TIME);
    digitalWrite(LED2, LOW);
}

Sketch Code Breakdown

Globals

Here we define a constant which is used to control how long each LED is turned on for. The specified value is in milliseconds and so each LED remains lit for one second at a time.

//How long to activate each LED
#define DELAY_TIME 1000

Setup()

Here we set each of the digital pins for the LEDs to OUTPUT mode using the pinMode() method. Additionally, we turn them off initially using the digitalWrite() method and the constant LOW.

void setup()
{
    //LED1
    pinMode(LED1, OUTPUT);
    digitalWrite(LED1, LOW);

    //LED2
    pinMode(LED2, OUTPUT);
    digitalWrite(LED2, LOW);
}

Loop()

First we switch LED1 on using digitalWrite() and the constant HIGH. We then call the method delay() which returns after the specified number of milliseconds (DELAY_TIME) have elapsed. Finally, we switch off LED1 with another call to digitalWrite() this time using the constant LOW. The same process is repeated for LED2.

void loop()
{
    //Switch LED1 on then off again after DELAY_TIME (ms)
    digitalWrite(LED1, HIGH);
    delay(DELAY_TIME);
    digitalWrite(LED1, LOW);

    //Repeat for LED2
    digitalWrite(LED2, HIGH);
    delay(DELAY_TIME);
    digitalWrite(LED2, LOW);
}