Go Down

Topic: Questions about this piezo... er... magnetic buzzer (Read 4 times) previous topic - next topic

dkl65

May 24, 2012, 12:25 am Last Edit: Jul 10, 2012, 09:46 pm by dkl65 Reason: 1
Hello!
I have finished a model password door lock project with Arduino! I used a red LED, green LED, piezo buzzer, soft potentiometer, and 3 buttons. Now, I am just making improvements. When I enter the correct password, the Arduino plays a victory "charge" music. Now, I want to create an audible CLICK with the piezo (sounding like a lock opening) when I enter the correct password. But, I don't want it to sound like a little TAP. Can someone teach me how to do that?

Link to datasheet of piezo: http://www.sparkfun.com/datasheets/Components/CEM-1203.pdf
Picture of piezo (copy and paste to browser, and find CIRC-06):
Code: [Select]
http://myclass.peelschools.org/ele/7/25580/Resources/Arduino%20Resources/ARDX-EG-SPAR-PRINT-85-REV-10[1].pdf
Code for my project:
Code: [Select]
#include <MomentaryButton.h>
#include <Easy.h>
#include <avr/sleep.h>
#include <avr/power.h>
// Project 27 Keypad door lock
#include <EEPROM.h>
MomentaryButton button(2);
MomentaryButton sButton(3);
MomentaryButton nButton(4);
Song alarm(11);
String secretCode = "1234";
String code = "";
int position = 0;
boolean locked = true;
int note[] = {
 262, 262, 392, 523, 392, 523};
int duration[] = {
 100, 100, 100, 300, 100, 300};
int redPin = 9;
int greenPin = 8;
byte piezo = 11;
byte tries = 0;
boolean playing = 1;
void setup()                    
{
 pinMode(redPin, OUTPUT);
 pinMode(greenPin, OUTPUT);
 DDRD = 0;
 PORTD = 0x1c;
 loadCode();
 flash();
 updateOutputs();
 button.setup();
 sButton.setup();
 nButton.setup();
}

void loop()                    
{
 char key = getKey();
 if (key == '*'  && ! locked)
 {
   // unlocked and * pressed so change code
   position = 0;
   getNewCode();
   updateOutputs();
 }
 if (key == '#')
 {
   locked = true;
   playing = 1;
   code = "";
   position = 0;
   updateOutputs();
   for(int i = 1; i <= 3; i++){
     tone(11, 250, 250);
     delay(250);
   }
 }
 if (key >= '0' && key <= '9')///
 {
   code += (char)key;
   position++;
 }
 if(!digitalRead(2)) tone(11, 500);
 else noTone(11);
 //if (position == 4)//
 //{
 if(code == secretCode && position == 4){
   locked = false;
   tries = 0;
   updateOutputs();
   for (int thisNote = 0; thisNote < 6 && playing; thisNote ++) {
     // play the next note:
     tone(11, note[thisNote]);
     // hold the note:
     delay(duration[thisNote]);
     // stop for the next note:
     noTone(11);
     delay(25);
   }
   playing = 0;
 }
 else if (position == 4 && code != secretCode){
   noTone(11);
   alarm.siren();
   alarm.siren();
   noTone(11);
   position = 0;
   code = "";
   tries++;
 }
 if(tries >= 3){
   alarm.ambulance();
   alarm.ambulance();
   noTone(11);
   sleep();
 }
 //}
}

void updateOutputs()
{
 if (locked)
 {
   digitalWrite(redPin, HIGH);
   digitalWrite(greenPin, LOW);  
 }
 else
 {
   digitalWrite(redPin, LOW);
   digitalWrite(greenPin, HIGH);
 }
}

void getNewCode()
{
 flash();
 for (int i = 0; i < 4; i++ )//
 {
   char key;
   key = getKey();
   while (key == 0)
   {
     key = getKey();
   }
   flash();
   secretCode[i] = key;
 }
 saveCode();
 flash();
 flash();
}

void loadCode()
{
 if (EEPROM.read(0) == 1)
 {
   for(int i = 0; i < 4; i++)//
     secretCode[i] = EEPROM.read(i+1);
 }
}

void saveCode()
{
 for(int i = 0; i < 4; i++){//
   EEPROM.write(i+1, secretCode[i]);
 }
 EEPROM.write(0, 1);
 tone(11, 500, 1000);  
}

void flash()
{
 digitalWrite(redPin, HIGH);
 digitalWrite(greenPin, HIGH);    
 delay(500);
 digitalWrite(redPin, LOW);
 digitalWrite(greenPin, LOW);    
}
char getKey(){
 button.check();
 sButton.check();
 nButton.check();
 if(button.wasClicked()){
   int reading = analogRead(A0);
   reading = map(reading, 0, 1023, 0, 7);
   return reading + 48;
 }
 if(sButton.wasClicked()) return '*';
 if(nButton.wasClicked()) return '#';
 return 0;
}
void sleep(){
 set_sleep_mode(SLEEP_MODE_PWR_DOWN);   // sleep mode is set here
 DDRD = 0;
 DDRB = 0;
 PORTD = 0;
 PORTB = 0;
 sleep_enable();
 power_adc_disable();
 power_spi_disable();
 power_timer0_disable();
 power_timer1_disable();
 power_timer2_disable();
 power_twi_disable();
 MCUCR = _BV (BODS) | _BV (BODSE);
 MCUCR = _BV (BODS);
 sleep_mode();
}



Thanks!

pYro_65

I've been wondering about PWM audio, so I had a quick look and found this: http://www.enide.net/webcms/index.php?page=pcm2pwm
It describes using mplayer to convert a wav to a pcm file then they use another tool to produce a header file for use in your sketch.

You could then download some samples ( maybe clicks from an old mono mobile phone ), and convert them.

Otherwise just experiment with different PWM values changing rapidly to create your desired effect.

dkl65

Using tone() to give a 1/2 duty cycle square wave to the piezo causes it the beep a certain tone. I think that the buzzer only works with square waves. Clicks don't have a particular tone; just volume. I will try using a transistor to amplify the volume! Also, I have a kit called "Fun with Computer Electronics" bought from Chapters book store. It uses a 4011 NAND gate chip, 4017 counter chip, 1uF and 10uF capicitors, piezo transducer, 10k, 100k, 1M ohm resistors, 10 LEDs, 9V battery to make various circuits on a cardboard box with metal springs. There are circuits that make thay piezo transducer click. I have another question: why does the black piezo buzzer I'm using creates no electricity from vibrations, but the other white piezo transducer from the Chapters kit does?

BulletMagnet83

Does it have an internal drive circuit? I would think (although I'm not sure) that such a circuit would prevent any voltage produced from tapping it from appearing on the input wires.

dkl65

#4
Jun 08, 2012, 12:30 am Last Edit: Jun 08, 2012, 12:40 am by dkl65 Reason: 1
What's the internal drive circuit? I need to provide it a square wave to generate a tone. The other white piezo transducer produces a current when I push it one way, and a reverse current when I push it another way. The black one generates no electricity. I changed the name of topic of this thread. Could this piezo buzzer respond fast enough to generate ultrasonic, or near ultrasonic tones (>= 17kHz)? I can still hear around 17kHz, according to a YouTube test. Also, do mosquitos actually keep some distance from you when you play a 17kHz+ noise?

Go Up