I want to stop the sound of a buzzer after I push the push button. I tried a code, but when the button is not pushed, the buzzer continue to make sound. How can I fixed it?

const int pin_contact= 6;
const int buzzer = 9; //buzzer to arduino pin 9

void setup(){
  pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
  pinMode(pin_contact, INPUT);

void loop() {
  tone(buzzer, 200);
  int stare_contact = digitalRead(pin_contact);
  if (stare_contact == HIGH) {
  • i tried with screen capture, but I couldn't upload in the comment, this is why i used a link. But in the end, I succeeded, using the first code and added 2 resistors in the circuit. Thank you!
    – Biancaaa
    Mar 26, 2021 at 16:21
  • If one of the answers helped you, you can vote it up. If neither helped you and you think you can provide an answer that someone else would find useful, you can answer your own question. If the question is both unanswered and unanswerable, then it's probably better off deleted.
    – timemage
    Mar 26, 2021 at 16:24
  • It is good to know. Right know I was trying to vote the answer, but i have to earn 15 reputation to upvote posts.
    – Biancaaa
    Mar 26, 2021 at 16:33
  • Ah, well, there's 10 for you.
    – timemage
    Mar 26, 2021 at 17:18
  • You restart the buzzer sound on every iteration of loop() so as soon as you stop pushing the button, the sound starts again. The simplest solution would be to move the tone (buzzer, 200); command out of loop() and place it at the end of setup(). Also make sure your button actually reads “HIGH” when pressed. Many buttons are active “LOW”.
    – StarCat
    Mar 26, 2021 at 21:44

2 Answers 2


Is your buzzer active low? try this code below. it will toggle buzzer pin output.

const int buzzerPin = 9;
const int buttonPin = 6; 
int buzzerState = 1;
int buttonState;
int buttonTemp = 0;

void setup() {
  pinMode(buzzerPin, OUTPUT);
  pinMode(buttonPin, INPUT);

void loop() { 
  buttonState = digitalRead(buttonPin); 
  if (buttonState == HIGH && buttonTemp == 0){
    Serial.print("Buzzer toggle to : ");
    if (buzzerState) {
        buzzerState = 0;
    } else {
        buzzerState = 1;
    buttonTemp = 1;
  } else if (buttonState == LOW) {
    buttonTemp = 0;

Try this

int LEDState = 1;
int SwitchPin = 6;
int LEDPIN = 9;

void setup()

void loop()

  int SwitchState = digitalRead(SwitchPin);

  if(LEDState==1 && SwitchState==HIGH){
   LEDState = 0; 

  • Sorry Jsotla, i have fixed it now... @Biancaaa I have fixed the code and i tried with an led except a buzzer because at this time buzzer is not available in my collections, afterall, try this code, this code worked very well for me using the LED as an output indicator instead of a buzzer Mar 28, 2021 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.