Morse Code Beep Arduino Micro
From charlesreid1
This page extends the discussion of the morse code blink circuit to making a morse code beep circuit. This replaces the blinking LED with a beeping piezoelectric speaker.
Contents
Start The Beeping Project
We want to replace the resistors and LED with a piezoelectric speaker. This will work essentially the same way - the piezoelectric speaker, like the LED, is a widget designed to turn a voltage into noise or light. By connecting the speaker to a voltage pin, and controlling the voltage of that pin with the Arduino microcontroller, we can generate morse code tones in a speaker.
The Speaker
The speaker I used was a 3 - 28 V DC piezoelectric speaker - these can be scavenged from many old electronics like phones, and since they're just generating a buzzing, they don't need to be fancy. No resistors were needed between the voltage pin and the speaker, since the board was using 5 volts, and that was well within the range of the speaker.
The Morse Code
As before, depending on your application, you may want to use either the Arduinomorse Library or the Morse Library, so I'll use both in this example.
The Circuit
The circuit is pretty simple: the microcontroller code turns the message into dashes and dots with voltage. The speaker turns voltage dashes and dots into beeps.
The Circuit
A Word on Pins
You should note that the Morse library uses Pulse Width Modulation (PWM) to activate the speaker. Not all of the Arduino pins are able to do PWM, but the ones that are, are usually indicated as such. On the Arduino Micro, the PWM pins are 3, 5, 6, 9, 10, 11 and 13. On the Arduino Leonardo, the PWM pins are the same.
PWM just means that we can send a variable-voltage signal out. Without PWM, all we have is ON and OFF, 1 and 0. But with PWM, we can use variable voltage to control the precise angle of a servo motor, or send a particular sound wave to a speaker.
Breadboard Diagram
Here is a breadboard layout using Fritzing, and connecting the speaker to pin 12 (this will only work with the Arduinomorse library, to use the Morse library you need to hook the speaker up to pin 9 or some other PWM pin):
As mentioned, pretty straightforward: we're powering the positive and negative buses with the red and black cables, connected to the Arduino's 12 pin and ground, respectively. We then route power from the positive bus to the piezo speaker's red wire, then the voltage passes through the speaker (generating sound), and the remaining voltage passes through the black wire to ground.
Breadboard Photo
Here is a photo of the above circuit, constructed on a breadboard:
The Morse Code Code (The Sketch)
Below are two sketches for sending morse code, one using Arduinomorse and the other using Morse.
Arduinomorse Speaker Sketch Code
Use the Arduinomorse library to generate our morse code tones, by switching from a LEDMorseSender
object to a SpeakerMorseSender
object. Note that this uses pin 12, which is not a PWM pin. The Arudinomorse library does not use PWM to send morse code to the speaker, so we're good.
#define MORSE_PIN 12
#define MORSE_WPM 20
#include <morse.h>
//LEDMorseSender sender(MORSE_PIN);
SpeakerMorseSender sender(MORSE_PIN);
void setup() {
sender.setup();
sender.setWPM(MORSE_WPM);
sender.setMessage(String("v v v de kc7dbu ar"));
sender.startSending();
}
void loop() {
sender.continueSending();
}
Morse Speaker Sketch Code
Here is the equivalent sketch using the Morse library. Note that this library DOES use pulse width modulation (PWM) to send morse code through the speaker, so we need to switch to pin 9 on the Micro, which supports PWM.
#include <Morse.h>
#define MORSE_PIN 9
#define MORSE_WPM 20
Morse morse(MORSE_PIN, MORSE_WPM, 1);
void setup()
{
// Nothing here for the Morse lib
}
void loop()
{
morse.sendmsg("V V V DE KC7DBU AR");
delay(5000);
}
Upload and Go
Once you upload the sketch, your piezo speaker should start sending out morse code.