How to program a buzzer to a PIC18F452 with PROTEUS and MPLAB IDE?

AI Thread Summary
The discussion focuses on programming a buzzer with a PIC18F452 microcontroller using MPLAB IDE and PROTEUS, specifically with a 4MHz crystal frequency. Users highlight the importance of the buzzer type, noting that some buzzers require an external oscillator while others operate directly with voltage. A participant successfully switched to a DC-operated buzzer and shared their code, which includes a function to control the buzzer based on a switch input. A challenge arises regarding the use of the __delay_ms(5000) function within a loop, raising questions about its behavior in conjunction with the buzzer control logic. Additionally, the need for a flyback diode is mentioned for buzzers with a magnet coil to prevent voltage spikes.
Maniac_XOX
Messages
86
Reaction score
5
TL;DR Summary
I have connected the Buzzer to the RC0 pin of PORTC but it is not working, I need it to sound with a period of 5 seconds on and 5 off. I can definitely handle the periods with timers or delay functions but I need the circuit to be working first.
1651513689302.png

crystal frequency is 4MHz
The code i have tried is:

Code:
#include <p18cxxx.h>
#include <xc.h>

void main(void)
{ 
    TRISC = 0;     // making TRISC pins output
    ADCON1 = 0x07;   // making them digital pins

   while(1)
    {
        RC0 = 1;
        __delay_ms(5000);
        RC0 = 0;
      
    }
}
 
Engineering news on Phys.org
What kind of a buzzer? Some will work just after applying voltage, some are just "speakers" that need an external oscillator/generator.

Check if a loop with 1ms delay between on/off states will produce any sound, you should get around 500Hz. Some buzzers are designed with a specific frequency in mind (like 2700 Hz) so it can be quiet, but at least you will know if it works.
 
Borek said:
What kind of a buzzer? Some will work just after applying voltage, some are just "speakers" that need an external oscillator/generator.

Check if a loop with 1ms delay between on/off states will produce any sound, you should get around 500Hz. Some buzzers are designed with a specific frequency in mind (like 2700 Hz) so it can be quiet, but at least you will know if it works.
Nevermind haha I have managed to make it through with some work, i had to also switch to a DC operated buzzer

Here is the code I have used:

Code:
#define _XTAL_FREQ 4000000
#define switch3 PORTBbits.RB4 // SWITCH INTERFACED
#define buzzer PORTCbits.RC0 // BUZZER INTERFACED

void BuzzerFunc(void);

void main (void)
{
    BuzzerFunc();
}

void BuzzerFunc(void)
{
   TRISBbits.RB4 = 1;     // RB4 is input
   TRISCbits.RC0 = 0;     // RC0 BUZZER IS OUTPUT
   buzzer = 0;           // initial value for buzzer
 

   while(1)
   {
       if (switch3 == 0)
       {
            buzzer = 1;
       }
   }
}

A problem I am facing at the moment is why doesn't the __delay_ms(5000) work after the buzzer = 1 line?? For example, if I make the while loop such that I want the buzzer to turn on and off at 5 second periods each:
Code:
 while(1)
   {
       if (switch3 == 0)
       {
            buzzer = 1;
            __delay_ms(5000)
           buzzer = 0;                // to turn it off
           __delay_ms(5000)
           buzzer = 1;                // to turn it back on
           __delay_ms(5000)      // wait another 5 seconds before turning it back on
       }
   }
Has it got to do with the while loop by any chance?
 
Last edited:
Borek said:
What kind of a buzzer?
This is an electrical engineering forum. If the buzzer BUZ1 now includes a magnet coil, then you will need to add a parallel flyback diode, to protect the BJT Q1 from the flyback voltage spike.
 
I have recently moved into a new (rather ancient) house and had a few trips of my Residual Current breaker. I dug out my old Socket tester which tell me the three pins are correct. But then the Red warning light tells me my socket(s) fail the loop test. I never had this before but my last house had an overhead supply with no Earth from the company. The tester said "get this checked" and the man said the (high but not ridiculous) earth resistance was acceptable. I stuck a new copper earth...
Thread 'Electromagnet magnetic field issue'
Hi Guys We are a bunch a mechanical engineers trying to build a simple electromagnet. Our design is based on a very similar magnet. However, our version is about 10 times less magnetic and we are wondering why. Our coil has exactly same length, same number of layers and turns. What is possibly wrong? PIN and bracket are made of iron and are in electrical contact, exactly like the reference design. Any help will be appreciated. Thanks. edit: even same wire diameter and coil was wounded by a...
Thread 'Beauty of old electrical and measuring things, etc.'
Even as a kid, I saw beauty in old devices. That made me want to understand how they worked. I had lots of old things that I keep and now reviving. Old things need to work to see the beauty. Here's what I've done so far. Two views of the gadgets shelves and my small work space: Here's a close up look at the meters, gauges and other measuring things: This is what I think of as surface-mount electrical components and wiring. The components are very old and shows how...
Back
Top