Altium
ESP32IOT Projects

XIAO ESP32C3 Bluetooth Tutorial, Range test, and Home Automation

XIAO ESP32C3 Bluetooth:

XIAO ESP32C3 Bluetooth

This is the Smallest ESP32C3 WiFi + Bluetooth development board. In my previous tutorial, I explained its technical specifications, it’s pinout, board installation using Arduino IDE, and I also practically demonstrated how to control an LED and how to make an IoT based temperature and Humidity monitoring system using the Adafruit IO and the ESP32C3 onboard WiFi. So, if you are just starting with the XIAO ESP32C3 then I highly recommend, you should read my getting started article on the XIAO ESP32C3 WiFi + Bluetooth Development Board.

Anyway, in today’s article, you will learn how to use XIAO ESP32C3 onboard Bluetooth with an Android application for controlling electrical appliances.

Besides this, I will also do a range test. And we will see if the XIAO ESP32C3 onboard Low Energy Bluetooth 5 and the WiFi/Bluetooth Antenna really increases the signal strength. The manufacturer claims that it has a maximum communication range of +100 meters. If it’s true then it means I can control my home appliances and other electrical loads even from outside; 100 meters is such a long distance. I am still going to be quite happy even if it gives me around 40 to 50 meters.

XIAO ESP32C3 Bluetooth

HC05 Bluetooth module never gave me such a long distance. And look at the size difference XIAO ESP32C3 is much smaller than the HC05 Bluetooth module. So, using the smallest ESP32C3 development board we can really reduce the project size and cost. Anyway, first I am going to share with you the final test results and afterward, I will explain everything else.



Altium Designer + Altium 365 + Octopart:

Arduino LoRa Free SMS

Altium 365 lets you hold the fastest design reviews ever. Share your designs from anywhere and with anyone with a single click. it’s easy, leave a comment tagging your teammate and they’ll instantly receive an email with a link to the design. Anyone you invite can open the design using a web browser. Using the browser interface, you’re able to comment, markup, cross probe, inspect, and more. Comments are attached directly to the project, making them viewable within Altium designer as well as through the browser interface. Design, share, and manufacture, all in the same space with nothing extra to install or configure. Connect to the platform directly from Altium Designer without changing how you already design electronics. Altium 365 requires no additional licenses and comes included with your subscription plan.

Get real-time component insights as you design with Octopart built into Altium 365. Octopart is the fastest search engine for electronic parts and gives you the most up-to-date part data like specs, datasheets, cad models, and how much the part costs at different amounts etc. Right in the design environment so you can focus on your designs. Start with Altium Designer and Activate Altium 365. Search for electronic parts on Octopart.

XIAO ESP32C3 Bluetooth

For demonstration purposes, I have connected 4 bulbs, which are connected to 4 relays. Besides lights, you can use any other 110/220Vac loads or use any other DC type loads.

If you want to use 110/220Vac supply, you must not forget to use protective gloves, because 110/220Vac can prove fatal. So, as far as possible you must ensure the presence of a friend or any companion while carrying on work on such projects. When the AC supply is ON, do not touch the relay module.

I have already paired the ESP32C3 onboard Bluetooth with my cell phone. For sending the control commands I am going to use nRF Connect application, which is available on Playstore. This application lets you scan, discover, and debug your Bluetooth Low Energy devices. So, let’s go ahead and open this application.


Enable the Bluetooth.

XIAO ESP32C3 Bluetooth

Click on BONDED.

XIAO ESP32C3 Bluetooth

You can see the name Home Automation, I defined it in the programming. If you want you can change this name. Anyway, click on the CONNECT button. And then click on the Unknown Service.

XIAO ESP32C3 Bluetooth

You can see the up and down arrows. Use the up arrow to send the control commands. Now, the cell phone App is ready and we can start sending the control commands. Simply click on the up arrow.

XIAO ESP32C3 Bluetooth

Write the command and press on the SEND button.

00 Turn off light 1
01 Turn on light 1
02 Turn off light 2
03 Turn on light 2
04 Turn off light 3
05 Turn on light 3
06 Turn off light 4
07 Turn on light 4

Here is a list of all the commands I am using to control all the 4 Bulbs. All these commands are defined in the programming, so if you want you can change these commands.

XIAO ESP32C3 Bluetooth

Its working great, I am able to turn ON and turn OFF these bulbs.


ESP32C3 Bluetooth Range Test:

Now, I am going to test the ESP32C3 Bluetooth communication range. My cousin is going to send the control signals from different locations. Right now my cousin is 26 meters away from the ESP32C3. I measured the distance using Google Earth.

XIAO ESP32C3 Bluetooth

At 26 meters its working great. For me, 26 meters is more than enough to control my Home appliances and other devices.

For this next test, I sent my cousin outside the house and now the distance between the cell phone and the XIAO ESP32C3 Onboard Bluetooth is 40 meters. I confirmed the distance on Google Maps. We were able to control the lights from 40 meters away. For practical demonstration watch video tutorial on my YouTube channel Electronic Clinic.

For the 3rd test, I told my cousin to control the same lights from his house which is around 80 meters away. But he wasn’t able to connect his cell phone with the XIAO ESP32C3 onboard Bluetooth. This is because of the obstacles like concrete walls. I am sure in an open area it’s going to hit 100+ meters. We also controlled the same lights from the ground floor and from other locations in my house. As per my needs and requirements, the XIAO ESP32C3 WiFi + Bluetooth development board is simply amazing.

