Arduino Uno Piezoelectric Sensor
This project demonstrates how to interface a piezoelectric sensor with an Arduino Uno. The piezoelectric sensor converts mechanical stress (such as pressure or vibrations) into electrical signals. The Arduino reads these signals and outputs the sensor values to the Serial Monitor.
How It Works
A piezoelectric element is a ceramic disc bonded to a brass plate. Flexing the ceramic displaces charge within its crystal lattice, producing a voltage across its faces. No excitation supply is needed — the element generates its own signal, which is why it is called a passive sensor.
The output is an AC spike, not a steady level. A sharp tap produces a damped oscillation that decays over a few milliseconds, and the peak can reach tens of volts for a firm knock. That voltage is the main hazard: connected directly, it can exceed the 5 V rating of a GPIO and stress the input protection diodes.
Two components tame it. A 1 MΩ resistor across the element gives the generated charge somewhere to bleed, which both sets the decay time and stops the voltage running away. A 5.1 V Zener diode (or 3.3 V on a 3.3 V board) clamps the peak to a safe level. Hobby carriers such as the KY-006 and KY-031 usually include the resistor but not always the Zener.
The Arduino Uno samples with a 10-bit ADC, so analogRead() returns 0–1023 across 5 V. Because the pulse is brief, sampling must be fast enough to catch the peak — a loop with a long delay() will miss most knocks entirely.
Components Needed
- Arduino Uno
- Piezoelectric Sensor
- Jumper Wires
- Arduino Uno
Wiring to the Arduino Uno
Connect the red lead of the piezo to A0 and the black lead to GND, then fit the 1 MΩ resistor directly across those same two points. Add the Zener diode in parallel with its cathode (the banded end) on the signal side.
A 5.1 V Zener suits the 5 V logic of this board.
Mount the disc flat against the surface you want to monitor, using double-sided tape or epoxy around the rim rather than the centre. The ceramic must be free to flex; clamping the middle kills sensitivity.
| Connection | Arduino Uno pin | Function |
|---|---|---|
| Piezo + | A0 | Signal, through the protection network |
| Piezo − | GND | Common ground |
| 1 MΩ resistor | A0 to GND | Bleed resistor — required |
| Zener 5.1 V | A0 to GND | Clamps spikes, cathode to signal |
Build and Upload
Open the Arduino IDE and create a new sketch.
Copy and paste the provided Arduino code into the sketch.
Upload the code to the Arduino Uno.
Open the serial monitor with a baud rate of 9600.
Observe the sensor values printed on the Serial Monitor as you apply pressure or vibrations to the piezoelectric sensor.
Example Code
Knock detection with peak capture and a lockout to avoid counting echoes. Upload it with the board set to Arduino Uno and open the Serial Monitor at 9600 baud.
const int PIEZO_PIN = A0;
const int THRESHOLD = 82; // tune to your mounting
const unsigned long LOCKOUT_MS = 150; // ignore the ring-down after a hit
unsigned long lastKnock = 0;
unsigned long knocks = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
int value = analogRead(PIEZO_PIN); // sample fast — no delay in this path
if (value > THRESHOLD && millis() - lastKnock > LOCKOUT_MS) {
int peak = value; // follow the spike to its maximum
unsigned long start = millis();
while (millis() - start < 15) {
int v = analogRead(PIEZO_PIN);
if (v > peak) peak = v;
}
knocks++;
lastKnock = millis();
Serial.print("Knock #");
Serial.print(knocks);
Serial.print(" peak=");
Serial.println(peak);
}
}
Applications
A piezoelectric sensor turns up in a wide range of projects. These are the uses where it is the right choice rather than a compromise:
- Knock-activated switches and secret-knock door locks
- Impact and drop detection on equipment and packaging
- Electronic drum pads, where peak amplitude sets note velocity
- Vibration monitoring on motors and bearings for early fault warning
- Intrusion detection on windows and display cases
Working with the Arduino Uno
The Arduino Uno is built around the ATmega328P and runs on 5 V logic with 2 KB of SRAM and 32 KB of program flash. These details change how this circuit is wired and what the sketch can do, so they are worth stating plainly before you build.
The Uno runs at 5 V, so most hobby sensor modules connect directly with no level shifting.
With only 2 KB of SRAM, avoid large buffers and prefer the F() macro for constant strings.
The single hardware UART is shared with the USB connection, so heavy Serial printing competes with uploads.
| Arduino Uno characteristic | Value | Why it matters here |
|---|---|---|
| Logic voltage | 5 V | Matches most hobby modules directly |
| ADC resolution | 10-bit (0–1023) | Sets how finely an analog reading can be resolved |
| Analog inputs | A0–A5 (six channels) | Determines how many analog sensors can share the board |
| PWM outputs | D3, D5, D6, D9, D10 and D11 | Needed for brightness, speed and tone control |
| I²C pins | A4 (SDA) and A5 (SCL) | Fixed by hardware — wiring copied from another board may not match |
| Interrupt pins | D2 and D3 only | Required for counting fast or asynchronous events |
| Serial | a single hardware UART shared with USB | Monitor runs at 9600 baud by default |
Troubleshooting
Most problems with this module fall into a handful of categories. Work through these before suspecting the part itself:
- Nothing registers — the 1 MΩ resistor is missing, or the disc is glued at its centre and cannot flex.
- A single tap counts as several — the element is ringing. Increase the lockout period.
- The board resets on a hard knock — the voltage spike is reaching the GPIO. Fit the Zener clamp.
- Sensitivity is wildly different between builds — mounting dominates. Compare like-for-like only on identical mountings.
- Readings sit high constantly — the element is under mechanical stress; loosen the mounting.
- Code written for an ESP board gives odd analog values — the Arduino Uno uses a 10-bit ADC returning 0–1023; rescale any constant taken from a 12-bit example.
- An I²C sensor is not found after copying wiring from another Arduino — on the Arduino Uno I²C is on A4 (SDA) and A5 (SCL).
Taking It Further on the Arduino Uno
Once the basic reading works, where you go next depends very much on which board you are using. These are the directions that suit the Arduino Uno specifically:
The Uno’s shield ecosystem is its real advantage. Once the circuit works on a breadboard, a prototyping shield turns it into something permanent that still stacks with a data-logging or Ethernet shield without rewiring.
Logging to an SD card via a shield is the natural next step, since the Uno has no onboard storage and no network. Timestamp each reading with a DS3231 real-time clock so the log survives power cuts with correct times.
Because SRAM is limited to 2 KB, keep logged strings short and write them out immediately rather than buffering. Building a long String in memory is the most common cause of an Uno sketch that runs for hours and then freezes.
Notes and Practical Limits
Peak-following, as in the sketch, matters more than threshold tuning. The spike may last under a millisecond, so a single analogRead() often catches it on the way up and under-reports the hit.
The same element works in reverse: drive it with a square wave and it becomes a buzzer. That is exactly what a piezo sounder is, and it is why the KY-006 is sold as an output device while the KY-031 is sold as a knock sensor.