buzzer does not work with distance sensor hc-sr04

yesterday I was try the external battery to connect to the arduino uno board.( maybe 9v)
after connecting, buzzer was not working anymore.

here is my code:

[color=orange]#define trigPin 8
#define echoPin 7
#define buzzerPin 10
void setup()
{
  Serial.begin(9600);
  pinMode(trigPin,OUTPUT);
  pinMode(echoPin,INPUT);
  
}

void loop()
{
  int duration, distance;
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin,LOW);
  duration=pulseIn(echoPin,HIGH);
  distance=(duration/2)/29.1;
  Serial.print(distance);
  Serial.println("cm");
  delay(500);
  
  if(distance > 100)
  {
    digitalWrite(10,LOW);
  }
  else
  {
    digitalWrite(10,HIGH);
  }
 
}[/color]

it doesn’t work. but sensor are reading the distance.

#include <Servo.h>

Servo myservo;

int pos=0;

void setup()
{
  myservo.attach(8);
}

void loop()
{
  for(pos=0;pos<180;pos+=1)
  {
    myservo.write(pos);
    delay(15);
  }
  
  for(pos=180;pos>=1;pos-=1)
  {
    myservo.write(pos);
    delay();
  }
}

i had tried 2 different buzzer, they are working on blink buzzer code only.

9v battery?
What kind of battery.
If it is this kind, it is not powerful enough.

this, with 6 1.5v battery

even with 3.3v from arduino uno, the buzzer out loud.(with buzzer blink code)

What buzzer are you using?

5v and 12v yxdz buzzer

Give us link to it.

http://www.online-components.com.my/Buzzer-PCB-Mount-5V/q?pid=93&doit=order

http://www.online-components.com.my/Buzzer-with-Lead-12V-Black/q?pid=91&doit=order

Do you know this guy?

nope...

who is he?

Is this a school project?

yes...

Where are you making this an output?
#define buzzerPin 10

PWM(digital)

Attach the actual code you are debugging to your next post.
Use the [ code] # button you see at the top of the Text box.

LarryD:
Where are you making this an output?
#define buzzerPin 10

as follow the link u give, even led doesn't light.

my led and buzzer are connect to 10 (positive terminal), negative terminal connect to ground

LarryD:
Attach the actual code you are debugging to your next post.
Use the [ code] # button you see at the top of the Text box.

#define trigPin 7
#define echoPin 8
#define buzzerPin 10
void setup()
{
  Serial.begin(9600);
  pinMode(trigPin,OUTPUT);
  pinMode(echoPin,INPUT);
}

void loop()
{
  int duration, distance;
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin,LOW);
  duration=pulseIn(echoPin,HIGH);
  distance=(duration/2)/29.1;
  Serial.print(distance);
  Serial.println("cm");
  delay(500);
  
  if(distance > 100)
  {
    digitalWrite(10,LOW);
  }
  else
  {
    digitalWrite(10,HIGH);
  }
}

Also, would you attach a image of your hardware setup to your next post?

digitalWrite(10,LOW);
You are trying to make this pin LOW but you have not made this pin an output.
Do this in setup.

from the sensor of pic

the arrangement is ground, echo, trig, vcc

I don't se a resistor in series with the LED, it should be about 220 ohms.
Did you make pin 10 an output?