Author Topic: Piezo Buzzers  (Read 2096 times)

0 Members and 1 Guest are viewing this topic.

Offline silo_xtremeTopic starter

  • Robot Overlord
  • ****
  • Posts: 172
  • Helpful? 0
  • I really love robots, they keep me awake at night.
Piezo Buzzers
« on: June 26, 2009, 12:45:44 PM »
Hey All,

How's everyone?

My electrical engineer put a SMT-2240-T piezo buzzer behind a BC817 (transistor I think).  So,  I'm not sure if I need to generate a 4KHz singal to turn it on, or if I need to just set a pin high.,

I'm not having luck with either.

What's the typical code for an ATMEGA for this type of application?  We're using an ATXMEGA .

Mike

Offline Trumpkin

  • Supreme Robot
  • *****
  • Posts: 1,177
  • Helpful? 5
Re: Piezo Buzzers
« Reply #1 on: June 26, 2009, 02:38:57 PM »
Usually you would just have to switch the pin high.
Here is some code to switch PORTD 7 high:
Code: [Select]
PORTD |= (1<<7);Before you would do that though you would need to declare that pin as an output like this:
Code: [Select]
DDRD |= _BV(PD7);
« Last Edit: June 26, 2009, 02:41:14 PM by Trumpkin »
Robots are awesome!

Offline silo_xtremeTopic starter

  • Robot Overlord
  • ****
  • Posts: 172
  • Helpful? 0
  • I really love robots, they keep me awake at night.
Re: Piezo Buzzers
« Reply #2 on: June 26, 2009, 03:10:43 PM »
Hey Trumpkin,

That is what i orginally thougt, but here is what I found:
http://www.pui.com/audioProducts/MovieClips/TechNotes/Glossary.asp?articalID=1


Since my buzzer is actually on PORTC Pin 6 -also as  Pin 21 on an ATXMEGA128A1  my code is this:
Code: [Select]
// Make PC6 an output
    PORTC.DIR = (1 << 6);

// Set that output to high
   PORTC.OUT = (1 << 6);

and I also tried sending a 4Khz signal as well.



Offline Trumpkin

  • Supreme Robot
  • *****
  • Posts: 1,177
  • Helpful? 5
Re: Piezo Buzzers
« Reply #3 on: June 26, 2009, 04:00:47 PM »
Could you post the datasheet of the buzzer?
Robots are awesome!


Offline Trumpkin

  • Supreme Robot
  • *****
  • Posts: 1,177
  • Helpful? 5
Re: Piezo Buzzers
« Reply #5 on: June 26, 2009, 05:20:30 PM »
Try connecting a nine volt battery to the two terminals of the buzzer (directly).
Robots are awesome!

Offline silo_xtremeTopic starter

  • Robot Overlord
  • ****
  • Posts: 172
  • Helpful? 0
  • I really love robots, they keep me awake at night.
Re: Piezo Buzzers
« Reply #6 on: June 26, 2009, 05:34:42 PM »
Hmm ... kinda hard to do when it's SMT part on a PCB.  ;)

Offline SmAsH

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: Piezo Buzzers
« Reply #7 on: June 26, 2009, 07:03:44 PM »
find the traces and touch probes/wires?
Howdy

Offline Trumpkin

  • Supreme Robot
  • *****
  • Posts: 1,177
  • Helpful? 5
Re: Piezo Buzzers
« Reply #8 on: June 26, 2009, 07:05:06 PM »
Quote
find the traces and touch probes/wires?
yep, that's what I meant.
Robots are awesome!

Offline silo_xtremeTopic starter

  • Robot Overlord
  • ****
  • Posts: 172
  • Helpful? 0
  • I really love robots, they keep me awake at night.
Re: Piezo Buzzers
« Reply #9 on: June 27, 2009, 06:46:50 AM »
Ok All,

Thanks for the help so far.  When I connect it to a 9V battery I hear something that is almost like static.

Definitely not the correct sound.

Does this imply I need to send in a 4KHZ signal - I'm a bit confused because that's a resonate frequency (output??)


Offline Trumpkin

  • Supreme Robot
  • *****
  • Posts: 1,177
  • Helpful? 5
Re: Piezo Buzzers
« Reply #10 on: June 27, 2009, 08:10:09 AM »
The sound of static implies that this is a piezo transducer and not a piezo buzzer. Read a bit about them here http://www.kpsec.freeuk.com/components/other.htm#piezo. So it looks like you need a square wave with a frequency of about 4khz, which is probably what you were doing before lol....
« Last Edit: June 27, 2009, 08:18:55 AM by Trumpkin »
Robots are awesome!

Offline silo_xtremeTopic starter

  • Robot Overlord
  • ****
  • Posts: 172
  • Helpful? 0
  • I really love robots, they keep me awake at night.
