Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Bluetooth Message Box
This project receives messages and displays them on an LCD screen.
You can use it as a family message board, reminding family members who haven’t left yet to remember to take their keys.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Elite Explorer Kit |
300+ |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
- |
|
Wiring
Schematic
Generate the UUID
UUIDs play a crucial role in BLE communication, ensuring the uniqueness of devices and accuracy of data exchange between them. You need to customize UUIDs to create your own BLE services and characteristics to meet specific application requirements. (Here, we need to create a characteristic that supports text input.)
Use the Online UUID Generator Tool to create UUIDs unique to you to avoid UUID conflicts.
Generate two version 4 UUIDs.
Copy them and replace the two UUIDs in your code.
#define SERVICE_UUID "uuid1" #define CHARACTERISTIC_UUID "uuid2"
Install the Library
ArduinoBLE.h
: Used for handling Bluetooth Low Energy (BLE) communication.
LiquidCrystal_I2C.h
: Used to control a 16x2 character LCD screen with an I2C interface.
Run the Code
Note
You can open the file
07_lightblue_lcd.ino
under the path ofelite-explorer-kit-main\iot_project\07_lightblue_lcd
directly.Or copy this code into Arduino IDE.
Note
To install the library, use the Arduino Library Manager to search for and install “ArduinoBLE” and “LiquidCrystal I2C”.
How to play?
To interact with the services and characteristics created in this sketch, you should use a generic Bluetooth® Low Energy central app like LightBlue (available for iOS and Android) or nRF Connect (for Android).
Let’s use LightBlue as an example to demonstrate how to control Arduino’s LED via Bluetooth.
Download the LightBlue app from the App Store (for iOS) or Google Play (for Android).
Connecting Arduino with Your Smartphone via Bluetooth
Go to your Bluetooth settings and locate the device named “UNO R4 BLE.” Connect to it.
Interacting with Arduino via Bluetooth Using LightBlue
Launch LightBlue and tap on the Bonded tab at the bottom of the interface. Here, you’ll see a list of BLE devices that your smartphone has previously paired with. Locate UNO R4 BLE and tap CONNECT.
Once connected, you’ll gain access to detailed information about the Bluetooth device. Scroll down to find your service UUID and your Characteristic UUID.
Tap on the Characteristic. You’ll notice that this Characteristic is both readable and writable, allowing you to both read from and write to it.
Continue scrolling to the “Data format” section and change it to UTF-8 String. Enter text in the text box and click write. The message you entered will appear on the LCD.
How it works?
Here are the main parts of the code and explanations of their functions:
Include the required libraries:
ArduinoBLE.h
: Used for handling BLE communication.Wire.h
: Used for I2C communication.LiquidCrystal_I2C.h
: Used to control a 16x2 character LCD screen with I2C interface.
Define a BLE service and a BLE characteristic:
Use the
BLEService
class to define a BLE service and assign it a unique UUID.Use the
BLECharacteristic
class to define a BLE characteristic, assign it a unique UUID, and give it read (BLERead
) and write (BLEWrite
) permissions.Create a character array
stringValue
as the initial value of the characteristic.
Initialize a 16x2 character LCD screen (LCD):
Use the
LiquidCrystal_I2C
class to initialize an LCD, specifying the I2C address (0x27) and the number of rows and columns (16x2).Turn on the backlight of the LCD, clear the screen, move the cursor to the start of the first row, and display “Bluetooth LCD.”
Perform initialization in the
setup()
function:Initialize serial communication.
Initialize the BLE module, and if initialization fails, enter an infinite loop.
Set the local name and service UUID for the BLE peripheral.
Add the BLE characteristic to the BLE service.
Start advertising the BLE service so that central devices can discover and connect to it.
Initialize the LCD.
The
loop()
function:Check if there is a central device connected to the BLE peripheral by calling
BLE.central()
. If a central device is connected, enter the connection handling logic.In the connected state, check if data has been written to the BLE characteristic by checking
boxCharacteristic.written()
.If data has been written, get the length of the written data using
boxCharacteristic.valueLength()
and create a byte arraybuffer
to store the written data.Use
boxCharacteristic.readValue()
to read data from the BLE characteristic into thebuffer
.Add a null character
'\0'
to the end of thebuffer
to convert it to a string.Print the received message to the serial monitor and display it on the LCD.
Continue waiting for the next central device to connect after the central device disconnects.