In this activity, we’ll build a small noise-making circuit on the BOE Shield’s prototyping area that will generate a tone if the robot’s batteries run too low.
When the voltage supply drops below the level a device needs to function properly, it’s called brownout. The device (your Arduino) typically shuts down until the supply voltage returns to normal. Then, it will restart whatever sketch it was running.
Brownouts typically happen when batteries are already running low, and the servos suddenly demand more power. For example, if the BOE Shield-Bot changes from full speed forward to full speed backward, the servos have to do extra work to stop the servos and then go the other direction. For this, they need more current, and when they try to pull that current from tired batteries, the output voltage dips enough to cause brownout.
Now, imagine your BOE Shield-Bot is navigating through a routine, and suddenly it stops for a moment and then goes in a completely unexpected direction. How will you know if it is a mistake in your code, or if it’s a brownout? One simple, effective solution is to add a speaker to your BOE Shield-Bot and make it play a “start” tone at the beginning of every sketch. That way, if your BOE Shield-Bot has a brownout while it’s navigating, you’ll know right away because it’ll play the start tone.
We’ll use a device called a piezoelectric speaker (piezospeaker) that can make different tones depending on the frequency of high/low signals it receives from the Arduino. The schematic symbol and part drawing are shown below.
Frequency is the measurement of how often something occurs in a given amount of time.
A piezoelectric element is a crystal that changes shape slightly when voltage is applied to it. Applying high and low voltages at a rapid rate causes the crystal to rapidly change shape. The resulting vibration in turn vibrates the air around it, and this is what our ear detects as a tone. Every rate of vibration makes a different tone.
Piezoelectric elements have many uses. When force is applied to a piezoelectric element, it can create voltage. Some piezoelectric elements have a frequency at which they naturally vibrate. These can be used to create voltages at frequencies that function as the clock oscillator for many computers and microcontrollers.
(1) piezospeaker (just peel off the “Remove the seal after washing” sticker if it has one)
(misc.) jumper wires
The picture below shows a wiring diagram for adding a piezospeaker to the breadboard.
Always disconnect power before building or modifying circuits!
Set the Power switch to 0.
Unplug the battery pack.
Unplug the programming cable.
The next example sketch tests the piezospeaker using calls to the Arduino’s tone function. True to its name, this function send signals to speakers to make them play tones.
There are two options for calling the tone function. One allows you to specify the pin and frequency (pitch) of the tone. The other allows you to specify pin, frequency, and duration (in milliseconds). We’ll be using the second option since we don’t need the tone to go on indefinitely.
tone(pin , frequency) tone(pin, frequency, duration)
This piezospeaker is designed to play 4.5 kHz tones for smoke alarms, but it can also play a variety of audible tones and usually sounds best in the 1 kHz to 3.5 kHz range. The start-alert tone we’ll use is:
tone(4, 3000, 1000); delay(1000);
That will make pin 4 send a series of high/low signals repeating at 3 kHz (3000 times per second). The tone will last for 1000 ms, which is 1 second. The tone function continues in the background while the sketch moves on to the next command. We don’t want the servos to start moving until the tone is done playing, so the tone command is followed by delay(1000) to let the tone finish before the sketch can move on to servo control.
Frequency can be measured in hertz (Hz) which is the number of times a signal repeats itself in one second. The human ear is able to detect frequencies in a range from very low pitch (20 Hz) to very high pitch (20 kHz or 20,000 Hz). One kilohertz is one-thousand-times-per-second, abbreviated 1 kHz.
This example sketch makes a beep when it starts running, then it goes on to send Serial Monitor messages every half-second. These messages will continue indefinitely because they are in the loop function. If the power to the Arduino is interrupted, the sketch will start at the beginning again, and you will hear the beep.
/* * Robotics with the BOE Shield - StartResetIndicator * Test the piezospeaker circuit. */ void setup() // Built in initialization block { Serial.begin(9600); Serial.println("Beep!"); tone(4, 3000, 1000); // Play tone for 1 second delay(1000); // Delay to finish tone } void loop() // Main loop auto-repeats { Serial.println("Waiting for reset..."); delay(1000); }
StartResetIndicator begins by displaying the message “Beep!” in the Serial Monitor. Then, immediately after printing the message, the tone function plays a 3 kHz tone on the piezoelectric speaker for 1 second. Because the instructions are executed so rapidly by the Arduino, it should seem as though the message is displayed at the same instant the piezospeaker starts to play the tone.
When the tone is done, the sketch enters the loop function, which displays the same “Waiting for reset…” message over and over again. Each time the reset button on the BOE Shield is pressed or the power is disconnected and reconnected, the sketch starts over again with the “Beep!” message and the 3 kHz tone.
Sketch Update Notice!!!
The sketch StartResetIndicator was updated on 11/16/2012. On the Arduino Mega 2650, the upload hangs with the original code listing. The offending command was:
Serial.println("Beep!!!");
It turns out that having more than one exclamation point in a row in a serial string causes this problem in the Mega 2650. From this point forward, we will find another way to express our enthusiasm in serial strings. - Editor.
We'll use tone at the beginning of every example sketch from here onward. So, it’s a good idea to get in the habit of putting tone and delay statements at the beginning of every sketch’s setup function.