How to use Piezoelectric Transducer with Arduino

In this tutorial it is shown how one can use Piezoelectric transducer with Arduino. Piezoelectric transducer can be used as both sensor or actuator. Here a piezoeletric transducer is used as sensor to detect 

Piezoelectric transducers can be encountered in various other gadgets as well, such as mobile phones, doorbells, and underwater sonar systems. Shown below, you can observe a representative piezoelectric transducer that has the potential to generate sounds akin to those employed in certain musical greeting cards.

Piezoelectric Transducer

How Piezoelectric Transducers Function

Piezoelectric transducers operate based on the principle of piezoelectricity, which refers to the generation of electricity under pressure. When pressure is applied to a piezoelectric device, it generates an electric charge, as depicted below. 


An Arduino can leverage this property by utilizing the transducer as a knock sensor. In response to a hit or knock on the transducer, the Arduino detects the impact and executes a designated action, such as activating an LED or emitting a tone from a speaker.

Conversely, if an electric charge is supplied to a piezoelectric transducer, it undergoes deformation or changes in shape, as illustrated below. 


By applying a varying voltage at a specific frequency, the transducer's movement can create sound or produce a musical note. This characteristic finds application in items like musical greeting cards or buzzers.

Remarkably, a single piezoelectric transducer can serve as both an input and an output device. Sonar systems, which incorporate a piezoelectric transducer at their core, emit sound signals and then detect the returning echoes. This phenomenon is recognizable as the classic "ping" sound commonly portrayed in submarine movies. The time taken for the ping's echo to return provides an estimate of the distance to a target. We will explore another instance of this concept in chapter 6, featuring the use of the Devantech SRF05 as a rangefinder.

Now that you have gained a brief understanding of piezoelectric transducers and their operational principles, you will employ a piezoelectric transducer as a knock sensor in an Arduino project. When the Arduino identifies a hit or knock on the sensor, it will sound a speaker, showcasing it use as alarm and responsiveness.

 Hardware and Schematic Diagram

Materials and Parts:

For this project, you will require the following components:

  • An Arduino board.
  • A breadboard along with jump wires.
  • A 0.5 watt 5V1 zener diode (Specifically, we utilized a BZX55C5V).
  • An uncased piezoelectric transducer, preferably a 27 mm variant obtainable from eBay.
  • A 1M ohm resistor.
  • A small speaker, 8 ohm
  • One 1k ohm resistor

Utilizing an uncased piezoelectric transducer is preferred in order to achieve superior outcomes compared to a cased alternative. Piezoelectric transducers, when impacted, can generate potentially harmful high voltages that might damage the Arduino. To safeguard the Arduino from these high voltages, a zener diode is deployed, while the resistor serves to dissipate the excess voltage produced by the transducer.

Refer to the provided circuit diagram for a comprehensive illustration. 

Piezoelectric Transducer with arduino and speaker schematic


Take note of the zener diode's orientation and how both the diode and resistor are positioned in parallel with the piezoelectric transducer.

Having examined the circuit diagram, you are now ready to proceed with assembling the circuit on your breadboard.

 Connecting the hardware

 Establish a connection between the hardware and the speaker, which should be attached to digital pin 8 using a 1k ohm resistor. Refer to figure below for the completed circuit illustration.

Piezoelectric Transducer with arduino and speaker
As the original speaker wire was overly pliable to be directly inserted into the breadboard, we opted to solder a pair of jumper wires to the speaker. If soldering tools are not available, you have the option to affix the wires to the wire jumpers using insulating tape, or alternatively, employ alligator clips.

With all elements properly linked, you can proceed to compose your sketch.

Code for Piezoelectric Transducer with Arduino

The Arduino code is an example of how to use a piezoelectric transducer sensor with an Arduino board to create a simple sound-producing system.

int sensorPin = 0;
int sensorValue = 0;
int threshold = 200;
int toneDuration = 40;
int toneFrequency = 262;
int speakerPin = 8;

void setup(){

void loop(){
   sensorValue = analogRead(sensorPin);
      if (sensorValue > threshold) {

Here's an explanation of the code step by step:

  1. Variable Declarations:

    • sensorPin: This variable is assigned the value 0, indicating that the analog input will be taken from analog pin 0 on the Arduino board.
    • sensorValue: This variable will store the analog reading obtained from the piezoelectric transducer sensor.
    • threshold: This variable is set to 200. It represents a threshold value above which the sensor reading needs to cross in order to trigger the sound.
    • toneDuration: This variable is set to 40 milliseconds. It determines how long the tone (sound) will be played.
    • toneFrequency: This variable is set to 262 Hz, which corresponds to the musical note "C4" on the piano.
    • speakerPin: This variable is assigned the value 8, indicating that the positive terminal of the speaker is connected to digital pin 8 on the Arduino board.
  2. setup() Function: The setup() function is used to initialize the Arduino board. In the provided code, this function is left empty, indicating that no specific initialization is needed for this project.

  3. loop() Function: The loop() function is the main part of the code that runs repeatedly. Here's how it works:

    • sensorValue = analogRead(sensorPin);: This line reads the analog voltage from the piezoelectric transducer sensor connected to the sensorPin. The sensor value is then stored in the sensorValue variable.
    • if (sensorValue > threshold) { ... }: This conditional statement checks if the sensor value is greater than the defined threshold value (threshold). If the condition is met, the following code block is executed:
      • tone(speakerPin, toneFrequency, toneDuration);: This function generates a square wave tone on the specified speakerPin. It takes three arguments: the pin number, the frequency of the tone in Hertz (Hz), and the duration of the tone in milliseconds (toneDuration).

In summary, the code continuously reads the analog input from the piezoelectric transducer sensor. When the sensor value surpasses the predefined threshold, it triggers the generation of a tone on the speaker connected to digital pin 8. This can create a sound-producing effect based on the vibrations detected by the piezoelectric sensor.

For more details:

Post a Comment

Previous Post Next Post