Re: Piezo Buzzers
« Reply #11 on: June 27, 2009, 08:24:25 AM »
LOL.  I'm working on it.


Offline silo_xtremeTopic starter

  • Robot Overlord
  • ****
  • Posts: 172
  • Helpful? 0
  • I really love robots, they keep me awake at night.
Re: Piezo Buzzers
« Reply #12 on: June 27, 2009, 08:32:32 AM »
I think that's the problem. my signal is probably no square.  :)

Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
Re: Piezo Buzzers
« Reply #13 on: June 27, 2009, 08:55:36 AM »
The sound of static implies that this is a piezo transducer and not a piezo buzzer. Read a bit about them here http://www.kpsec.freeuk.com/components/other.htm#piezo. So it looks like you need a square wave with a frequency of about 4khz, which is probably what you were doing before lol....


You are right, it's listed as a piezo transducer on mouser: http://ca.mouser.com/Search/ProductDetail.aspx?qs=sGAEpiMZZMuNFJjvCI6tQgUWCBGptnRr%2f1ZaW3omx7A%3d

Offline silo_xtremeTopic starter

  • Robot Overlord
  • ****
  • Posts: 172
  • Helpful? 0
  • I really love robots, they keep me awake at night.
Re: Piezo Buzzers
« Reply #14 on: June 27, 2009, 11:02:37 AM »
Ok.  So logic check me please.

Wouldn't this cause a 4000 Hz square wave?


Code: [Select]
unsigned int cycles = 30;
unsigned int freq = 4000;
PORTC.DIR |= 1<<6; //make PC6 an output
long i;
while (cycles > 0) {
PORTC.OUT |= 1<<6; // buzzer pin high
for(i=0;i<freq;i++);
PORTC.OUT &= !(1<<6); // buzzer pin low
for(i=0;i<freq;i++);
cycles--;
}

if yes, how would I control the duty cycle?

Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
Re: Piezo Buzzers
« Reply #15 on: June 27, 2009, 11:36:17 AM »
Ok.  So logic check me please.

Wouldn't this cause a 4000 Hz square wave?


Code: [Select]
unsigned int cycles = 30;
unsigned int freq = 4000;
PORTC.DIR |= 1<<6; //make PC6 an output
long i;
while (cycles > 0) {
PORTC.OUT |= 1<<6; // buzzer pin high
for(i=0;i<freq;i++);
PORTC.OUT &= !(1<<6); // buzzer pin low
for(i=0;i<freq;i++);
cycles--;
}

if yes, how would I control the duty cycle?



I see several issues.

First, to drive the pin low, you need to use a binary not operator (~), not a logical not (!).
Try:

PORTC.OUT &= ~_BV(PC6)

Second, your delay loop is completely wrong ;)
You need to introduce a delay corresponding to the period and the duty cycle you want.
The period is 1/F, that's easy. A complete iteration (HIGH then LOW) must last 1/4000Hz = 250 microseconds.
The duty cycle of a square wave is 50% so you need to drive your pin HIGH for 250*0.5 = 125 microseconds and then drive your pin low for the same duration.

so basically your loop should look like
Code: [Select]
  unsigned int cycles = 20000;           //make noise for 20000 * 250 microseconds = 5 seconds
  PORTC.DIR |= _BV(PC6); //make PC6 an output
  while (cycles > 0) {
    PORTC.OUT |= _BV(PC6); // buzzer pin high
    //wait for 125 micro seconds
    PORTC.OUT &= ~_BV(PC6); // buzzer pin low
    //wait for 125 micro seconds
    cycles--;
  }

The way you introduce delay is up to you. You can use:
- a busy loop (like you did): it depends on the frequency of your MCU.
- a timer: take a look at this page for more information about timers: http://members.shaw.ca/climber/avrtimers.html

Michel.

Offline silo_xtremeTopic starter

  • Robot Overlord
  • ****
  • Posts: 172
  • Helpful? 0
  • I really love robots, they keep me awake at night.
Re: Piezo Buzzers
« Reply #16 on: June 27, 2009, 02:15:20 PM »
hmm... what compiler are you using?   my compiler does not like the PC6 or _BV function.

Either way, when I just changed _BV( PC6) to (1<<6)

and used delay_us(125)  from WinAVR util\delay.h

I didn't get this working.   I'm still working on it and will report back.

Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
Re: Piezo Buzzers
« Reply #17 on: June 27, 2009, 02:49:46 PM »
hmm... what compiler are you using?   my compiler does not like the PC6 or _BV function.

Either way, when I just changed _BV( PC6) to (1<<6)

and used delay_us(125)  from WinAVR util\delay.h

I didn't get this working.   I'm still working on it and will report back.


I'm using avr-gcc 4.3.2 and avr-libc 1.6.2

I dont know WinAVR, but I guess there is a initialization function. Did you call it?

Do you have access to an oscilloscope or a multimeter with frequency measure?

 


Get Your Ad Here