Controlling passive buzzer

Thread Starter

Djsarakar

Joined Jul 26, 2020
352
Hi, I want to turn on/off passive buzzer for 2 seconds time. Buzzer detail given here https://www.thegeekpub.com/wiki/sensor-wiki-ky-006-passive-piezo-buzzer-module/

I followed given link and wrote my own code for PIC18F45K80.

When I run following code Buzzer gives sound continuously

C:
// PIC18F45K80 Configuration Bit Settings

#define _XTAL_FREQ 20000000

// CONFIG1L
#pragma config RETEN = ON       // VREG Sleep Enable bit (Ultra low-power regulator is Enabled (Controlled by SRETEN bit))
#pragma config INTOSCSEL = LOW  // LF-INTOSC Low-power Enable bit (LF-INTOSC in Low-power mode during Sleep)
// SOSCSEL = No Setting
#pragma config XINST = OFF      // Extended Instruction Set (Disabled)

// CONFIG1H
#pragma config FOSC = HS2        //  HS oscillator (high power, 16 MHz-25 MHz
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)

// CONFIG2L
#pragma config PWRTEN = ON      // Power Up Timer (Enabled)
#pragma config BOREN = OFF      // Brown Out Detect (Disabled in hardware, SBOREN disabled)
#pragma config BORV = 0         // Brown-out Reset Voltage bits (3.0V)
#pragma config BORPWR = LOW     // BORMV Power level (BORMV set to low power level)

// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1        // Watchdog Postscaler (1:1)

// CONFIG3H
#pragma config CANMX = PORTC    // ECAN Mux bit (ECAN TX and RX pins are located on RC6 and RC7, respectively)
#pragma config MSSPMSK = MSK5   // MSSP address masking (5 bit address masking mode)
#pragma config MCLRE = OFF      // Master Clear Enable (MCLR Disabled, RE3 Enabled)

// CONFIG4L
#pragma config STVREN = OFF     // Stack Overflow Reset (Disabled)
#pragma config BBSIZ = BB1K     // Boot Block Size (1K word Boot Block size)

// CONFIG5L
#pragma config CP0 = ON         // Code Protect 00800-01FFF (Enabled)
#pragma config CP1 = ON         // Code Protect 02000-03FFF (Enabled)
#pragma config CP2 = ON         // Code Protect 04000-05FFF (Enabled)
#pragma config CP3 = ON         // Code Protect 06000-07FFF (Enabled)

// CONFIG5H
#pragma config CPB = ON         // Code Protect Boot (Enabled)
#pragma config CPD = ON         // Data EE Read Protect (Enabled)

// CONFIG6L
#pragma config WRT0 = ON        // Table Write Protect 00800-01FFF (Enabled)
#pragma config WRT1 = ON        // Table Write Protect 02000-03FFF (Enabled)
#pragma config WRT2 = ON        // Table Write Protect 04000-05FFF (Enabled)
#pragma config WRT3 = ON        // Table Write Protect 06000-07FFF (Enabled)

// CONFIG6H
#pragma config WRTC = ON        // Config. Write Protect (Enabled)
#pragma config WRTB = ON        // Table Write Protect Boot (Enabled)
#pragma config WRTD = ON        // Data EE Write Protect (Enabled)

// CONFIG7L
#pragma config EBTR0 = ON       // Table Read Protect 00800-01FFF (Enabled)
#pragma config EBTR1 = ON       // Table Read Protect 02000-03FFF (Enabled)
#pragma config EBTR2 = ON       // Table Read Protect 04000-05FFF (Enabled)
#pragma config EBTR3 = ON       // Table Read Protect 06000-07FFF (Enabled)

// CONFIG7H
#pragma config EBTRB = ON       // Table Read Protect Boot (Enabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

#define Buzzer             LATDbits.LATD0
#define TRUE                  1
#define Buzzer_HIGH           1
#define Buzzer_LOW            0

