Created
January 17, 2025 00:11
-
-
Save ridercz/aee3ecc86ebf398a017a24ccf0fc5963 to your computer and use it in GitHub Desktop.
M5Stack CoreS3 + ENV unit demo for Arduino serial plotter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "M5CoreS3.h" | |
#include "M5UnitENV.h" | |
SHT3X sht3x; | |
QMP6988 qmp; | |
void setup() | |
{ | |
auto cfg = M5.config(); | |
CoreS3.begin(cfg); | |
// Initialize serial port | |
Serial.begin(115200); | |
// Initialize display | |
CoreS3.Display.setTextSize(2); | |
CoreS3.Display.fillScreen(TFT_BLACK); | |
CoreS3.Display.setTextColor(TFT_WHITE, TFT_BLACK); | |
CoreS3.Display.setCursor(0, 0); | |
CoreS3.Display.println("Initializing..."); | |
// Serial.println("Initializing..."); | |
// Initialize QMP6988 sensor | |
if (!qmp.begin(&Wire, QMP6988_SLAVE_ADDRESS_L, G2, G1)) | |
{ | |
CoreS3.Display.println("QMP6988 init error!"); | |
Serial.println("QMP6988 init error!"); | |
while (true) | |
; | |
} | |
// Initialize SHT3X sensor | |
if (!sht3x.begin(&Wire, SHT3X_I2C_ADDR, G2, G1)) | |
{ | |
CoreS3.Display.println("SHT3X init error!"); | |
Serial.println("SHT3X init error!"); | |
while (true) | |
; | |
} | |
CoreS3.Display.println("Initialization done"); | |
// Serial.println("Initialization done"); | |
} | |
void loop() | |
{ | |
// Wait to update sensor data | |
delay(1000); | |
// Define variables to store sensor data | |
float temp1 = 0.0f, temp2 = 0.0f, hum = 0.0f, alt = 0.0f, press = 0.0f; | |
// Read temperature and humidity from SHT3X sensor | |
if (sht3x.update()) | |
{ | |
temp1 = sht3x.cTemp; // Celsius | |
hum = sht3x.humidity; // % rH | |
} | |
// Read temperature, altitude and pressure from QMP6988 sensor | |
if (qmp.update()) | |
{ | |
alt = qmp.altitude; // meters | |
temp2 = qmp.cTemp; // Celsius | |
press = qmp.pressure; // Pa | |
} | |
// Clear screen | |
CoreS3.Display.fillScreen(TFT_BLACK); | |
CoreS3.Display.setCursor(0, 0); | |
// Print data from SHT3X sensor | |
CoreS3.Display.setTextColor(TFT_YELLOW, TFT_BLACK); | |
CoreS3.Display.println("SHT3X Sensor:"); | |
CoreS3.Display.setTextColor(TFT_WHITE, TFT_BLACK); | |
CoreS3.Display.printf(" Temp.: %.2f C\n", temp1); | |
CoreS3.Display.printf(" Humidity: %.2f %% rH\n", hum); | |
CoreS3.Display.println(""); | |
// Serial.printf("SHT3X Sensor: Temp.: %.2f C, Humidity: %.2f %% rH\n", temp1, hum); | |
Serial.printf("SHT3X_Temp:%.2f,SHT3X_Hum:%.2f,", temp1, hum); | |
// Print data from QMP6988 sensor | |
CoreS3.Display.setTextColor(TFT_YELLOW, TFT_BLACK); | |
CoreS3.Display.println("QMP6988 Sensor:"); | |
CoreS3.Display.setTextColor(TFT_WHITE, TFT_BLACK); | |
CoreS3.Display.printf(" Temp.: %.2f C\n", temp2); | |
CoreS3.Display.printf(" Altitude: %.2f m\n", alt); | |
CoreS3.Display.printf(" Pressure: %.2f Pa\n", press); | |
CoreS3.Display.println(""); | |
// Serial.printf("QMP6988 Sensor: Temp.: %.2f C, Altitude: %.2f m, Pressure: %.2f Pa\n", temp2, alt, press); | |
Serial.printf("QMP6988_Temp:%.2f\n", temp2); | |
// Print battery status | |
CoreS3.Display.setTextColor(TFT_YELLOW, TFT_BLACK); | |
CoreS3.Display.println("Battery:"); | |
CoreS3.Display.setTextColor(TFT_WHITE, TFT_BLACK); | |
float voltage = CoreS3.Power.getBatteryVoltage() / 1000.0f; | |
if (voltage < 3) | |
{ | |
// If the battery voltage is less than 3V, the battery is either dead or not connected | |
CoreS3.Display.println(" Failed or not connected."); | |
} | |
else | |
{ | |
CoreS3.Display.printf(" Voltage: %.2f V\n", voltage); | |
CoreS3.Display.printf(" Level: %d %%\n", CoreS3.Power.getBatteryLevel()); | |
if (CoreS3.Power.isCharging() == true) | |
{ | |
CoreS3.Display.println(" Charging: Yes"); | |
} | |
else | |
{ | |
CoreS3.Display.println(" Charging: No"); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment