🛠 Console série : afficher les capteurs embarqués
Objectives
Use the native USB serial port of the ATmega32u4 to spy on the Esplora's onboard sensors: linear potentiometer (slider), light sensor, temperature, joystick, and 3-axis accelerometer. You will understand that Serial here is NOT the UART on pins 0/1 but the virtual CDC port created by the microcontroller itself, and that the accelerometer is the only sensor connected directly to the ADC (A5/A11/A6), while the others go through the multiplexer.
Hardware
- Arduino Esplora + micro-USB cable.
- Arduino IDE with the Esplora library.
- No external wiring.
Complete Code
/* Affiche tous les capteurs de l'Esplora sur le moniteur série.
Serial = port USB CDC natif de l'ATmega32u4 (pas les broches 0/1). */
#include <Esplora.h>
void setup {
Serial.begin(9600);
Serial.println("Esplora — lecture des capteurs embarques");
}
void loop {
int slider = Esplora.readSlider; // 0..1023
int light = Esplora.readLightSensor; // 0..1023
int tempC = Esplora.readTemperature(DEGREES_C); // entier en degres C
int sound = Esplora.readMicrophone; // 0..1023
int joyX = Esplora.readJoystickX; // -512..511
int joyY = Esplora.readJoystickY;
int ax = Esplora.readAccelerometer(X_AXIS); // offsets zero-g 320/330/310
int ay = Esplora.readAccelerometer(Y_AXIS);
int az = Esplora.readAccelerometer(Z_AXIS);
Serial.print("slider="); Serial.print(slider);
Serial.print(" lum="); Serial.print(light);
Serial.print(" T="); Serial.print(tempC); Serial.print("C");
Serial.print(" son="); Serial.print(sound);
Serial.print(" joy=("); Serial.print(joyX);
Serial.print(","); Serial.print(joyY); Serial.print(")");
Serial.print(" acc=("); Serial.print(ax);
Serial.print(","); Serial.print(ay);
Serial.print(","); Serial.print(az); Serial.println(")");
delay(250);
}
Expected Verification
Open the serial monitor at 9600 baud. Slide the potentiometer: the "slider" value follows from 0 to 1023. Cover the light sensor with your hand: "lum" drops. Hold the board between your fingers: "T" rises slowly (the library's conversion is integer-based, not accurate to ~0.5 °C). Tilt the board: the X/Y axes of the accelerometer vary around the idle offsets coded as 320/330/310 in the library.
Common Errors
- No data displayed: The Esplora is already open in another program (serial plotter in another tab); close it and reset the board.
- Expecting UART on pins 0/1: On native USB boards like the Esplora,
Serialis the USB CDC; the real hardware UART isSerial1, not accessible on standard pins. - Raw temperature: The internal formula
(raw*500/1024)-50is an uncalibrated approximation — do not compare it with a medical thermometer. - Drifting accelerometer: Zero offsets are hardcoded per axis; for a precise project, record your own values on a flat surface and subtract them.
- Concluding "it doesn't work" too quickly: The monitor must be opened AFTER startup; otherwise you miss the header, but measurements still arrive continuously.
Other tutorials for this board
Code examples for this board
joystick-couleur.inoArduinojeu-boutons.inoArduinoslider-luminosite.inoArduinothermometre-couleur.inoArduinosonometre.inoArduinoaccelerometre-tilt.inoArduino