void Port_Initialized (void)
{
    ANCON0 = 0; // Set to digital port
    ANCON1 = 0; // Set to digital port
    CM1CON = 0; // Comparator off
    CM2CON = 0; // Comparator off
    ADCON0 = 0; // A/D conversion Disabled
    ADCON1 = 0; // A/D conversion Disabled
    ADCON2 = 0; // A/D conversion Disabled
   
    LATA =  0;
    LATB =  0;
    LATC =  0;
    LATD =  0;
    LATE =  0;
   
    TRISA = 0b0000000; // all are output, Unused
    TRISB = 0b0000000; // all are output, Unused
    TRISC = 0b0000000; // all are output, Unused
    TRISD = 0b0000000; //all are output, Unused
    TRISE = 0b0000000; // All are output, Unused
}

void main(void)
{
   
    unsigned int count = 0;
 
    Port_Initialized(); // Initialize the PORT
   
   while(TRUE) // run forever
     {    
       ////Frequency 1
         for ( count = 0; count < 80; count++ )
         {
             Buzzer = Buzzer_HIGH;
             __delay_ms (1);
             Buzzer = Buzzer_LOW;
             __delay_ms (1) ;
           
          }
         //Frequency 2
         for ( count = 0; count < 1000; count++ )
         {
             Buzzer = Buzzer_HIGH;
             __delay_ms (2);
             Buzzer = Buzzer_LOW;
             __delay_ms (2) ;
           
          }
     }
   
}
What should I do in code to turn on/off passive buzzer for 2 seconds?
 

jpanhalt

Joined Jan 18, 2008
10,515
Please show how your buzzer is connected. Is Vcc connected? Can you post a real datasheet for the buzzer, not some arduino how-to link?

Are those delays in milliseconds? A period of 3 ms is a frequency of 333 Hz (approximately this note:
).

Of course, periods of 2 ms and 4 ms would be a higher and lower pitch, respectively.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
352
Please show how your buzzer is connected.
Please look at picture attached in documents

Is Vcc connected?
No I haven't connected vcc yet but with out vcc it works

Buzzer s connected RD0 pin
Buzzer ground connected to power supply ground

Can you post a real datasheet for the buzzer, not some arduino how-to link?
sorry jpanhalt i can't provide you real datasheet because I and the forum members tried to find the real datasheet but we could not find the real datasheet. I can understand how difficult it is to work without a real datasheet

Are those delays in milliseconds?
Yes the delay in the first loop is 1ms and the delay in the another loop is 2 ms
 

Attachments

jpanhalt

Joined Jan 18, 2008
10,515
Please look at picture attached in documents
That may be part of the problem. Your "picture" describes two different connection schemes. Neither has anything to do with the PIC18F45K80.

No I haven't connected vcc yet but with out vcc it works
So, without connecting Vcc it works. What does that mean? I thought the problem was that your code didn't work (i.e., do what you expected it to do). Yes, that buzzer can be powered through the signal pin according to the arduino connections.

sorry jpanhalt i can't provide you real datasheet because I and the forum members tried to find the real datasheet but we could not find the real datasheet. I can understand how difficult it is to work without a real datasheet
AAC is not ElectronicsPoint. I realize they are related, but some of us, including myself, rarely visit there.

Yes the delay in the first loop is 1ms and the delay in the another loop is 2 ms
So, it appears you have two loops that should make an alternating high-low pitch. Where do you turn that off in your code?
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
352
That may be part of the problem. Your "picture" describes two different connection schemes. Neither has anything to do with the PIC18F45K80.
That may be part of the problem. Your "picture" describes two different connection schemes. Neither has anything to do with the PIC18F45K80.
I do not understand what the problem is in the connection.

As said previously

Buzzer S connected RD0 pin of PIC
Buzzer ground connected to ground pin on the board

I posted working that sound the buzzer continuously so I just wanted to get sound for 2 seconds only
 
Last edited:

hexreader

Joined Apr 16, 2011
464
Solution is below:

Code:
// PIC18F45K80 Configuration Bit Settings

#define _XTAL_FREQ 20000000

// CONFIG1L
#pragma config RETEN = ON       // VREG Sleep Enable bit (Ultra low-power regulator is Enabled (Controlled by SRETEN bit))
#pragma config INTOSCSEL = LOW  // LF-INTOSC Low-power Enable bit (LF-INTOSC in Low-power mode during Sleep)
// SOSCSEL = No Setting
#pragma config XINST = OFF      // Extended Instruction Set (Disabled)

// CONFIG1H
#pragma config FOSC = HS2       //  HS oscillator (high power, 16 MHz-25 MHz
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)

