Skip to content

Xiao ESP32C3

Set up for Arduino IDE

Ref. Getting Started with Seeed Studio XIAO ESP32C3

Additional Boards Manager URLs
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json

Board manager Espressif Systems
alt text

Test 1

Xiao ESP32C3 does not have an on-board LED, so check the operation using serial communication.

Ref. Seeed Studio XIAO ESP32C3の環境構築

/** 
 *  XIAO ESP32C3 Serial test
 */
void setup() {
  Serial.begin(115200);
  Serial.println("--- setup end ---");
}

int count = 1;

void loop() {
  Serial.println(count);
  count++;

  if (count > 20) {
    count = 1;
  }

  delay(1000);
}

Open Serial Monitor
alt text

Press Reset Button
alt text

Error

Ref. FAQ

Attention

Q1: My Arduino IDE is stuck when uploading code to the board You can first try to reset the board by clicking the RESET BUTTON once while the board is connected to your PC.
If that does not work,
(1) hold the BOOT BUTTON,
(2) connect the board to your PC while holding the BOOT button, and then
(3) release it to enter bootloader mode.

Test 2

Ref.Seeed Studio XIAO ESP32C3でBLE

void setup() {
  Serial.begin(115200);
  Serial.println("Starting ");
}

void loop() {
  float temp = temperatureRead();
  Serial.println(temp);
  delay(2000);
}

Ref. Blink the LED

alt text alt text

/ define led according to pin diagram
int led = D10;

void setup() {
  // initialize digital pin led as an output
  pinMode(led, OUTPUT);
}

void loop() {
  digitalWrite(led, HIGH);   // turn the LED on 
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off
  delay(1000);               // wait for a second
}

Scan Bluetooth devices

Ref. Scan Bluetooth devices

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>

int scanTime = 5; //In seconds
BLEScan* pBLEScan;

class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
      Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
    }
};

void setup() {
  Serial.begin(115200);
  Serial.println("Scanning...");

  BLEDevice::init("");
  pBLEScan = BLEDevice::getScan(); //create new scan
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
  pBLEScan->setInterval(100);
  pBLEScan->setWindow(99);  // less or equal setInterval value
}

void loop() {
  // put your main code here, to run repeatedly:
  BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
  Serial.print("Devices found: ");
  Serial.println(foundDevices.getCount());
  Serial.println("Scan done!");
  pBLEScan->clearResults();   // delete results fromBLEScan buffer to release memory
  delay(2000);
}
Without Antenna
Advertised Device: Name: , Address: ed:ba:b2:06:15:aa, manufacturer data: 4c0012020111, rssi: -99 
Devices found: 17
Scan done!

With Antenna (supporting communication over 100m with a U.FL antenna)
Ref. Feature

Advertised Device: Name: , Address: ed:ba:b2:06:15:aa, manufacturer data: 4c0012020111, rssi: -99 
Devices found: 49
Scan done!

XIAO ESP32C3 as Bluetooth server

Ref. XIAO ESP32C3 as Bluetooth server

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"


class MyCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pCharacteristic) {
      std::string value = pCharacteristic->getValue();

      if (value.length() > 0) {
        Serial.println("*********");
        Serial.print("New value: ");
        for (int i = 0; i < value.length(); i++)
          Serial.print(value[i]);

        Serial.println();
        Serial.println("*********");
      }
    }
};

void setup() {
  Serial.begin(115200);

  BLEDevice::init("MyESP32_tamiya");
  BLEServer *pServer = BLEDevice::createServer();

  BLEService *pService = pServer->createService(SERVICE_UUID);

  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setCallbacks(new MyCallbacks());

  pCharacteristic->setValue("Hello World");
  pService->start();

  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  pAdvertising->start();
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(2000);
}

Install LightBlue App

alt text alt text alt text alt text alt text alt text alt text

BLE Server and Client

BLE Server

/*
BLE Server
*/

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <Wire.h>


