Objectives
- Use the serial monitor (
Serial.begin(115200)) as the debug console for the M5Stack. - Read an analog input on the Grove PORT.B connector (black), connected to G26/G36 — these are ADC1 channels, usable even when Wi-Fi is active (unlike ADC2).
- Display the raw value (0-4095) on both the serial port and the screen.
Hardware
- 1 M5Stack Core + USB-C cable.
- 1 Grove analog sensor (potentiometer, light sensor, etc.) on PORT.B, or simply a G36 pin connected via a potentiometer to 3.3 V and GND.
- Warning: 3.3 V logic — never inject 5 V into an analog pin.
Complete code
#include <M5Unified.h>
const int PIN_CAPTEUR = 36; // PORT.B (Grove noir), ADC1_CH0
uint16_t moyenne {
uint32_t s = 0;
for (int i = 0; i < 16; i++) { s += analogRead(PIN_CAPTEUR); delay(2); }
return s / 16;
}
void setup {
Serial.begin(115200);
auto cfg = M5.config;
M5.begin(cfg);
analogReadResolution(12); // 0..4095
M5.Display.setTextSize(2);
Serial.println("Console serie M5Stack — 115200");
}
void loop {
uint16_t v = moyenne;
float mv = v * 3300.0f / 4095.0f; // ADC1 non linéaire en bout de plage
Serial.printf("brut=%u ~%u mV\n", v, (unsigned)mv);
M5.Display.fillRect(0, 40, 320, 60, TFT_BLACK);
M5.Display.setCursor(10, 45);
M5.Display.printf("ADC G36 : %u", v);
delay(250);
}
Expected verification
- The serial monitor (115200) displays two new lines per second with a raw value that changes when the sensor is stimulated (light, potentiometer).
- The screen displays the same value in large characters.
- At ground (pin disconnected or connected to GND), the value drops near 0; near 3.3 V it exceeds 3000.
Common errors
- Wrong serial baud rate: the USB-serial bridge (CP2104 or CH9102F) operates at 115200; a different baud rate displays noise.
- Choosing an ADC2 pin (e.g., G25, shared with the speaker): ADC2 is unavailable as soon as Wi-Fi is running, and G25 is already used by the speaker.
- Expecting encoder precision: the classic ESP32 ADC is non-linear; hence the averaging over 16 readings.
- Forgetting
delay or vTaskDelay in loop: the watchdog and Wi-Fi do not handle blocking loops well. - Wiring 5 V to G36: the ESP32 operates at a maximum of 3.3 V.