# Buzzer not working with delay avr atmega162

Hi I have got a problem that will be simple to you guys but i am newbie so its difficult for me to get things done correctly,

I Have buzzer interfaced with PinC0 of atmega162 , with crystal 12MHz Problem is that Its not producing beep with delays.

#define F_CPU 12000000UL
#include<avr/io.h>
#include<util/delay.h>

int main(void)
{
DDRC |= 1<<PINC0;
PORTC |= 1<<PINC0;
while(1){
PORTC ^=1<<PINC0;
_delay_ms(1000);
}
}


Buzzer is connected on PINc0 and then grounded on another end. its just a common buzzer.I dont know its model or datasheet

Every thing was working fine some days before I don't exactly know what happened all of sudden.

Proteus Simulation is working correct but realtime is producing long beep in slow voice with no delays

Awaiting for your appraising replies. :)

• As per your edit if you have a multimeter because you don't know the buzzer's details it might be worth connecting straight to Vcc on the microcontroller and put the meter in series to measure what current it needs. That will also confirm when connected directly the buzzer itself is still OK. – PeterJ Jan 26 '15 at 10:18
• put the meter on C0 too, see if the voltage there goes far enough to run your buzzer. see what happens with the buzzer disconnected, it could be that you need a transistor to switch the curren for your buzzer. – Jasen Jan 26 '15 at 10:24
• @Jasen Voltage across buzzer is 3.12V current it need is 23.4mA P.S. Everything has worked perfectly 2,3 days before. I think my delay has got a problem because buzzer is giving beep continuoisly. – user3306314 Jan 26 '15 at 10:59
• @PeterJ Buzzer is Ok. Its also gives beep with my code above, but I want delay in beeps. Thats not happening. – user3306314 Jan 26 '15 at 11:00

Perhaps do this instead of _delay_ms(1000)
for(int x=0;x<50;++x) _delay_ms(20);

• 23 was wrong, and is too short to hear anyway. you'll need to enable c99 mode or move the int x outside of the for {int x;for(x=0;x<50;++x) _delay_ms(20);} – Jasen Jan 26 '15 at 11:26