// CONFIG2L
#pragma config PWRTEN = ON      // Power Up Timer (Enabled)
#pragma config BOREN = OFF      // Brown Out Detect (Disabled in hardware, SBOREN disabled)
#pragma config BORV = 0         // Brown-out Reset Voltage bits (3.0V)
#pragma config BORPWR = LOW     // BORMV Power level (BORMV set to low power level)

// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1        // Watchdog Postscaler (1:1)

// CONFIG3H
#pragma config CANMX = PORTC    // ECAN Mux bit (ECAN TX and RX pins are located on RC6 and RC7, respectively)
#pragma config MSSPMSK = MSK5   // MSSP address masking (5 bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable

// CONFIG4L
#pragma config STVREN = OFF     // Stack Overflow Reset (Disabled)
#pragma config BBSIZ = BB1K     // Boot Block Size (1K word Boot Block size)

// CONFIG5L
#pragma config CP0 = ON         // Code Protect 00800-01FFF (Enabled)
#pragma config CP1 = ON         // Code Protect 02000-03FFF (Enabled)
#pragma config CP2 = ON         // Code Protect 04000-05FFF (Enabled)
#pragma config CP3 = ON         // Code Protect 06000-07FFF (Enabled)

// CONFIG5H
#pragma config CPB = ON         // Code Protect Boot (Enabled)
#pragma config CPD = ON         // Data EE Read Protect (Enabled)

// CONFIG6L
#pragma config WRT0 = ON        // Table Write Protect 00800-01FFF (Enabled)
#pragma config WRT1 = ON        // Table Write Protect 02000-03FFF (Enabled)
#pragma config WRT2 = ON        // Table Write Protect 04000-05FFF (Enabled)
#pragma config WRT3 = ON        // Table Write Protect 06000-07FFF (Enabled)

// CONFIG6H
#pragma config WRTC = ON        // Config. Write Protect (Enabled)
#pragma config WRTB = ON        // Table Write Protect Boot (Enabled)
#pragma config WRTD = ON        // Data EE Write Protect (Enabled)

// CONFIG7L
#pragma config EBTR0 = ON       // Table Read Protect 00800-01FFF (Enabled)
#pragma config EBTR1 = ON       // Table Read Protect 02000-03FFF (Enabled)
#pragma config EBTR2 = ON       // Table Read Protect 04000-05FFF (Enabled)
#pragma config EBTR3 = ON       // Table Read Protect 06000-07FFF (Enabled)

// CONFIG7H
#pragma config EBTRB = ON       // Table Read Protect Boot (Enabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

#define Buzzer             LATDbits.LATD0
#define TRUE                  1
#define Buzzer_HIGH           1
#define Buzzer_LOW            0

void Port_Initialized (void)
{
    ANCON0 = 0;                                        // Set to digital port
    ANCON1 = 0;                                        // Set to digital port
    CM1CON = 0;                                        // Comparator off
    CM2CON = 0;                                        // Comparator off
    ADCON0 = 0; // A/D conversion Disabled
    ADCON1 = 0; // A/D conversion Disabled
    ADCON2 = 0; // A/D conversion Disabled
  
    LATA =  0;
    LATB =  0;
    LATC =  0;
    LATD =  0;
    LATE =  0;
  
    TRISA = 0b0000000; // all are output, Unused
    TRISB = 0b0000000; // all are output, Unused
    TRISC = 0b0000000; // all are output, Unused
    TRISD = 0b0000000; //all are output, Unused
    TRISE = 0b0000000; // All are output, Unused
}

void main(void)
{
  
    unsigned int count = 0;
 
    Port_Initialized(); // Initialize the PORT
  
    // Frequency 1
    for ( count = 0; count < 600; count++ )
    {
         Buzzer = Buzzer_HIGH;
         __delay_ms (1);
         Buzzer = Buzzer_LOW;
         __delay_ms (1) ;

    }
    // Frequency 2
    for ( count = 0; count < 200; count++ )
    {
         Buzzer = Buzzer_HIGH;
         __delay_ms (2);
         Buzzer = Buzzer_LOW;
         __delay_ms (2) ;

    }
    while(1);
}
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
352
Solution is below:
@hexreader I haven't tested your code yet but have a question?
I can see you have changed delay length in original code. I don't see any line that turn on/off buzzer for 2 seconds.
Perhaps it's better if you can explain code.
I don't understand which part/ lines of code turn on/off buzzer for 2 seconds.
 

