Piezo triggered LED lights (mounted on an acoustic drum kit) HELP!

Hello there everyone,

I have been working on my final uni project which involves triggering LEDs from Piezo elements connected to a drum set (so each time I hit a drum an LED strip gets activated and deactivated).

I have been doing this on an Arduino Uno, a breadboard and have been using this code on LED testers at the moment, however, the lights don’t seem to be responding as consistently to the code as Id hope.

I am a complete beginner in coding and was recommended this arduino Knock! (https://www.arduino.cc/en/Tutorial/Knock). It seems to be triggering the LED tester ok, however when triggered once, the lights do not turn off for a couple of seconds and the serial print is printing hundreds of hits when I only tapped it once or sometimes I haven't even touched it at all.

I have amended the code with some guidance from people in another forum and below is the code I am currently using:

int knockSensor = 0;               

byte val = 0;

int statePin = LOW;

int THRESHOLD = 100;

void setup() {

pinMode(8, OUTPUT); 



void loop() {

 val = analogRead(knockSensor);     

 if (val >= THRESHOLD) {

 statePin = HIGH; //Change state to on
 digitalWrite(8, statePin); // set pin on
 delay(100); //wait 100 miliseconds
 Serial.println("Knock!"); // Write to serial.
 statePin = LOW; //Change state to Off
 digitalWrite(8, statePin); // set pin Off

 delay(5);  // we have to make a delay to avoid overloading the serial port


The lights are reacting to each tap a little better however it is still quite temperamental. I have set the delay to really low and the threshold to 100 as this seems to be where it is most responsive.

I have also bought some solid colour LED strips which only came with two wires for an output (red and white/greyish colour) and i am unsure what I need/where I need to plug them into for them to work as part of the circuit. They are 12v and at the moment I am powering the Arduino Uno with USB power.
(At the moment the Piezo is connected to a breadboard and a 1 megohm resistor).

I am planning to connect two more LED strips so any advice would be extremely helpful!!!

First of all, please go read the forum guide in the sticky post. Then modify your post above and add in the code tags. There are other useful hints & tips in the guide like how to post links and pictures, and what else to include in your question to allow people to help you, like links to components, schematics etc.

To drive 12V strips you will need some transistors. We can advise what type when we know more about the strips you are using.

So if your getting more than like (.49) volts on your A0 it runs the loop.

What kind of knock sensor is actually going to A0?

So you don’t lose responsiness , use millis counter to control how often you serial print.
That way you can loop without delay and still not flood serial output.

I have also bought some solid colour LED


Your program doesn't look too bad.

Take out delay(5);

...Your program makes an instantaneous read, and if you're not over the threshold at that exact-instant it does nothing but wait for 5 milliseconds. ...There's a very good chance it will miss the drum hit because it's delaying (not reading) during the hit. ...Most of the time, your program is delaying and not reading the input.

You want your loop to run as fast as possible so it can read as frequently as possible. There should be no delays while waiting for the next hit!

The 100mS delay after you detect a hit is good because you want the LED to say-on long enough to see it. (And you shouldn't need to detect another hit within 1/10th of a second.)

If you are getting false triggers try increasing the threshold. If you are getting false triggers from noise pick-up you may have to use shielded cable. If your signal is too low (compared to the noise) you may need to change your mechanical set-up or you may need to use a different sensor.

You can check the noise/false triggers by running the [u]Analog Read Serial Example[/u]. Obviously, the threshold has to be higher than your "background" readings when there is no drum hit.

You can also check the "hit" readings with Analog Read Serial, but take-out the delay. There will still be some delay from the print statements but you should be able to "catch" come of the hits to get an idea. Obviously, the threshold has be lower than your drum hit readings.

vbshightime 's averaging program isn't going to help. You need to know the maximum background/noise level and the minimum hit level.

Hello everyone! thankyou so much for such prompt replies!

To answer some of your questions:

Thankyou for the guidance in forum posting PaulRB!

The LED strips that I have are 'TECH LIGHT 'Flexible Adhesive LED Strip' 1 metre (12VDC, 800 Lumens, 1000mA per metre). It comes with a red and grey-ish wire for the output.

Slumpert: A stock standard Piezo element is being used as the triggers for this project and I currently have just one going into pin A0.

Vbshightime: Thankyou so much for that code recommendation! will try it out. As for the LED lights input, what kind of MOSFET will I need?

DVDdoug: Ahh this is great! thankyou very much. I needed a more dynamic response to each hit so I think this will work perfectly! I will try this out! thanks for the great tips!

Seems like my only pickle now is hooking up the LED strips to the breadboard then the Arduino! any tips/help with what kind of MOSFETs or Capacitors or Resistors I may need would be great! I am still very much a beginner in electronics.

Cheers everyone!

You can use TIP120 npn darlington pair transistors or IRF 520. connect TIP's base to the arduino (use 10k resistor with the base). connect collector to the led strip and emmiter to ground.

No, don't use either of those models. Darlingtons are old technology, they drop too much voltage and get hot. The irf, even though it is a more modern design, may have similar problems because it does not have a logic level gate, so may not switch properly/fully on with only 5V on it's gate.

IRL520 would be a better choice. It has a logic level gate, or stp16nf06l, or any other n-channel, logic-level MOSFET.

@AJBACK1997 thanks for using code tags.

Hello there everyone.

Thanks for all the advice with what MOSfets to get and to connect them.

My problem has returned to an unstable code at the moment as the LEDs keep flashing even when I haven’t even touched the piezo sensor.

I’ve tried setting the threshold to all sorts of different levels but regardless of any code ammendments the LED tester keeps flashing on and off for what seems like forever. When reading the values in the serial print, the numbers go up and down until it reaches the threshold. I will continue trying the threshold in the mean time!


I’m not sure what else to do? Any help would be wonderful. I have still been using the same code

My problem has returned to an unstable code at the moment as the LEDs keep flashing even when I haven’t even touched the piezo sensor.

Check your piezo wiring and 1Megohm resistor.
The wiring should be short, or shielded.
Mains hum could easily be mistaken for a trigger.

The A/D could return 1023, so 'val' can't be a byte (255 max).
Cleaned up the code. Works here.

const byte sensorPin = A0;
const byte ledPin = 8;
int sensorValue;
int threshold = 500;

void setup() {
  pinMode(ledPin, OUTPUT);

void loop() {
  sensorValue = analogRead(sensorPin); // read sensor
  if (sensorValue >= threshold) {
    digitalWrite(ledPin, HIGH); // set pin on
    Serial.print("Knock!\tvalue: "); // write to serial (\t = tab)
    Serial.println(sensorValue); // not very useful, since it only prints just above the set threshold
    delay(100); // wait 100 miliseconds
    digitalWrite(ledPin, LOW); //set pin off

Hello @Wawa,

I have rewired and checked the resistor, I am currently hooked up to the breadboard with crocodile clips. Could that be the issue?

Even when I take the crocodile clips off and wire the red and black wires that come attached to the Piezo elements directly onto the breadboard, I still get a reading that it is being knocked straight to 1023 and the LED is on constantly. It does not turn off at all and the serial print keeps printing.

Id like to use a lengthier connection to the piezos rather as they will be attached to drums.

Any help would be greatly appreciated! thanks for your advice with the code! I will keep trying.

  • Andrew

Analogue values of 1023 means piezo wiring faulty.
Don't think of extending it to the drumkit until it works.
Post a picture of the setup.

Test your Uno.
The above sketch works with NOTHING connected to the Uno (just the USB cable).
Touching pin A0 with your finger should print trigger values.