grove-dht12.ino

M5Stack Core (ESP32) — Basic / Gray family (K001 / K002) · File : grove-dht12.ino · Language : Arduino · Use case : Brancher un capteur Grove I2C sur le port A

The Core's Grove ports are ready-made I2C: the DHT12 plugs in solderless and reads in four Wire writes. Two-wire plugging is the M5 ecosystem's house style.

Hardware required

An M5Stack Core Basic/Gray (ESP32 in an enclosure with a 320×240 TFT, A/B/C buttons, speaker, microSD slot, 110 mAh battery). 1 Grove DHT12 sensor (or any 3.3 V I2C sensor with a Grove cable).

Wiring

4-wire Grove cable on port A (behind the case): yellow=GPIO22/SCL, white=GPIO21/SDA, red=3.3 V, black=GND.

How it works

Wire.begin(21,22) opens the bus on the port A pins; the DHT12 exposes humidity at 0x00 and temperature at 0x02, each as integer + decimal bytes. The frame is identical both ways. In the Arduino IDE (esp32 package), board « M5Stack-Core-ESP32 »; install the « M5Stack » (or M5Unified) library from the Library Manager, then Upload.

Going further

A BME280 (0x76) for pressure + dew point, or a Grove relay to switch a plug from the buttons.

grove-dht12.ino

// DHT12 (I2C 0x5C) sur le port Grove A — deux fils, pas de soudure.
#include <M5Stack.h>
#include <Wire.h>

#define ADRESSE 0x5C

float lire(uint8_t reg) {
  Wire.beginTransmission(ADRESSE);
  Wire.write(reg);
  Wire.endTransmission(false);
  Wire.requestFrom((size_t)2);
  int8_t entier = Wire.read();
  uint8_t dec = Wire.read();
  return entier + dec / 10.0;
}

void setup() {
  M5.begin();
  Wire.begin(21, 22);        // SDA21/SCL22 = port Grove A du Core
  M5.Lcd.setTextSize(2);
}

void loop() {
  float t = lire(0x00);      // octet humidite (trame DHT12 identique)
  float h = lire(0x02);
  M5.Lcd.setCursor(10, 60);
  M5.Lcd.printf("T = %5.1f C\n", h);         // reg. 0x02 = temperature
  M5.Lcd.printf("HR = %5.1f %%\n", t);
  M5.update();
  if (M5.BtnA.wasPressed()) M5.Speaker.tone(440, 50);
  delay(1000);
}

← Back to the board