hexreader

Joined Apr 16, 2011
464
Frequency 1:
2 delays of 1ms looped 600 times = 2x600 = 1200 ms (1.2 seconds)

Frequency 2:
2 delays of 2ms looped 200 times = 4x200 = 800 ms (0.8 seconds)

1.2+0.8 = 2 seconds
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
352
I want a 2 seconds sound of buzzer when sensor become active

https://tkkrlab.nl/wiki/Arduino_KY-032_Obstacle_avoidance_sensor_module

1602934091239.png

Here is my program that supposed to give 2 seconds sound of buzzer when sensor become active

C:
// PIC18F45K80 Configuration Bit Settings

#define _XTAL_FREQ 20000000

// CONFIG1L
#pragma config RETEN = ON       // VREG Sleep Enable bit (Ultra low-power regulator is Enabled (Controlled by SRETEN bit))
#pragma config INTOSCSEL = LOW  // LF-INTOSC Low-power Enable bit (LF-INTOSC in Low-power mode during Sleep)
// SOSCSEL = No Setting
#pragma config XINST = OFF      // Extended Instruction Set (Disabled)

// CONFIG1H
#pragma config FOSC = HS2       //  HS oscillator (high power, 16 MHz-25 MHz
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)

// CONFIG2L
#pragma config PWRTEN = ON      // Power Up Timer (Enabled)
#pragma config BOREN = OFF      // Brown Out Detect (Disabled in hardware, SBOREN disabled)
#pragma config BORV = 0         // Brown-out Reset Voltage bits (3.0V)
#pragma config BORPWR = LOW     // BORMV Power level (BORMV set to low power level)

// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1        // Watchdog Postscaler (1:1)

// CONFIG3H
#pragma config CANMX = PORTC    // ECAN Mux bit (ECAN TX and RX pins are located on RC6 and RC7, respectively)
#pragma config MSSPMSK = MSK5   // MSSP address masking (5 bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable

// CONFIG4L
#pragma config STVREN = OFF     // Stack Overflow Reset (Disabled)
#pragma config BBSIZ = BB1K     // Boot Block Size (1K word Boot Block size)

// CONFIG5L
#pragma config CP0 = ON         // Code Protect 00800-01FFF (Enabled)
#pragma config CP1 = ON         // Code Protect 02000-03FFF (Enabled)
#pragma config CP2 = ON         // Code Protect 04000-05FFF (Enabled)
#pragma config CP3 = ON         // Code Protect 06000-07FFF (Enabled)

// CONFIG5H
#pragma config CPB = ON         // Code Protect Boot (Enabled)
#pragma config CPD = ON         // Data EE Read Protect (Enabled)

// CONFIG6L
#pragma config WRT0 = ON        // Table Write Protect 00800-01FFF (Enabled)
#pragma config WRT1 = ON        // Table Write Protect 02000-03FFF (Enabled)
#pragma config WRT2 = ON        // Table Write Protect 04000-05FFF (Enabled)
#pragma config WRT3 = ON        // Table Write Protect 06000-07FFF (Enabled)

// CONFIG6H
#pragma config WRTC = ON        // Config. Write Protect (Enabled)
#pragma config WRTB = ON        // Table Write Protect Boot (Enabled)
#pragma config WRTD = ON        // Data EE Write Protect (Enabled)

// CONFIG7L
#pragma config EBTR0 = ON       // Table Read Protect 00800-01FFF (Enabled)
#pragma config EBTR1 = ON       // Table Read Protect 02000-03FFF (Enabled)
#pragma config EBTR2 = ON       // Table Read Protect 04000-05FFF (Enabled)
#pragma config EBTR3 = ON       // Table Read Protect 06000-07FFF (Enabled)

// CONFIG7H
#pragma config EBTRB = ON       // Table Read Protect Boot (Enabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

#define Buzzer                 LATDbits.LATD0
#define  Obstacle_Sensor       LATCbits.LATC0
#define TRUE                  1
#define Buzzer_HIGH           1
#define Buzzer_LOW            0
#define Sensor_HIGH           1
#define Sensor_LOW            0

