I am just trying to use an ultrasonic sensor with a buzzer but the buzzer doesn't respond to my code. I mean it doesn't make any sound. I tested it to check if by sending 5 volts to it, it will make a sound.. and it did... but using this code below... for some reason, it doesn't make any sound....
#include <NewPing.h>
#define TRIGGER_PIN 11
#define ECHO_PIN 12
#define buzzer 13
NewPing sonar1 (TRIGGER_PIN, ECHO_PIN);
long inches1;
int sound = 250;
void setup (){
Serial.begin (9600);
pinMode (TRIGGER_PIN, OUTPUT);
pinMode (ECHO_PIN, INPUT);
pinMode (buzzer, OUTPUT);
}
void loop () {
inches1 = sonar1.ping_in();
if (inches1 <= 5 ){
// sound = 250;
tone(buzzer, 1000); // Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(1000);
Serial.print (inches1);
Serial.print ('\n');
}
delay (200);
}