The built-in LED example that comes with the Arduino IDE is a great way to familiarise with the Arduino platform. But the Arduino can do so much more than that, it’s a doorway to the world of IoT. This blog post will describe the process of connecting a switch to an Arduino Nano 33 IoT board (e.g a BLE doorbell solution).
High level overview
Above is a high level diagram of the components that will be interacting with one another. The doorbell switch is connected to a digital input of the Arduino Nano 33 IoT board so that BLE clients (e.g. nRF Connect) can get notified of the doorbell events.
Arduino Nano 33 IoT pin layout
Circuit’s physical connection and wiring
The above diagram shows 3 main components:
Given the above setup what we want to do at this stage is to make sure the circuit works:
So the sketch needed 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
To interact with the BLE module on the Nano, the ArduinoBLE library is a great option and is easy to use.
Installing ArduinoBLE library via VS Code
Now update the 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 arduino board and it should be ready to go.
At this point we should be able to use a BLE Client (e.g. nRF Connect) to connect to the 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:
Awesome Doorbell status update
There we have it, the ArduinoBLE library makes it really easy to get Bluetooth up and running on Arduino devices.