void Port_Initialized (void)
{
    ANCON0 = 0;                                        // Set to digital port
    ANCON1 = 0;                                        // Set to digital port
    CM1CON = 0;                                        // Comparator off
    CM2CON = 0;                                        // Comparator off
    ADCON0 = 0; // A/D conversion Disabled
    ADCON1 = 0; // A/D conversion Disabled
    ADCON2 = 0; // A/D conversion Disabled
 
    LATA =  0;
    LATB =  0;
    LATC =  0;
    LATD =  0;
    LATE =  0;
 
    TRISA = 0b0000000; // all are output, Unused
    TRISB = 0b0000000; // all are output, Unused
    TRISC = 0b0000001; // all are output, Unused
    TRISD = 0b0000000; //all are output, Unused
    TRISE = 0b0000000; // All are output, Unused
}

void main(void)
{
 
    unsigned int count = 0;
 
    Port_Initialized(); // Initialize the PORT
 
    while (1)
    {
        if (Obstacle_Sensor == Sensor_LOW   )
         {
            // Frequency 1
            for ( count = 0; count < 600; count++ )
            {
               Buzzer = Buzzer_HIGH;
                __delay_ms (1);
               Buzzer = Buzzer_LOW;
               __delay_ms (1) ;

             }
         // Frequency 2
           for ( count = 0; count < 200; count++ )
            {
              Buzzer = Buzzer_HIGH;
              __delay_ms (2);
              Buzzer = Buzzer_LOW;
              __delay_ms (2) ;

            }
         }

    }
}
When I run the code Buzzer is giving continuously sound.

if I change line if (Obstacle_Sensor == Sensor_HIGH ) still Buzzer is giving continuously sound.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
352
I have corrected line 72 in code but problem is same still Buzzer is giving continuously sound.

C:
// PIC18F45K80 Configuration Bit Settings

#define _XTAL_FREQ 20000000

// CONFIG1L
#pragma config RETEN = ON       // VREG Sleep Enable bit (Ultra low-power regulator is Enabled (Controlled by SRETEN bit))
#pragma config INTOSCSEL = LOW  // LF-INTOSC Low-power Enable bit (LF-INTOSC in Low-power mode during Sleep)
// SOSCSEL = No Setting
#pragma config XINST = OFF      // Extended Instruction Set (Disabled)

// CONFIG1H
#pragma config FOSC = HS2       //  HS oscillator (high power, 16 MHz-25 MHz
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)

// CONFIG2L
#pragma config PWRTEN = ON      // Power Up Timer (Enabled)
#pragma config BOREN = OFF      // Brown Out Detect (Disabled in hardware, SBOREN disabled)
#pragma config BORV = 0         // Brown-out Reset Voltage bits (3.0V)
#pragma config BORPWR = LOW     // BORMV Power level (BORMV set to low power level)

// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1        // Watchdog Postscaler (1:1)

// CONFIG3H
#pragma config CANMX = PORTC    // ECAN Mux bit (ECAN TX and RX pins are located on RC6 and RC7, respectively)
#pragma config MSSPMSK = MSK5   // MSSP address masking (5 bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable

// CONFIG4L
#pragma config STVREN = OFF     // Stack Overflow Reset (Disabled)
#pragma config BBSIZ = BB1K     // Boot Block Size (1K word Boot Block size)

// CONFIG5L
#pragma config CP0 = ON         // Code Protect 00800-01FFF (Enabled)
#pragma config CP1 = ON         // Code Protect 02000-03FFF (Enabled)
#pragma config CP2 = ON         // Code Protect 04000-05FFF (Enabled)
#pragma config CP3 = ON         // Code Protect 06000-07FFF (Enabled)

// CONFIG5H
#pragma config CPB = ON         // Code Protect Boot (Enabled)
#pragma config CPD = ON         // Data EE Read Protect (Enabled)

// CONFIG6L
#pragma config WRT0 = ON        // Table Write Protect 00800-01FFF (Enabled)
#pragma config WRT1 = ON        // Table Write Protect 02000-03FFF (Enabled)
#pragma config WRT2 = ON        // Table Write Protect 04000-05FFF (Enabled)
#pragma config WRT3 = ON        // Table Write Protect 06000-07FFF (Enabled)

