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

In summary: 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:
  • #1
Maniac_XOX
86
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
  • #2
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.
 
  • #3
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:
  • #4
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.
 

1. How do I connect the buzzer to the PIC18F452 microcontroller?

To connect the buzzer to the microcontroller, you will need to use a digital output pin on the PIC18F452. This pin will be connected to the positive terminal of the buzzer. The negative terminal of the buzzer should be connected to ground.

2. How do I program the PIC18F452 to make the buzzer sound?

To program the microcontroller, you will need to use the MPLAB IDE. First, you will need to define the digital output pin as an output in your code. Then, you can use a simple logic statement to toggle the pin on and off, which will make the buzzer sound. Make sure to also set the frequency and duration of the buzzer sound in your code.

3. Can I use any buzzer with the PIC18F452?

Yes, you can use any buzzer as long as it is compatible with the voltage and current levels of the microcontroller. Make sure to check the datasheet of both the buzzer and the microcontroller to ensure compatibility.

4. How do I test my code and circuit before programming the actual microcontroller?

You can use the PROTEUS simulation software to test your code and circuit before programming the actual microcontroller. This software allows you to simulate the behavior of the microcontroller and its connected components, including the buzzer. This way, you can troubleshoot any issues before programming the actual hardware.

5. Can I use interrupts to control the buzzer?

Yes, you can use interrupts to control the buzzer. This can be useful for more complex programs where you may want the buzzer to sound at specific times or in response to certain events. You will need to configure the interrupt settings in your code and make sure to handle the interrupts properly to avoid any unexpected behavior.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Electrical Engineering
Replies
2
Views
2K
  • Electrical Engineering
Replies
6
Views
1K
  • Electrical Engineering
Replies
1
Views
2K
  • Electrical Engineering
Replies
6
Views
8K
Replies
4
Views
4K
  • STEM Career Guidance
Replies
1
Views
1K
  • Programming and Computer Science
Replies
7
Views
16K
  • Programming and Computer Science
Replies
7
Views
3K
  • Electrical Engineering
Replies
12
Views
14K
Back
Top