This circuit involves an ESP32 microcontroller, a BMP388 sensor, and a servo motor. The ESP32 receives a float value via Bluetooth, converts it to an angle, and rotates the servo to that angle. It also reads temperature and air pressure from the BMP388 sensor and sends these values via Bluetooth. The ESP32 then enters deep sleep mode for 2 minutes to conserve power.
Seeed Studio XIAO ESP32C6
Adafruit BMP388
Servo
/*
* This Arduino Sketch for the ESP32 microcontroller performs the following tasks:
* 1. Receives a float value (0-1) via Bluetooth, converts it to an angle (0-180),
* and rotates a connected servo to that angle.
* 2. Reads temperature and air pressure from a BMP388 sensor and sends these
* values via Bluetooth.
* 3. Shuts off unnecessary peripherals and enters deep sleep for 2 minutes.
*/
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP3XX.h>
#include <Servo.h>
#include <BluetoothSerial.h>
#define BMP_SCK 19
#define BMP_SDI 18
#define BMP_CS 21
#define BMP_SDO 20
#define SERVO_PIN 22
Adafruit_BMP3XX bmp;
Servo myServo;
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32_BMP_Servo");
myServo.attach(SERVO_PIN);
if (!bmp.begin_SPI(BMP_CS, BMP_SCK, BMP_SDI, BMP_SDO)) {
Serial.println("Could not find a valid BMP388 sensor, check wiring!");
while (1);
}
bmp.setTemperatureOversampling(BMP3_OVERSAMPLING_8X);
bmp.setPressureOversampling(BMP3_OVERSAMPLING_4X);
bmp.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3);
bmp.setOutputDataRate(BMP3_ODR_50_HZ);
}
void loop() {
if (SerialBT.available()) {
float receivedValue = SerialBT.readString().toFloat();
int angle = receivedValue * 180;
myServo.write(angle);
}
if (!bmp.performReading()) {
Serial.println("Failed to perform reading :(");
return;
}
float temperature = bmp.temperature;
float pressure = bmp.pressure / 100.0;
SerialBT.print("Temperature: ");
SerialBT.print(temperature);
SerialBT.print(" C, Pressure: ");
SerialBT.print(pressure);
SerialBT.println(" hPa");
delay(1000);
esp_sleep_enable_timer_wakeup(120 * 1000000);
esp_deep_sleep_start();
}
This code initializes the BMP388 sensor and the servo motor, sets up Bluetooth communication, and enters a loop where it reads a float value via Bluetooth to control the servo's angle. It also reads temperature and pressure from the BMP388 sensor and sends these values via Bluetooth before entering deep sleep mode for 2 minutes.