Play a tone through a Pezi Speaker when button pressed

Hi, I got my own kit a few days ago so excuse my poor Arduino Language!

I am wanting to know how to play a sound through a pezi or what ever it is called speaker, I need the script and the circuit. If someone could please help me!

The sound I want to use is the one featured in the tone example

Thanks!!!

claw106:
I am wanting to know how to play a sound through a pezi or what ever it is called speaker, I need the script and the circuit. If someone could please help me!

The sound I want to use is the one featured in the tone example

https://www.arduino.cc/en/Tutorial/Tone

The example comes with a circuit drawing and with full code.

So what's wrong with the example on the linked page?

The hardware you need is a "piezo speaker". Do you have that?

After uploading the code to your board the sound will play once when the setup() function is executed. You need to press the reset button on the board to play the sound again.

I have the Speaker, it works fine. But what I want is to have like a button hooked up to the breadboard so that when the button is pressed it replays the sound or a single tone from that sound.

I tried following this circuit: http://www.instructables.com/id/Arduino-Basics-Making-Sound/step4/The-Simple-Keyboard/

but I cant get it to work.

This is exactly what I want: Piezo Speaker & push button - Audio - Arduino Forum

but I cannot seem to get it to work. The fourm was posted in 2012 so...

claw106:
This is exactly what I want: Piezo Speaker & push button - Audio - Arduino Forum

but I cannot seem to get it to work. The fourm was posted in 2012 so...

Did you also work through the button tutorial?
http://www.arduino.cc/en/Tutorial/Button

Do you know about all three methods of connecting a button to an Arduino board:

  • using button and external pull-down resistor
  • using button and external pull-up resistor
  • using button and activate internal pull-up resistor
    Which is the circuit you are using for the button connections?

I do not have an arduino board I have a sparkfun redboard [still works with arduino, looks almost identical to the uno.

I did the circuit to what i thought was exact [I'm a beginner so i'm not to good at it]. But it doesn't work still.

I will try the circuit again later.

claw106:
I do not have an arduino board I have a sparkfun redboard [still works with arduino, looks almost identical to the uno.

I've looked up the board specification, it's fully compatible so that all UNO circuits and UNO sketches should execute the same as on an UNO.

claw106:
I did the circuit to what i thought was exact [I'm a beginner so i'm not to good at it]. But it doesn't work still.

I will try the circuit again later.

When programming, you always should use "serial debugging" to make the Arduino "Serial Monitor" show what's going on in your program.

Just start Serial in the setup and print a greeting message to the serial monitor. Then (after uploading the sketch) open the serial monitor (must be set to the same baudrate as used in Serial.begin), watch the greeting message and the following output in the serial monitor.

You already tested that the speaker is working?

And now have problems with the button?

So here is a sketch you can use for debugging with the circuit shown in instructables here.

#define BUTTON 2  // pin number of the pushbutton
#define SPEAKER 8 // pin number of the speaker
#define FREQUENCY 1000 // 1000 Hz tone frequency
void setup() {
  Serial.begin(9600);
  Serial.println("Good night and good luck");
  pinMode(BUTTON,INPUT);
}

void loop() {
  byte state= digitalRead(BUTTON); 
  if (state) tone(SPEAKER, FREQUENCY);
  else noTone(SPEAKER);
  Serial.println(state);
  delay(250); // halt the programm for a quarter second to slow it down for watching
}

When the button is up. you should see printing 0s in the serial monitor, and when the button is down you should see 1s printed. Does that work?

Or do you possibly see:

  • only 0s printed?
  • only 1s printed?
  • 0s and 1s printed randomly, no matter whether the button is pressed down or not?

What's happening? Watch out the debug messages on the serial monitor!