Arduino UNO Tutorial 7 - Piezo Beep

Arduino UNO Tutorial 7 - Piezo Beep

In this easy Arduino UNO tutorial, we are going to use a simple Piezo Transducer to create some beeps. Piezo Transducers are similar to speakers in that an applied voltage causes the piezo crystal to move and make a sound. In order to get a tone a square wave needs to be applied usually in the range 20Hz to 20kHz.

Don't confuse piezo transducers with piezo sounders or piezo buzzers. These behave in the same way as buzzers and have a built in oscillator and are switched on by a d.c. signal voltage. They are therefore limited to one frequency

We will use the PWM functionality of the Arduino to create a tone on the piezo transducer.

Note: For this tutorial we will not use the Arduino function tone() as this has a number of restrictions

  • Can only be used on one pin at one time
  • Use of the tone() function will interfere with PWM output on pins 3 and 11

The tone() function should be used if you require a different frequency and/or you are not using PWM on pins 3 and 11

The Arduino PWM runs at 500Hz so will produce a nice audible tone. We use the Piezo Transducer available here.

Piezo Transducer

Connect the transducer to pin 9 and 0V on the Arduino

In the Arduino Sketch shown below we have created a separate function beep() which sends the PWM signal to the transducer, waits for a small delay, then turns the transducer off, then has another small delay. Thus, it beeps once. The delay (in milliseconds) is passed as a parameter. The PWM pulse duration (20 in sketch below) should not be important as it is the frequency that matters; set it to somewhere in the middle of the PWM range.

The sketch below beeps 3 times at startup and then beeps continuously at a slower rate.

/*
 Piezo
 
 This example shows how to run a Piezo Buzzer on pin 9
 using the analogWrite() function.
 
 It beeps 3 times fast at startup, waits a second then beeps continuously
 at a slower pace
 
 */

void setup()  { 
  // declare pin 9 to be an output:
  pinMode(9, OUTPUT);
  beep(50);
  beep(50);
  beep(50);
  delay(1000);
} 

void loop()  { 
  beep(200); 
}

void beep(unsigned char delayms){
  analogWrite(9, 20);      // Almost any value can be used except 0 and 255
                           // experiment to get the best tone
  delay(delayms);          // wait for a delayms ms
  analogWrite(9, 0);       // 0 turns it off
  delay(delayms);          // wait for a delayms ms   
}  

Tutorial 6 - Rotary Encoder

Tutorial 8 - Nightlight

Shopping Cart
0 items
 
DELIVERY/PAYMENT

Free Delivery Orders over £50

UK - £2.40 orders below
                    £50.00
EU Airmail from £3.95
Worldwide from £7.95
More Details...

 
Brands
 
 Check out our videos