Super Simple Alarm --- how to connect beeper to Arduino Nano

Hi everyone,

first I wanted to say "Hello". I am Darian and pretty new to everything. I hope that I chose the correct forum (General electronics).

What I would like to do is use a simple setup to create a alarm.
30 seconds after the programme has been started an alarm should go off.

Code-wise everything seems fine:

int counter = 0; // Variable to count the seconds
int pin = 12;  // My preferred digital PIN

void setup() {
  
   Serial.begin(9600);
   pinMode(pin, OUTPUT); // Setting pin 12 as output
   
}



void loop() {

  
  delay (1000); 
  counter ++; // every second the counter gets increased

  if (counter > 30) {
 
      // after 30 seconds the beeper goes off

      Serial.println ("Beep"); // control message
      digitalWrite(pin, HIGH);   // beep!
      delay (4000);  // wait 4 seconds
      digitalWrite(pin, LOW);  // turn beep off
      delay (2000); // wait another 2 seconds
    
  }
  

}

I uploaded the sketch to my Arduino Nano. The sketch arrived savely on the Nano.
The programme started. After 30 seconds the message "Beep" was printed to the Serial Monitor.
Apparently the code seems to work.

I have a 95db alarm module speaker.
(https://www.amazon.de/gp/product/B01MT5V0FM)
Having connected it to a 9V battery it started to make a loud noise. Apparently it is working.

But now I am sort of clueless how to attach the speaker to the Nano.
I connected D12 to one cable of the speaker and one to GND.
I assume that the voltage of the Nano are too weak.

Alternatively I already used the 5V pin - which is probably too weak, too.

Is it possible to simply "add" another battery to my circuit?

D12 connected to speaker connected to 9 Volt battery connected to GND

Or will this fry the Nano??

Can I use one of the digital PINs just to "open" an close an "external" circuit with its own battery supply?
Maybe I am completely on the wrong track.

I hope that I submitted everything you need to know to help me.
And really would like to apologize in advance, because the question is most likely very basic and probably even stupid. But I couldn't find a proper answer so far. So I hope this is the proper place for my question.

Thanks for your help,

best,

Darian

Did you try-out your circuit and software with an LED? (Always use a "current-limiting" resistor in series with the LED.)

Alternatively I already used the 5V pin - which is probably too weak, too.

The description does say 6-12V. :wink:

D12 connected to speaker connected to 9 Volt battery connected to GND

Or will this fry the Nano??

Yes! 9V* to an I/O pin can fry the Arduino. You need a MOSFET or transistor driver circuit (and the higher voltage source) to boost the voltage. [u]Here is a MOSFET driver circuit[/u]. (Or, you can make a similar circuit with an NPN transistor.)

Just about any "logic level" MOSFET or any NPN transistor will work since the voltage & current is low. (A "regular" MOSFET needs more than 5V to turn-on the gate.)

You can leave-out the diode since the load is non-inductive.

Having connected it to a 9V battery it started to make a loud noise. Apparently it is working.

That's good to know because there are (at least) 2 kinds of piezo devices and sometimes the descriptions are wrong. Plus, it proves the thing is working! I don't understand Deutsch but you can't always trust 3rd-party Amazon suppliers (or eBay or Alibaba, etc.) and they often don't give you the manufacturer's part number or a link to the datasheet.

A "buzzer" or "beeper" has a built-in tone-generating circuit and it makes sound when you apply DC voltage. It can't play music. They often require more current than the Arduino directly supply but in your case the voltage is the issue. (The same driver circuit can be used in any case.)

A piezo "transducer" or "speaker" (or tweeter) is a passive device that converts an AC audio (or higher frequency) electrical signal to sound and it can play music (but you don't get bass. :wink: ) They can be directly driven from the Arduino using tone(), etc. It will only make a "click" when connected, and again when disconnected, from DC power. (You cannot directly-drive a 4-Ohm or 8-Ohm speaker.)

  • You can't connect any voltage source directly to an output pin. For example If you connect 5V and then write a LOW, the output goes to (about) zero volts which will "short out" the 5V and possibly fry the Arduino. (Of course you can[/b] apply 5V (or less) to a pin configured for input.

Thanks a lot for your help and time!

You made things a lot clearer to me.

I will check out the MOSFET driver thing. (Never would have looked up this term by myself!)

Now I also know the difference between buzzer and speaker (which might come handy when discussing my project).

Again: thank you!

Darian