Driving a piezo speaker with a PIC
From Mech
Jump to navigationJump to searchOriginal Assignment
In this project you will use a PWM output of the PIC microcontroller to play tones on a piezo speaker. Document frequencies that play the basic notes in the musical scale and demonstrate a simple song.
Overview
Piezoelectric speakers operate by the converse piezoelectric effect: when a voltage is applied across the terminals, the piezoelectric material in the speaker to deflect in one direction. Applying an alternating voltage will cause the material to vibrate, and create a sound.
Circuit
Code
#include <18f4520.h> #fuses HS,NOLVP,NOWDT,NOPROTECT #use delay(clock=40000000) //define timer scaling value for each note #define C 255 #define D 231 #define E 205 #define F 192 #define G 175 #define A 150 #define B 123 #define C2 100 // #define x 14 //total number of notes in song to be played - modify for specific song //the song to be played is "Twinkle Twinkle Little Star" in this demonstration int i; int song [x]={C, C, G, G, A, A, G, F, F, E, E, D, D, C}; //insert notes of song in array int length[x]={1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2}; //relative length of each note void main() { setup_ccp1(CCP_PWM); // PWM output on CCP1/RC2, pin 17 set_pwm1_duty(30); // demonstrate the use of both PWMs independently for (i=0; i<x; i++) { //play x notes inside song array setup_timer_2(T2_DIV_BY_16, song[i], 16); //set PWM frequency according to entries in song array delay_ms(400*length[i]); //each note is played for 400ms*relative length setup_timer_2(T2_DIV_BY_16, 1, 16); //short break between notes //the PWM frequency set beyond audible range in order to create silence delay_ms(50); //to distinguish between consecutive notes } }