
Hello guys.Welcome back to my srituhobby blog. Today we will learn about piezo sensors. A Piezo sensor is a device used to measure changes in pressure, acceleration, temperature, strain, or force. It uses electric charges. The word piezo is a Greek word. It means ‘press’ or squeezes’.
Ok, let’s see how does work this sensor. This works on the principle of electric effect. Here the charge is collected on the metal plate and used to generate a voltage and send an electric current through a circuit. It is through this process that the sensor is activated.
This sensor is made using two plates. That is, made of a copper plate and ceramic plate. Especially, the negative wire should be connected to the copper plate and the positive wire to the ceramic plate. We can use this piezo sensor to get inputs and outputs.
Okay, now let’s see what applications use these sensors.
- PIN pads
- Key fobs.
- Microwave.
- Alarm clock.
- Music instrument.
We will now learn how to use this sensor for input and output using two examples. For that, I’m using an Arduino UNO board. For the first time, we will now learn how to get input from the piezo sensor. Okay, here is the complete list of components we need. You can buy components easily using these links.
Components
- Arduino UNO board x 1 — Amazon / Our Store
- Piezo sensor x 1 — Our Store / Amazon
- LED bulb x 5 — Banggood / Our Store
- 10k Resistor x 1 — Banggood /Our Store
- 180-ohm Resistor x 5 — Banggood /Our Store
- Jumper wires — Amazon / Our Store
- Breadboard x 1 — Amazon / Our Store
Step 1
Arduino UNO board.
Piezo sensor
LED bulb
10K Resistor
180-ohm resistor

Jumper wires

Breadboard

Step 2

Step 3
/*Piezo sensor with Arduino.
Serial monitor readings.
created by the SriTu Tech team.
Read the code below and use it for any of your creation
*/
void setup() {
Serial.begin(9600);//enable serial monitor
for (byte a = 2; a <= 6; a++) {
pinMode(a, OUTPUT);
}
}
void loop() {
int value = analogRead(A1);//read analog value and put in to the variable
Serial.println(value);//print serial monitor
for (int a = 1; a <= 5; a++) {
if (value > a * 20) {
digitalWrite(a + 1, HIGH);
} else {
digitalWrite(a + 1, LOW);
}
}
}
Step 4



Okay, the first example is just learned. Let us now learn the second example. In this example, we will learn how to get an output from the piezo sensor.
Step 1

Step 2
/*tone generate.
created by the SriTu Tech team.
Read the code below and use it for any of your creation
*/
#include "pitches.h"//tone library
void setup() {
//Don't need setup function
}
void loop() {
Toneone();
delay(100);
Tonetwo();
delay(100);
Tonethree();
delay(100);
Tonefour();
delay(100);
}
void Toneone() {
int melody[] = { //tone array
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0,
NOTE_B3, NOTE_B3, NOTE_B3, NOTE_B3, NOTE_A3, NOTE_G3,
NOTE_B3, NOTE_B3, NOTE_B3, NOTE_B3, NOTE_A3, NOTE_G3,
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_G3, 0
};
int noteDurations[] = {4, 8, 8, 4, 4, 4, 8, 8, 8, 8, 4, 4, 8, 8, 8, 8, 4, 4, 4, 8, 8, 4, 4};
for (int thisNote = 0; thisNote < 23; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(3, melody[thisNote], noteDuration); //play tone
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);//delay
noTone(3);//tone off
}
}
void Tonetwo() {
int melody2[] = {NOTE_GS7, NOTE_DS8, NOTE_GS7, 0, NOTE_DS8, NOTE_DS8, 0, NOTE_GS7, NOTE_GS7};
int noteDurations[] = {4, 8, 8, 4, 8, 8, 4, 4, 4};
for (int thisNote = 0; thisNote < 9; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(3, melody2[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(3);
}
}
void Tonethree() {
int melody3[] = {NOTE_GS6, NOTE_A6, NOTE_AS6, NOTE_B6, NOTE_C7,
NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7
};
int noteDurations[] = {4, 8, 8, 4, 8, 8, 4, 4, 4};
for (int thisNote = 0; thisNote < 9; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(3, melody3[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(3);
}
}
void Tonefour() {
int melody4[] = { NOTE_F5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_GS5,
NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_GS5, NOTE_FS5, NOTE_F5, NOTE_C6,
NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_GS5, NOTE_AS5, NOTE_C6,
NOTE_AS5, NOTE_F5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6,
NOTE_GS5, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_GS5, NOTE_FS5, NOTE_DS5,
NOTE_F5, NOTE_FS5, NOTE_GS5, NOTE_FS5, NOTE_F5, NOTE_DS5, NOTE_FS5, NOTE_F5
};
int noteDurations[] = {4, 8, 4, 8, 8, 8, 4, 8, 8, 4, 8, 8, 4, 8, 4, 8, 8, 8, 4, 8, 8, 2, 4, 8, 4, 8, 8, 8, 4, 8, 8, 4, 8, 8, 4, 8, 8, 4, 8, 8, 4, 8, 2};
for (int thisNote = 0; thisNote < 43; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(3, melody4[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(3);
}
}
Step 3



How does work piezo sensor – piezo sensor with Arduino UNO board.