volatile uint8_t data[14]; //Array for storing data from sensors
volatile int16_t ax = 0;   //Output data (raw value)
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483d-36e1-4688-b7f5-ea07361b26a8"



  BLEServer *pServer;
  BLECharacteristic *pCharacteristic;




void setup() {
  Serial.begin(115200);
  Serial.println("Starting BLE work!");
  Wire.begin();//Start I2C communication

  Wire.beginTransmission(0x68);//Start the transmission process (0x68 is the sensor address)
  Wire.write(0x6b);            //Specify register “0x6b” (operation state variable)
  Wire.write(0x00);            //Specify 0x00 (ON)
  Wire.endTransmission();      //Finish sending

  BLEDevice::init("KANNAI_BLE");
  pServer = BLEDevice::createServer();
  BLEService *pService = pServer->createService(SERVICE_UUID);
  pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setValue("Hello, Neil");
  pService->start();
  // BLEAdvertising *pAdvertising = pServer->getAdvertising();  // this still is working for backward compatibility
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue
  pAdvertising->setMinPreferred(0x12);
  BLEDevice::startAdvertising();
  Serial.println("Characteristic defined! Now you can read it in your phone!");
}

void MPU_DATAGET() {
  Wire.beginTransmission(0x68); //Start the sending process
  Wire.write(0x3b);             //(Specify the beginning of the obtained value)
  Wire.endTransmission();       //Finish sending
  Wire.requestFrom(0x68, 14);   //Request data (14 bytes from 0x3b are 6 axis values)

  uint8_t i = 0;
  while (Wire.available()) {
    data[i++] = Wire.read();//Load data
  }


  ax = (data[0] << 8) | data[1];//Concatenate Low and High to get the value

}


void loop() {
  // put your main code here, to run repeatedly:
  MPU_DATAGET();
  Serial.print("ax: ");
  Serial.println(ax);  

  // Convert sensor data to string
  String sensorData = String(ax);
  pCharacteristic->setValue(sensorData.c_str());
  //pCharacteristic->setValue(ax.c_str()); 
  delay(2000);
}

BLE_Client

/*
BLE_Client
 */

#include <BLEDevice.h>
#include <BLEScan.h>
//
//// The remote service we wish to connect to.
//static BLEUUID serviceUUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
//// The characteristic of the remote service we are interested in.
//static BLEUUID    charUUID("beb5483e-36e1-4688-b7f5-ea07361b26a8");

// The remote service we wish to connect to.
static BLEUUID serviceUUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
// The characteristic of the remote service we are interested in.
static BLEUUID    charUUID("beb5483d-36e1-4688-b7f5-ea07361b26a8");

//// The remote service we wish to connect to.
//static BLEUUID serviceUUID("BA914E58-CE1A-4A9B-9ACA-0703DA6D790E");
//// The characteristic of the remote service we are interested in.
//static BLEUUID    charUUID("BA911805-CE1A-4A9B-9ACA-0703DA6D790E");

static boolean doConnect = false;
static boolean connected = false;
static boolean doScan = false;
static BLERemoteCharacteristic* pRemoteCharacteristic;
static BLEAdvertisedDevice* myDevice;

static void notifyCallback(
  BLERemoteCharacteristic* pBLERemoteCharacteristic,
  uint8_t* pData,
  size_t length,
  bool isNotify) {
    Serial.print("Notify callback for characteristic ");
    Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
    Serial.print(" of data length ");
    Serial.println(length);
    Serial.print("data: ");
    Serial.write(pData, length);
    Serial.println();
}

class MyClientCallback : public BLEClientCallbacks {
  void onConnect(BLEClient* pclient) {
  }

  void onDisconnect(BLEClient* pclient) {
    connected = false;
    Serial.println("onDisconnect");
  }
};

