4

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);
}
4
  • 2
    don't use tone() if it makes sound with 5 V. only set pin HIGH
    – Juraj
    Sep 20, 2019 at 18:45
  • hhhmmmm..... i see... i will try.. thanks for your answer... :) Sep 20, 2019 at 18:48
  • what exit code are you talking about?
    – jsotola
    Sep 20, 2019 at 20:12
  • 1
    What's the whole message the Arduino IDE gives you. Exit 1 usually means you've got a missing library or a syntax error in your code.
    – Dougie
    Sep 21, 2019 at 10:22

1 Answer 1

2

If you have a look at the complete error message you will see that there is a collision between the libraries NewPing and Tone.

Arduino: 1.8.9 (Windows Store 1.8.21.0) (Windows 10), Board: "Arduino/Genuino Uno"

Tone.cpp.o (symbol from plugin): In function `timer0_pin_port':

(.text+0x0): multiple definition of `__vector_7'

libraries\NewPing\NewPing.cpp.o (symbol from plugin):(.text+0x0): first defined here

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Uno.
2
  • 1
    yes, but the point is that they don't have to use tone()
    – Juraj
    Sep 21, 2019 at 18:20
  • I agree! But the question is "error" and perhaps someone take in the information that libraries can have conflicts, I have seen it to many times.
    – MatsK
    Sep 24, 2019 at 17:55

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.