🛠 Console série : afficher les capteurs embarqués

Arduino Esplora — Board Fiche · debutant · Schéma : IMG-esplora-debutant-3-esplora-console-serie-capteurs.svg — Esplora reliée par câble USB à un ordinateur affichant le moniteur série avec slider, lumière, température et accéléromètre.

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

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

  1. No data displayed: The Esplora is already open in another program (serial plotter in another tab); close it and reset the board.
  2. Expecting UART on pins 0/1: On native USB boards like the Esplora, Serial is the USB CDC; the real hardware UART is Serial1, not accessible on standard pins.
  3. Raw temperature: The internal formula (raw*500/1024)-50 is an uncalibrated approximation — do not compare it with a medical thermometer.
  4. Drifting accelerometer: Zero offsets are hardcoded per axis; for a precise project, record your own values on a flat surface and subtract them.
  5. 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

← Back to the board 🛠 All tutorials