L298P Buzzer code

Hi

This is my first time posting/asking for help and my first Arduino projects (SMARS). I am using a UNO R3 and a L298P as per this link http://www.mantech.co.za/Datasheets/Products/EX029.pdf which states “Onboard buzzer (D4), you can set the astern alarm ringtone”.

I am trying to enable the onboard buzzer and have tried the following 2 code examples:-

int buzzer = 4;

void setup() 
{ 
  pinMode(buzzer, OUTPUT);
}

void loop() {
 for (int i=40; i<256; i++)
 {
  tone(buzzer, 1000); //1KHz
  delay(1000);
  noTone(buzzer);
  delay(1000);
 }
}
int speakerPin = 4; //buzzer pin?
//#define speakerPin  1

void setup()
{ 
pinMode(speakerPin, OUTPUT);
}

void loop()
{
for (int i=0; i<255; i++)
  {    
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(500);
  }
}

Neither code example works, any help would be most appreciated.

What is pin 4 on the UNO connected to? Do you have the UNO GND connected to the L298 board ground terminal? Can you post a wiring diagram?
Sorry, just noticed it’s a shield. :blush:
See if this works:

void loop() {
 for (int i=40; i<255; i++)
 {
  digitalWrite(buzzer,HIGH);
  delay(1000);
  digitalWrite(buzzer,LOW);
  delay(1000);
 }
}

Hi

it is stacked on top of the Arduino so pin 4 on the Arduino is connected to pin 4 on the Motor shield as is GND etc.

As picture attached.

OK, that may be just a 5V buzzer like a SonAlert, apply 5V, it buzzes, remove and it’s silent, no choice of frequency.

so digitalWrite(buzzer,HIGH); should apply 5v?

Yes.

void loop() {
 for (int i=40; i<255; i++)
 {
  digitalWrite(buzzer,HIGH); // apply 5V to pin 4
  delay(1000);
  digitalWrite(buzzer,LOW); // apply 0V to pin 4
  delay(1000);
 }
}