// CONFIG6H
#pragma config WRTC = ON        // Config. Write Protect (Enabled)
#pragma config WRTB = ON        // Table Write Protect Boot (Enabled)
#pragma config WRTD = ON        // Data EE Write Protect (Enabled)

// CONFIG7L
#pragma config EBTR0 = ON       // Table Read Protect 00800-01FFF (Enabled)
#pragma config EBTR1 = ON       // Table Read Protect 02000-03FFF (Enabled)
#pragma config EBTR2 = ON       // Table Read Protect 04000-05FFF (Enabled)
#pragma config EBTR3 = ON       // Table Read Protect 06000-07FFF (Enabled)

// CONFIG7H
#pragma config EBTRB = ON       // Table Read Protect Boot (Enabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

#define Buzzer                 LATDbits.LATD0
#define  Obstacle_Sensor       PORTCbits.RC0
#define TRUE                  1
#define Buzzer_HIGH           1
#define Buzzer_LOW            0
#define Sensor_HIGH           1
#define Sensor_LOW            0

void Port_Initialized (void)
{
    ANCON0 = 0;                                        // Set to digital port
    ANCON1 = 0;                                        // Set to digital port
    CM1CON = 0;                                        // Comparator off
    CM2CON = 0;                                        // Comparator off
    ADCON0 = 0; // A/D conversion Disabled
    ADCON1 = 0; // A/D conversion Disabled
    ADCON2 = 0; // A/D conversion Disabled

    LATA =  0;
    LATB =  0;
    LATC =  0;
    LATD =  0;
    LATE =  0;

    TRISA = 0b0000000; // all are output, Unused
    TRISB = 0b0000000; // all are output, Unused
    TRISC = 0b0000001; // all are output, Unused
    TRISD = 0b0000000; //all are output, Unused
    TRISE = 0b0000000; // All are output, Unused
}

void main(void)
{

    unsigned int count = 0;

    Port_Initialized(); // Initialize the PORT

    while (1)
    {
        if (Obstacle_Sensor == Sensor_LOW   )
         {
            // Frequency 1
            for ( count = 0; count < 600; count++ )
            {
               Buzzer = Buzzer_HIGH;
                __delay_ms (1);
               Buzzer = Buzzer_LOW;
               __delay_ms (1) ;

             }
         // Frequency 2
           for ( count = 0; count < 200; count++ )
            {
              Buzzer = Buzzer_HIGH;
              __delay_ms (2);
              Buzzer = Buzzer_LOW;
              __delay_ms (2) ;

            }
         }

    }
}
Edit : when sensor detect any object small red led is turning on the board

IMG_20201017_195533.jpg
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,344
It simulates correctly.
Use your voltmeter to make sure that the pin voltage is swinging from Vdd to ground levels as the sensor switches.
The sensor output must return to a '1' to turn the buzzer off.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
352
@JohnInTX I don't understand exactly where might be problem in the code or sensor module

I have tried new sensor https://tkkrlab.nl/wiki/Arduino_KY-033_Hunt_sensor_module to find out problem if the sensor module is faulty

Here is my code to test module

C:
// PIC18F45K80 Configuration Bit Settings

#define _XTAL_FREQ 20000000

// CONFIG1L
#pragma config RETEN = ON       // VREG Sleep Enable bit (Ultra low-power regulator is Enabled (Controlled by SRETEN bit))
#pragma config INTOSCSEL = LOW  // LF-INTOSC Low-power Enable bit (LF-INTOSC in Low-power mode during Sleep)
// SOSCSEL = No Setting
#pragma config XINST = OFF      // Extended Instruction Set (Disabled)

// CONFIG1H
#pragma config FOSC = HS2       //  HS oscillator (high power, 16 MHz-25 MHz
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)

// CONFIG2L
#pragma config PWRTEN = ON      // Power Up Timer (Enabled)
#pragma config BOREN = OFF      // Brown Out Detect (Disabled in hardware, SBOREN disabled)
#pragma config BORV = 0         // Brown-out Reset Voltage bits (3.0V)
#pragma config BORPWR = LOW     // BORMV Power level (BORMV set to low power level)

// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1        // Watchdog Postscaler (1:1)

// CONFIG3H
#pragma config CANMX = PORTC    // ECAN Mux bit (ECAN TX and RX pins are located on RC6 and RC7, respectively)
#pragma config MSSPMSK = MSK5   // MSSP address masking (5 bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable

// CONFIG4L
#pragma config STVREN = OFF     // Stack Overflow Reset (Disabled)
#pragma config BBSIZ = BB1K     // Boot Block Size (1K word Boot Block size)

// CONFIG5L
#pragma config CP0 = ON         // Code Protect 00800-01FFF (Enabled)
#pragma config CP1 = ON         // Code Protect 02000-03FFF (Enabled)
#pragma config CP2 = ON         // Code Protect 04000-05FFF (Enabled)
#pragma config CP3 = ON         // Code Protect 06000-07FFF (Enabled)

// CONFIG5H
#pragma config CPB = ON         // Code Protect Boot (Enabled)
#pragma config CPD = ON         // Data EE Read Protect (Enabled)

// CONFIG6L
#pragma config WRT0 = ON        // Table Write Protect 00800-01FFF (Enabled)
#pragma config WRT1 = ON        // Table Write Protect 02000-03FFF (Enabled)
#pragma config WRT2 = ON        // Table Write Protect 04000-05FFF (Enabled)
#pragma config WRT3 = ON        // Table Write Protect 06000-07FFF (Enabled)

// CONFIG6H
#pragma config WRTC = ON        // Config. Write Protect (Enabled)
#pragma config WRTB = ON        // Table Write Protect Boot (Enabled)
#pragma config WRTD = ON        // Data EE Write Protect (Enabled)

// CONFIG7L
#pragma config EBTR0 = ON       // Table Read Protect 00800-01FFF (Enabled)
#pragma config EBTR1 = ON       // Table Read Protect 02000-03FFF (Enabled)
#pragma config EBTR2 = ON       // Table Read Protect 04000-05FFF (Enabled)
#pragma config EBTR3 = ON       // Table Read Protect 06000-07FFF (Enabled)

// CONFIG7H
#pragma config EBTRB = ON       // Table Read Protect Boot (Enabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

#define LED                LATDbits.LATD0
#define  Sensor            PORTBbits.RB0
#define TRUE                  1
#define LED_ON                1
#define LED_OFF               0

void Port_Initialized (void)
{
    ANCON0 = 0;                                        // Set to digital port
    ANCON1 = 0;                                        // Set to digital port
    CM1CON = 0;                                        // Comparator off
    CM2CON = 0;                                        // Comparator off
    ADCON0 = 0; // A/D conversion Disabled
    ADCON1 = 0; // A/D conversion Disabled
    ADCON2 = 0; // A/D conversion Disabled

    LATA =  0;
    LATB =  0;
    LATC =  0;
    LATD =  0;
    LATE =  0;

    TRISA = 0b0000000; // all are output, Unused
    TRISB = 0b0000001; // all are output, Unused
    TRISC = 0b1000001; // all are output, Unused
    TRISD = 0b0000000; //all are output, Unused
    TRISE = 0b0000000; // All are output, Unused
}

void main(void)
{
    Port_Initialized(); // Initialize the PORT
  
     LED = LED_OFF;

    while (1)
    {
        if (Sensor == 0)
          
        {
         LED = 1;
         __delay_ms(1000);
        }
    }
}

I don't understand why led is turning on if the sensor is not active

Attached screenshot
 

Attachments

hexreader

Joined Apr 16, 2011
464
I have never known JohnInTX to be wrong before, but maybe today is a first.
Seems like a digital output to me, according to the datasheet that I found. I do not own this device, so cannot be sure.

Try this code, which has commenting improved (but needs you to correct and improve further) and formatting improved.

This code reacts to low and high.

C:
// random code found on forum
// PIC18F45K80 with external 2-pin 20MHz crystal
// test some random sensor thing that gives digital on/off output

// PIC18F45K80 Configuration Bit Settings

#define _XTAL_FREQ 20000000

// CONFIG1L
#pragma config RETEN = ON       // VREG Sleep Enable bit (Ultra low-power regulator is Enabled (Controlled by SRETEN bit))
#pragma config INTOSCSEL = LOW  // LF-INTOSC Low-power Enable bit (LF-INTOSC in Low-power mode during Sleep)
// SOSCSEL = No Setting
#pragma config XINST = OFF      // Extended Instruction Set (Disabled)