Now, I am going to explain the circuit diagram and programming. So, without any further delay, let’s get started!!!

Amazon Links:

XIAO ESP32C3

4 channel relay module

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!


XIAO ESP32C3 Home Automation:

XIAO ESP32C3 Bluetooth

It’s just a basic circuit diagram. I am using digital pins 2, 3, 4, and 5 to control all the 4 relays.

XIAO ESP32C3 Bluetooth

I will be using my designed 4 channel relay module. These are 12V relays, that’s why I will be using a separate 12V power supply. Anyway, I connected everything as per the circuit diagram, and now let’s take a look at the programming.



XIAO ESP32C3 Home Automation Code:

#include <ArduinoBLE.h>

BLEService ledService(“19B10000-E8F2-537E-4F6C-D104768A1214”); // Bluetooth® Low Energy LED Service

// Bluetooth® Low Energy LED Switch Characteristic – custom 128-bit UUID, read and writable by central

BLEByteCharacteristic switchCharacteristic(“19B10001-E8F2-537E-4F6C-D104768A1214”, BLERead | BLEWrite);

const int Relay1 = D2;

const int Relay2 = D3;

const int Relay3 = D4;

const int Relay4 = D5;

int Rvalue; // received value from Bluetooth Application

void setup() {

  Serial.begin(9600); //Baudrate

  // Set all the Relays as output

  pinMode(Relay1, OUTPUT);

   pinMode(Relay2, OUTPUT);

     pinMode(Relay3, OUTPUT);

   pinMode(Relay4, OUTPUT);

  // begin initialization

  if (!BLE.begin()) {

    Serial.println(“starting Bluetooth® Low Energy module failed!”);

    while (1);

  }

  // set advertised local name and service UUID:

  BLE.setLocalName(“HOME Automation”);            // this will appear in the App search result.

  BLE.setAdvertisedService(ledService);

  // add the characteristic to the service

  ledService.addCharacteristic(switchCharacteristic);

  // add service

  BLE.addService(ledService);

  // set the initial value for the characeristic:

  switchCharacteristic.writeValue(0);

  // start advertising

  BLE.advertise();

  Serial.println(“BLE LED Peripheral”);

}

void loop() {

  // listen for Bluetooth® Low Energy peripherals to connect:

  BLEDevice central = BLE.central();

  // if a central is connected to peripheral:

  if (central) {

    Serial.print(“Connected to central: “);

    // print the central’s MAC address:

    Serial.println(central.address());

  while (central.connected()) {

        if (switchCharacteristic.written()) {

          Rvalue=switchCharacteristic.value(); // received value is stored in variable Rvalue.

          // we check the received commands and then accordingly control

          // all the relays.

          if (Rvalue==0) { 

            Serial.println(switchCharacteristic.value() );

            Serial.println(“Relay1 OFF”);

            digitalWrite(Relay1, LOW); // changed from HIGH to LOW      

          }

          else if(Rvalue==1){                             

            Serial.println(F(“Relay1 ON”));

            digitalWrite(Relay1, HIGH); // changed from LOW to HIGH    

          }    

          else  if (Rvalue==2) { 

            Serial.println(switchCharacteristic.value() );

            Serial.println(“Relay2 OFF”);

            digitalWrite(Relay2, LOW); // changed from HIGH to LOW      

          }

          else if(Rvalue==3){                             

            Serial.println(F(“Relay2 ON”));

            digitalWrite(Relay2, HIGH); // changed from LOW to HIGH    

          }

          else if (Rvalue==4) { 

            Serial.println(switchCharacteristic.value() );

            Serial.println(“Relay3 OFF”);

            digitalWrite(Relay3, LOW); // changed from HIGH to LOW      

          }

          else if(Rvalue==5){                             

            Serial.println(F(“Relay3 ON”));

            digitalWrite(Relay3, HIGH); // changed from LOW to HIGH    

          }

            if (Rvalue==6) { 

            Serial.println(switchCharacteristic.value() );

            Serial.println(“Relay4 OFF”);

            digitalWrite(Relay4, LOW); // changed from HIGH to LOW      

          } else if(Rvalue==7){                              

            Serial.println(F(“Relay4 ON”));

            digitalWrite(Relay4, HIGH); // changed from LOW to HIGH    

          }

        }

      }

    // when the central disconnects, print it out:

    Serial.print(F(“Disconnected from central: “));

    Serial.println(central.address());

  }

}

Its just a simple code. I have already commented most of the instructions so you won’t have any problem in understanding the code. Anyway, You will need to add the ArduinoBLE library. You can see I have defined pins for all the 4 relays. I have also defined a variable Rvalue which I am going to use to store the commands from the Bluetooth application.

Inside the setup() function, you can see I have activated the Serial communication for debugging purposes. Once your code is ready, then you can deactivate this and you can remove all the serial.print() lines. Because, you won’t need to print anything on the Serial monitor.

I was talking about this name, this name will appear in the cell phone search results. So, if you want to use another name, just go ahead and change this.

Rest of the code is to receive the data from Bluetooth App and then to control the relays as per the commands. So, that’s all about the programming. Upload this code into the XIAO_ESP32C3, if you are not able to find the XIAO_ESP32C3 in the boards list, then you need to read my previous article on the XIAO ESP32C3 WiFi + Bluetooth Dev board.


Watch Video Tutorial

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published.

Back to top button