accelerometre-tilt.ino
The three-axis accelerometer measures gravity; the library subtracts the rest offsets, so each axis reads about 0 when flat. Tilting the board changes the color — the sketch of a fall detector.
Hardware required
An Arduino Esplora (ATmega32U4, 5 V) with its onboard sensors: joystick, 4 buttons, slider, RGB LED, buzzer, light, temperature, sound and accelerometer. None.
Wiring
Nothing to wire.
How it works
readAccelerometer returns the value minus the calibrated zero-g (320/330/310); thresholds compare against ±150 to infer orientation. In the Arduino IDE, select « Arduino Esplora »; add the « Esplora » library from the Library Manager to reach the onboard sensors.
Going further
Compute an angle (atan2) for a spirit level, or detect shake via sudden changes on all three axes.
accelerometre-tilt.ino
// Accelerometre embarque : valeurs relatives au repos -> couleur.
#include <Esplora.h>
void setup() {
Serial.begin(9600);
}
void loop() {
int ax = Esplora.readAccelerometer(X_AXIS); // ~0 au repos
int ay = Esplora.readAccelerometer(Y_AXIS);
int az = Esplora.readAccelerometer(Z_AXIS);
Serial.print(ax); Serial.print('\t'); Serial.print(ay);
Serial.print('\t'); Serial.println(az);
if (az < -150) Esplora.writeRGB(255, 0, 0); // retournee : rouge
else if (ax > 150) Esplora.writeRGB(0, 255, 0); // penchee : vert
else Esplora.writeRGB(0, 0, 80); // a plat : bleu
delay(100);
}