Why buzzer keep turning on even i tried to make it off?

Im trying to make an automatic water dispenser using Nodemcu esp8266, Ultrasonic sensor, buzzer, relay, and waterpump. Ive tried 2 code but they both cant turn off the buzzer. Im fairly new to arduino, any help would be much appreciated!!!

first code

/* Full video - https://youtu.be/IyywqOiAAZg */

const int pingPin = D0;//d0
const int echoPin = D1;//d1

const int LED1 = D2;//d2
const int BUZZER = D4; //d4
const int LED3 = D5;//d5


void setup() {
  pinMode(LED1 , OUTPUT);
  pinMode(BUZZER , OUTPUT);
  pinMode(LED3 , OUTPUT);
  
   Serial.begin(115200); // Starting Serial Terminal
}

void loop() {
   long duration, distance;
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(pingPin, LOW);
   pinMode(echoPin, INPUT);
   duration = pulseIn(echoPin, HIGH);
   Serial.print("Distance from the object = ");
   distance = microsecondsToCentimeters(duration);
   
   Serial.print(distance);
   Serial.print("cm");
   Serial.println();
    if ( distance <=  5 )
  {
    digitalWrite(LED1, LOW);
    digitalWrite(BUZZER, HIGH);
    digitalWrite(LED3, HIGH);
  }
  else
  {
    digitalWrite(LED1, HIGH);
    digitalWrite(BUZZER, LOW);
    digitalWrite(LED3, LOW);
  }
  
   delay(100);
}


long microsecondsToCentimeters(long microseconds) {
   return microseconds / 29 / 2;
}

second code

const int trigPin = D0;  // Ultrasonic sensor trig pin
const int echoPin = D1;  // Ultrasonic sensor echo pin
const int buzzerPin = D4;  // Buzzer pin
const int relayPin = D2;  // Relay pin

void setup() {
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW); // Initialize the relay to off state
}

void loop() {
  int duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;

  if (distance <= 5) { // If the distance is less than or equal to 10 cm
    digitalWrite(relayPin, HIGH); // Turn on the relay (water pump)
    tone(buzzerPin, 1000); // Produce a 1 kHz tone on the buzzer
    delay(2000); // Wait for 2 seconds
    digitalWrite(relayPin, LOW); // Turn off the relay (water pump)
    noTone(buzzerPin); // Stop the buzzer tone
  }
  delay(50000); // Wait for 0.5 seconds before taking the next measurement
  
}

Are you sure it's a passive buzzer.
Most buzzers are active, and don't work with tone();

Try writing the buzzerPin HIGH or LOW when needed instead of using tone();

That's 50 seconds, not 0.5 seconds.

Leo..

What distance does it report?
Does the led work correct?
Can you post a schematic and a picture of your setup?
You use d1 and d2 for ultrasonic. On uno those are used for serial communication. Not sure if that also holds for this board...

Thank you! I just realised that 3v isnt for ultrasonic sensor

Thanks!! ive change the microcontroler into arduino nano and it work

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.