Beyond The Built-In LED


The built-in LED example that comes with the Arduino IDE is a great way to familiarise ourselves with the Arduino. But the Arduino can do so much more than that, it is our doorway to the world of IoT.
In this tutorial we will be looking at a switch connected to an Arduino Nano 33 IoT board (acting as an IoT connected doorbell for example). So in this scenario, the doorbell is a Bluetooth LE connected device. In order to be notified when someone presses the doorbell we need to indicate to the Nano board that yes, tell me when someone presses the doorbell.

High level overview

Above is a high level diagram of the components that will be interacting with one another. We have a doorbell switch connected to a digital input of the Arduino Nano 33 IoT board. The Nano 33 IoT has built-in Bluetooth LE so we will be setting it up as a BLE server so that a BLE client e.g. nRF Connect app can connect to it. If you are not familiar with Bluetooth low energy but keen to get familiarise with it check out my article Getting To Know Bluetooth Low Energy.

What we will need:

Arduino Nano 33 IoT pin layout
The circuit’s physical connection and wiring

The above diagram shows 3 main components:

  • Arduino Nano 33 IoT board
    • Provides the 3.3V to drive the digital input high when the doorbell is pressed.
    • Provides a digital input pin to detect when the doorbell is pressed.
  • Doorbell (push button switch)
    • Creates an open circuit when not pressed (pin state = LOW)
    • Creates a close circuit when pressed (pin state = HIGH)
  • 10k resister
    • Acts as a pull-down resister to avoid false triggers.
    • Ensures digital pin 2 is logically 0 when the doorbell is not pressed.
    • Ensures digital pin 2 is logically 1 when the doorbell is pressed.

Given the above setup all we want to do at this stage is to make sure the circuit works:

  • When the doorbell is pressed, the built-in LED will be on.
  • When the doorbell is released, the built-in LED will be off.

So the sketch that we need to load onto the Arduino is as follow:

#define DOORBELL_PIN 2u

static bool isPressed;

void setup()
{
   pinMode(LED_BUILTIN, OUTPUT);
   pinMode(DOORBELL_PIN, INPUT);
   isPressed = false;
}

void loop()
{
   if (digitalRead(DOORBELL_PIN) == HIGH)
   {
      if (!isPressed)
      {
         digitalWrite(LED_BUILTIN, HIGH);
         isPressed = true;
      }
   }
   else
   {
      if (isPressed)
      {
         digitalWrite(LED_BUILTIN, LOW);
         isPressed = false;
      }
   }
}
LED toggles reflecting the state of switch

Setting up the Bluetooth service

To interact with the BLE module on the Nano we will use the ArduinoBLE library.

Installing ArduinoBLE library via VS Code

Now update our Arduino sketch to include the doorbell BLE service.

#include <ArduinoBLE.h>

#define DOORBELL_PIN 2u

static bool isPressed;

BLEService doorBellService("8158b2fd-94e4-4ff5-a99d-9a7980e998d7");
BLEByteCharacteristic doorBellCharacteristic("8158b2fe-94e4-4ff5-a99d-9a7980e998d7", BLERead | BLENotify);

void setup()
{
   pinMode(LED_BUILTIN, OUTPUT);
   pinMode(DOORBELL_PIN, INPUT);
   isPressed = false;

   if (!BLE.begin())
   {
      /* Just keep looping until BLE module is up and running. */
      while (1);
   }

   /* BLE module is up and running, now add our service and characteristic to it. */
   BLE.setLocalName("Awesome Doorbell");

   doorBellCharacteristic.writeValue(isPressed);
   doorBellService.addCharacteristic(doorBellCharacteristic);
   BLE.addService(doorBellService);

   BLE.setAdvertisedService(doorBellService);
   BLE.advertise();
}

void loop()
{
   BLE.poll();

   if (digitalRead(DOORBELL_PIN) == HIGH)
   {
      if (!isPressed)
      {
         digitalWrite(LED_BUILTIN, HIGH);
         isPressed = true;
         doorBellCharacteristic.writeValue(isPressed);
      }
   }
   else
   {
      if (isPressed)
      {
         digitalWrite(LED_BUILTIN, LOW);
         isPressed = false;
         doorBellCharacteristic.writeValue(isPressed);
      }
   }
}

Download the sketch to the board and we should be ready to go.

At this point we should be able to use a BLE Client to connect to our “Awesome Doorbell”.

Awesome Doorbell as shown on nRF Connect

After connecting to the doorbell, we can dig into the characteristic details and see that when:

  • Doorbell is not pressed, value is 0x00.
  • Doorbell is pressed, value is 0x01.
Awesome Doorbell status update

There we have it, the ArduinoBLE library makes it really easy to get Bluetooth up and running on our Arduino devices.

Categories: Arduino, Bluetooth

1 comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: