Help with my sensor speaker circuit

Hey everyone,
I have been trying to get a piezo buzzer to work with an IR sensor. I have got a code that works on software such as Tinker Cad, but when I try to actaully do it with a circuit, it isn't working. I'm new to coding so can't see my mistake. If anyone can see the mistake I have made that would be a lot of help

This is the code I have been using

// C++ code
int buttonState = 0;

void setup()
  pinMode(2, INPUT);
  pinMode(13, OUTPUT);

void loop()
  // read the state of the pushbutton
  buttonState = digitalRead(2);
  // check if pushbutton is pressed. if it is, the
  // button state is HIGH
  if (buttonState == HIGH) {
    tone(13, 659, 10000); // play tone 64 (E5 = 659 Hz)
  } else {
  delay(10); // Delay a little bit to improve simulation performance

And this is the circuit I am using

Change pin 2 to INPUT_PULLUP, wire the pin to connect to GND when the button is pressed, and test for a LOW.

Are you sure the IO pin can drive the buzzer directly?
You may need a transistor to provide enough current.
Or a resistor to limit the IO pin current and not destroy the pin.

@dtb123456789, your topic was moved to a more suitable location on the forum.

Please edit your post, select all code and click </>; next save your post.

Generally this looks OK, but maybe you could check a couple of things:

Like @CrossRoads says, check that the buzzer can be driven directly from the output pin (I think this should probably be OK, looking at it).

As a troubleshooting step, try connecting an LED (with resistor) to any output and set its pin to high at the same time as you activate the buzzer. This way you can work out if your sensor is behaving as expected (just make sure to switch it off again in the else condition).

You could also check the sensor voltage with a multimeter (against ground) when you think it should be activated and not (and check you've got the connections in the right order on the actual PIR)

Let us know how you get on :smiley:

Thanks a lot I'll try it in a bit

1 Like

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