bool connectToServer() {
    Serial.print("Forming a connection to ");
    Serial.println(myDevice->getAddress().toString().c_str());

    BLEClient*  pClient  = BLEDevice::createClient();
    Serial.println(" - Created client");

    pClient->setClientCallbacks(new MyClientCallback());

    // Connect to the remove BLE Server.
    pClient->connect(myDevice);  // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)
    Serial.println(" - Connected to server");
    pClient->setMTU(517); //set client to request maximum MTU from server (default is 23 otherwise)

    // Obtain a reference to the service we are after in the remote BLE server.
    BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
    if (pRemoteService == nullptr) {
      Serial.print("Failed to find our service UUID: ");
      Serial.println(serviceUUID.toString().c_str());
      pClient->disconnect();
      return false;
    }
    Serial.println(" - Found our service");


    // Obtain a reference to the characteristic in the service of the remote BLE server.
    pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
    if (pRemoteCharacteristic == nullptr) {
      Serial.print("Failed to find our characteristic UUID: ");
      Serial.println(charUUID.toString().c_str());
      pClient->disconnect();
      return false;
    }
    Serial.println(" - Found our characteristic");

    // Read the value of the characteristic.
    if(pRemoteCharacteristic->canRead()) {
      std::string value = pRemoteCharacteristic->readValue();
      Serial.print("The characteristic value was: ");
      Serial.println(value.c_str());
    }

    if(pRemoteCharacteristic->canNotify())
      pRemoteCharacteristic->registerForNotify(notifyCallback);

    connected = true;
    return true;
}
/**
 * Scan for BLE servers and find the first one that advertises the service we are looking for.
 */
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
 /**
   * Called for each advertising BLE server.
   */
  void onResult(BLEAdvertisedDevice advertisedDevice) {
    Serial.print("BLE Advertised Device found: ");
    Serial.println(advertisedDevice.toString().c_str());
//advertisedDevice.haveServiceUUID()
//Serial.println(serviceUUID.toString().c_str());
//    BLEUUID service = advertisedDevice.getServiceUUID();
//    Serial.print("Have ServiceUUI: "); Serial.println(service.toString().c_str());

    // We have found a device, let us now see if it contains the service we are looking for.
    //if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID)) {
    if (advertisedDevice.isAdvertisingService(serviceUUID)) {
      BLEDevice::getScan()->stop();
      myDevice = new BLEAdvertisedDevice(advertisedDevice);
      doConnect = true;
      doScan = true;

    } // Found our server
  } // onResult
}; // MyAdvertisedDeviceCallbacks


void setup() {
  Serial.begin(115200);
  Serial.println("Starting Arduino BLE Client application...");
  BLEDevice::init("");

  // Retrieve a Scanner and set the callback we want to use to be informed when we
  // have detected a new device.  Specify that we want active scanning and start the
  // scan to run for 20 seconds.
  BLEScan* pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setInterval(1349);
  pBLEScan->setWindow(449);
  pBLEScan->setActiveScan(true);
  pBLEScan->start(180, false);//5sec -> 180sec
} // End of setup.


// This is the Arduino main loop function.
void loop() {

  // If the flag "doConnect" is true then we have scanned for and found the desired
  // BLE Server with which we wish to connect.  Now we connect to it.  Once we are 
  // connected we set the connected flag to be true.
  if (doConnect == true) {
    if (connectToServer()) {
      Serial.println("We are now connected to the BLE Server.");
    } else {
      Serial.println("We have failed to connect to the server; there is nothin more we will do.");
    }
    doConnect = false;
  }

  // If we are connected to a peer BLE Server, update the characteristic each time we are reached
  // with the current time since boot.
  if (connected) {
    //String newValue = "Sensor Value: " + String(millis()/1000);
    //Serial.println("Setting new characteristic value to \"" + newValue + "\"");

    //added by TAKE
    // Read the value of the characteristic.
    if(pRemoteCharacteristic->canRead()) {
      std::string value = "Sensor Value: " + pRemoteCharacteristic->readValue();
      Serial.print("The characteristic value was: ");
      Serial.println(value.c_str());
    }

    // Set the characteristic's value to be the array of bytes that is actually a string.
    //pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
  }else if(doScan){
    BLEDevice::getScan()->start(0);  // this is just example to start scan after disconnect, most likely there is better way to do it in arduino
  }

  delay(1000); // Delay a second between loops.
} // End of loop


Last update: April 27, 2024