// CONFIG1H
#pragma config FOSC = HS2       //  HS oscillator (high power, 16 MHz-25 MHz
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)

// CONFIG2L
#pragma config PWRTEN = ON      // Power Up Timer (Enabled)
#pragma config BOREN = OFF      // Brown Out Detect (Disabled in hardware, SBOREN disabled)
#pragma config BORV = 0         // Brown-out Reset Voltage bits (3.0V)
#pragma config BORPWR = LOW     // BORMV Power level (BORMV set to low power level)

// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1        // Watchdog Postscaler (1:1)

// CONFIG3H
#pragma config CANMX = PORTC    // ECAN Mux bit (ECAN TX and RX pins are located on RC6 and RC7, respectively)
#pragma config MSSPMSK = MSK5   // MSSP address masking (5 bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable

// CONFIG4L
#pragma config STVREN = OFF     // Stack Overflow Reset (Disabled)
#pragma config BBSIZ = BB1K     // Boot Block Size (1K word Boot Block size)

// CONFIG5L
#pragma config CP0 = ON         // Code Protect 00800-01FFF (Enabled)
#pragma config CP1 = ON         // Code Protect 02000-03FFF (Enabled)
#pragma config CP2 = ON         // Code Protect 04000-05FFF (Enabled)
#pragma config CP3 = ON         // Code Protect 06000-07FFF (Enabled)

// CONFIG5H
#pragma config CPB = ON         // Code Protect Boot (Enabled)
#pragma config CPD = ON         // Data EE Read Protect (Enabled)

// CONFIG6L
#pragma config WRT0 = ON        // Table Write Protect 00800-01FFF (Enabled)
#pragma config WRT1 = ON        // Table Write Protect 02000-03FFF (Enabled)
#pragma config WRT2 = ON        // Table Write Protect 04000-05FFF (Enabled)
#pragma config WRT3 = ON        // Table Write Protect 06000-07FFF (Enabled)

// CONFIG6H
#pragma config WRTC = ON        // Config. Write Protect (Enabled)
#pragma config WRTB = ON        // Table Write Protect Boot (Enabled)
#pragma config WRTD = ON        // Data EE Write Protect (Enabled)

// CONFIG7L
#pragma config EBTR0 = ON       // Table Read Protect 00800-01FFF (Enabled)
#pragma config EBTR1 = ON       // Table Read Protect 02000-03FFF (Enabled)
#pragma config EBTR2 = ON       // Table Read Protect 04000-05FFF (Enabled)
#pragma config EBTR3 = ON       // Table Read Protect 06000-07FFF (Enabled)

// CONFIG7H
#pragma config EBTRB = ON       // Table Read Protect Boot (Enabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

#define LED                LATDbits.LATD0
#define Sensor             PORTBbits.RB0
#define TRUE                  1
#define LED_ON                1
#define LED_OFF               0

void Port_Initialized (void)
{
    ANCON0 = 0;                                                                 // Set to digital port
    ANCON1 = 0;                                                                 // Set to digital port
    CM1CON = 0;                                                                 // Comparator off
    CM2CON = 0;                                                                 // Comparator off
    ADCON0 = 0;                                                                 // A/D conversion Disabled
    ADCON1 = 0;                                                                 // A/D conversion Disabled
    ADCON2 = 0;                                                                 // A/D conversion Disabled

    LATA =  0;
    LATB =  0;
    LATC =  0;
    LATD =  0;
    LATE =  0;

    TRISA = 0b0000000;                                                          // all are output, Unused
    TRISB = 0b0000001;                                                          // all are output, Unused
    TRISC = 0b1000001;                                                          // all are output, Unused
    TRISD = 0b0000000;                                                          // all are output, Unused
    TRISE = 0b0000000;                                                          // All are output, Unused
}

void main(void)
{
    Port_Initialized();                                                         // Initialize PIC ports

    LED = LED_OFF;                                                              // D0 low to turn off LED

    while (1)
    {
        if (Sensor == 0)                                                        // sensor goes low when ???
        {
            LED = 1;                                                            // D0 high to light LED
            __delay_ms(1000);                                                   // keep LED lit long enough to see change of status
        }
        else
        {
            LED = 0;                                                            // D0 low to extinguish LED
            __delay_ms(1000);                                                   // keep LED off long enough to see change of status         
        }
    }
}
 
Top