

The code below sets a threshold of 200 to distinguish between the two types of hits. You simply need a threshold value to distinguish between hard and soft hits. For example, you could trigger a loud drum sound when the element is hit hard, and a quiet drum sound when the element is hit softly. When applied to something like a MIDI keyboard, this is called "velocity." You can trigger different things based on whether or not the piezo element was hit very hard, very soft, or anywhere in between. This is because the more the piezo element is agitated, the higher the voltage it produces. Then hit the element a bit harder, and notice that the value registered is higher than before. Hit it lightly, and note the value registered. Go ahead and hit the piezo element with your finger. Once the board resets, the Arduino will start printing out voltage values (in this case, just an endless string of zeros, because of the 1M Ohm pull-down).
#Piezoelectric arduino serial#
Once the code is uploaded, open the Arduno IDE serial monitor. Serial.println(val, DEC) // Print the voltage to the terminalĬopy-paste and load this code onto your Arduino. Val = analogRead(DRUM1) // Read the voltage Serial.begin(9600) // Initializing the serial port at 9600 baud The following code does just that:īyte val = 0 // Initializing the variable for the voltage value At this step, we need code that does two things: reads the voltage on the analog line, and then prints that voltage to the terminal.

This is a very high-level description, so if you'd like to know exactly why this works, be sure to read up on the use of pull-down resistors. When this happens, the voltage produced "overrides" the weak pull-down by the resistor, allowing the voltage to be read on the analog line. The reason for the resistor in parallel with the element is to keep the analog line at zero volts until the piezo element produces a voltage. When that happens we will read a voltage on the analog zero line. This circuit will keep the analog line at zero volts until the piezo element is agitated. When you're finished, it should look like this. To connect this to the Arduino, insert one lead of your assembly into the Analog 0 line (any analog input will work, but for this example we'll use A0) and the other lead of your assembly into a ground line. To make the connection, solder the leads of the resistor to the leads of the piezo element in a parallel fashion like this If you've never soldered before, you might want to check out our Soldering 101 tutorial. However, that might not form a proper connection, so we'll solder them together. We could simply jam the resistor and piezo element leads into the Duemilanove's female headers. Out of the box, our first step is to wire a resistor to the piezo element. In this guide we'll be using an Arduino Uno for the example code, but this kit could be used with any device that can detect a small (less than 5v) voltage in an analog fashion. If this doesn't make sense now, don't worry, it will be explained further later. These are used in parallel with the piezo elements as pull-down resistors to ensure a good read of the voltage produced by the element. The other main component of the kit is the bag of 1M Ohm resistors. Piezo elements come in lots of shapes and sizes, but the ones in your kit are well-suited for detecting a hit or finger press. The piezo element has two leads, and when the pad is pressed, the element generates a small voltage across its leads. The first component is the piezo element itself. We call this the drum kit because we first used it to generate drum sounds using an Arduino and MP3 player shield, but it could be used with any other hardware that requires percussive velocity input.Īlong with everything inside the kit, we will be using these items in this guide:įirst, let's talk about the two main components of the kit.

Once you are able to detect voltage levels from the piezo sensors, you can use them to trigger sounds, turn things on and off, start your car, etc. This guide is a simple introduction to working with the piezo elements. In the past I have used these piezo sensors to rig up my desk and couch to play sounds when people interacted with them. The Piezo Drum Kit gives you the sensors to turn virtually anything into a five-piece drum kit.
