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

    Hello guys.Welcome back to my SriTu Hobby 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 the 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

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Let’s identify these components.

Step 2

Now connect these components using the below circuit diagram.

Step 3

Ok, let’s look at the code below.

  • The complete program of this project – Download
/*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

Finally, select the correct board and upload this code.

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.

For this, we need the following components.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Ok, let’s do this example step by step.

Step 1

First, connect these components. To do this, use the circuit diagram below.

Step 2

Then look at the code below. The first time you need to install the tone library. For that, use the below link and download it.

/*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

Finally, select the correct board and port. After, upload this code.

So I think you will learn something from this article. The full video guide is below. We hope to see you in the next project.

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

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *