detection_choc.ino

Arduino Nicla Vision · File : detection_choc.ino · Language : Arduino · Use case : Détecter un choc et allumer la LED rouge

Standalone shock detector: when the acceleration vector norm crosses 2 g, the red LED (pin 23) lights for two seconds and the event is timestamped on the console. Ideal for watching a parcel, a machine or a forced gate.

Hardware

An Arduino Nicla Vision (STM32H7 M7+M4, 23×23 mm) with GC2145 camera, LSM6DSOX IMU, PDM mic, ToF (VL53Lx), ESP32 Wi-Fi/BLE module and RGB LED (LEDR/G/B). The board alone; the on-board LSM6DSOX IMU delivers acceleration past 100 Hz. Test by tapping the table under the board.

Wiring

No wiring. For an external trigger, an actuator on a connector logic pin with separate supply (3.3 V logic pins).

How it works

The norm sqrt(ax²+ay²+az²) sits near 1 g at rest; a shock pushes it past the threshold. The 2 g threshold avoids vibration false positives, and the timed LED feedback confirms visually.

Running it

Programmed from the Arduino IDE (ArduinoCore-mbed, NICLA_VISION target) over USB-C; Camera, PDM, Arduino_LSM6DSOX, WiFiNINA and ArduinoBLE libraries provided.

Variants

Lower the threshold for sensitivity, count shocks per hour, or wake the board on the IMU activity interrupt for battery life.

detection_choc.ino

// Choc-détecteur : si la norme de l'accélération dépasse 2 g,
// la LED rouge s'allume 2 s et l'événement est tracé.

#include <Arduino_LSM6DSOX.h>

unsigned long dernierChoc = 0;

void setup() {
  Serial.begin(115200);
  pinMode(LEDR, OUTPUT);          // LEDR = broche 23 (macros du core)
  if (!IMU.begin()) { while (1) { ; } }
  Serial.println("Surveillance des chocs (seuil 2 g)");
}

void loop() {
  float ax, ay, az;
  if (IMU.accelerationAvailable()) {
    IMU.readAcceleration(ax, ay, az);
    float norme = sqrtf(ax * ax + ay * ay + az * az);

    if (norme > 2.0) {            // 2 g = choc ou chute rapide
      dernierChoc = millis();
      Serial.print("CHOC norme=");
      Serial.println(norme);
    }
  }

  // LED à retour d'état : 2 s après le dernier choc
  bool actif = millis() - dernierChoc < 2000;
  digitalWrite(LEDR, actif);
  delay(5);                       // ~200 Hz : assez rapide pour un choc
}

← Back to the board