Vibration sensors and LEDs

Hi everyone! I am trying to create a project using 6 piezo vibration sensors and 6 LEDs. The goal is for piezo1 to turn on LED1 for 1 second when a vibration is sensed, piezo2 blinks LED2, etc. I borrowed the wiring diagram from another project and adapted it for this.

The piezos all work great and I can see in the serial monitor the various levels of input. But no LEDs light up. Code and fritzing diagram below. I am using a Mega2560, though the diagram is with an Uno. Same pin numbers, though. Please help! TIA :slight_smile:

// Define the pin numbers
const int piezoPins[6] = {A0, A1, A2, A3, A4, A5}; // Analog pins for piezo sensors
const int ledPins[6] = {2, 3, 4, 5, 6, 7};        // Digital pins for LEDs

void setup() {
  // Initialize LED pins as outputs
  for (int i = 0; i < 6; i++) {
    pinMode(ledPins[i], OUTPUT);
    digitalWrite(ledPins[i], LOW); // Ensure LEDs are off initially
  }
  
  // Initialize piezo sensor pins as inputs
  for (int i = 0; i < 6; i++) {
    pinMode(piezoPins[i], INPUT);
  }
  
  // Start serial communication
  Serial.begin(9600);
}

void loop() {
  for (int i = 0; i < 6; i++) {
    int sensorValue = analogRead(piezoPins[i]); // Read the sensor value
    
    // Print sensor value to Serial Monitor
    Serial.print("Sensor ");
    Serial.print(i);
    Serial.print(": ");
    Serial.println(sensorValue);
    
    if (sensorValue > 10) { // Threshold value to determine vibration (Adjust as needed)
      digitalWrite(ledPins[i], HIGH); // Turn on the corresponding LED
      delay(500);
      digitalWrite(ledPins[i], LOW); // Turn on the corresponding LED
    } else {
      digitalWrite(ledPins[i], LOW); // Turn off the LED
    }
  }
  
  delay(100); // Small delay to avoid rapid toggling
}

Maybe wrong resistor values or wiring etc.. Try the blink example with led(s) to confirm it.

I think you have it wired correctly, but a schematic would be much easier to follow than a wiring diagram.

Also, I didn’t see the rest of your code, specifically where you declared any of the LED pins as outputs. Could you share that part?

Good news: the wiring and code work wonderfully
Bad news: I'm an idiot and didn't have a ground in the right pin :woman_facepalming:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.