ESP32C3-Based Multi-Sensor Data Logger with Wi-Fi and SD Card Storage
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to collect data from various sensors including the BME/BMP280, HMC5883L compass, MAX30102, and GPS NEO 6M. The collected data is recorded to an SD card and sent over WiFi using API calls. The circuit is powered by a 3.7V LiPo battery and controlled by an XIAO ESP32C3 microcontroller.
Component List
HMC5883L Compass
- Description: 3-axis digital magnetometer
- Pins: DRDY, SDA, SCL, GND, VCC
BME/BMP280
- Description: Temperature, pressure, and humidity sensor
- Pins: GND, SCL, SDA, VIN
3.7V LiPo Battery
- Description: Rechargeable lithium polymer battery
- Pins: +Ve, GND
Resistor (4700 Ohms)
- Description: Resistor with 4700 Ohms resistance
- Pins: pin1, pin2
MAX30102
- Description: Pulse oximeter and heart-rate sensor
- Pins: VIN, SDA, SCL, GND, RD, IRD, INT
GPS NEO 6M
- Description: GPS module
- Pins: VCC, RX, TX, GND
SD SDHC
- Description: SD card module
- Pins: 3V3, CS, MOSI, CLK, MISO, GND
XIAO ESP32C3
- Description: Microcontroller with WiFi and Bluetooth capabilities
- Pins: GPIO2/A0/D0, GPIO3/A1/D1, GPIO4/A2/D2, GPIO5/A3/D3, GPIO6/SDA/D4, GPIO7/SCL/D5, GPIO21/Tx/D6, 5V, GND, 3V3, GPIO10/MOSI/D10, GPIO9/MISO/D9, GPIO8/SCK/D8, GPIO20/Rx/D7
Wiring Details
HMC5883L Compass
- VCC connected to 3.3V power rail
- GND connected to ground rail
- SDA connected to XIAO ESP32C3 GPIO6/SDA/D4
- SCL connected to XIAO ESP32C3 GPIO7/SCL/D5
BME/BMP280
- VIN connected to 3.3V power rail
- GND connected to ground rail
- SDA connected to XIAO ESP32C3 GPIO6/SDA/D4
- SCL connected to XIAO ESP32C3 GPIO7/SCL/D5
3.7V LiPo Battery
- +Ve connected to XIAO ESP32C3 5V pin
- GND connected to ground rail
Resistor (4700 Ohms)
- pin1 connected to 3.3V power rail
- pin2 connected to SDA and SCL lines
MAX30102
- VIN connected to 3.3V power rail
- GND connected to ground rail
- SDA connected to XIAO ESP32C3 GPIO6/SDA/D4
- SCL connected to XIAO ESP32C3 GPIO7/SCL/D5
GPS NEO 6M
- VCC connected to 3.3V power rail
- GND connected to ground rail
- RX connected to XIAO ESP32C3 GPIO21/Tx/D6
- TX connected to XIAO ESP32C3 GPIO20/Rx/D7
SD SDHC
- 3V3 connected to 3.3V power rail
- GND connected to ground rail
- CS connected to XIAO ESP32C3 GPIO10/MOSI/D10
- MOSI connected to XIAO ESP32C3 GPIO10/MOSI/D10
- CLK connected to XIAO ESP32C3 GPIO8/SCK/D8
- MISO connected to XIAO ESP32C3 GPIO9/MISO/D9
XIAO ESP32C3
- 3V3 connected to 3.3V power rail
- GND connected to ground rail
- 5V connected to 3.7V LiPo Battery +Ve
- GPIO6/SDA/D4 connected to SDA lines of HMC5883L, BME/BMP280, and MAX30102
- GPIO7/SCL/D5 connected to SCL lines of HMC5883L, BME/BMP280, and MAX30102
- GPIO21/Tx/D6 connected to GPS NEO 6M RX
- GPIO20/Rx/D7 connected to GPS NEO 6M TX
- GPIO10/MOSI/D10 connected to SD SDHC CS and MOSI
- GPIO9/MISO/D9 connected to SD SDHC MISO
- GPIO8/SCK/D8 connected to SD SDHC CLK
Documented Code
Code for XIAO ESP32C3
/*
* This Arduino Sketch collects data from various sensors (BME/BMP280, HMC5883L,
* MAX30102, GPS NEO 6M) and records the data to an SD card. Additionally, it
* sends the data over WiFi using API calls.
*/
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <WiFi.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Adafruit_HMC5883_U.h>
#include <Adafruit_MAX30105.h>
#include <TinyGPS++.h>
// Pin definitions
#define SD_CS_PIN 10
// Sensor objects
Adafruit_BME280 bme;
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
Adafruit_MAX30105 max30105;
TinyGPSPlus gps;
// WiFi credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// API endpoint
const char* server = "http://your_api_endpoint.com/data";
void setup() {
Serial.begin(115200);
Wire.begin();
// Initialize SD card
if (!SD.begin(SD_CS_PIN)) {
Serial.println("SD card initialization failed! Retrying...");
delay(1000);
if (!SD.begin(SD_CS_PIN)) {
Serial.println("SD card initialization failed again. Check connections.");
return;
}
}
Serial.println("SD card initialized.");
// Initialize BME280
if (!bme.begin(0x76)) {
Serial.println("BME280 initialization failed! Check sensor connections.");
return;
}
Serial.println("BME280 initialized.");
// Initialize HMC5883L
if (!mag.begin()) {
Serial.println("HMC5883L initialization failed! Check sensor connections.");
return;
}
Serial.println("HMC5883L initialized.");
// Initialize MAX30105
if (!max30105.begin(Wire, I2C_SPEED_FAST)) {
Serial.println("MAX30105 initialization failed! Check sensor connections.");
return;
}
Serial.println("MAX30105 initialized.");
// Initialize GPS
Serial1.begin(9600, SERIAL_8N1, 20, 21);
// Connect to WiFi
WiFi.begin(ssid, password);
unsigned long startAttemptTime = millis();
while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 10000) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Failed to connect to WiFi. Check credentials and network.");
return;
}
Serial.println("Connected to WiFi.");
}
void loop() {
// Read BME280 data
float temperature = bme.readTemperature();
float pressure = bme.readPressure() / 100.0F;
float humidity = bme.readHumidity();
// Read HMC5883L data
sensors_event_t event;
mag.getEvent(&event);
float heading = event.magnetic.heading;
// Read MAX30105 data
long irValue = max30105.getIR();
// Read GPS data
while (Serial1.available() > 0) {
gps.encode(Serial1.read());
}
float latitude = gps.location.lat();
float longitude = gps.location.lng();
// Create data string
String dataString = String(temperature) + "," + String(pressure) + "," +
String(humidity) + "," + String(heading) + "," +
String(irValue) + "," + String(latitude) + "," +
String(longitude);
// Write data to SD card
File dataFile = SD.open("