Hardware Reference
In-Depth Information
Generating Audio Tones
Although most Arduinos are incapable of playing back advanced audio without
additional electronics, they can play musical notes and tones natively.
Audio, or sound in general, is simply a vibration that propagates as waves of
pressure. To generate sound, speakers and buzzers vibrate at certain frequen-
cies to create sound.
Audio tones generated by Arduinos are variable frequencies, which can range
from just a few Hertz up to 20 kHz, around the limits of human audition.
tone()
tone() is used mainly to generate audio tones on devices like buzzers. Although
designed to generate audible tones, it is not limited to audio. This function
generates a square wave, a signal that alternates instantly between two values,
typically the maximum voltage and zero. It generates signals with a i xed 50
percent duty cycle, from frequencies as low as 31 Hz to 80 kHz (humans can
typically hear up to 20 kHz). tone() accepts unsigned integers as a parameter.
This function requires either two or three parameters, depending on your use.
tone(pin, frequency)
tone(pin, frequency, duration)
The pin parameter is the pin number on which to produce a tone. The fre-
quency parameter is the frequency to generate in hertz, passed as an unsigned
int. Finally, the optional duration parameter is the duration of the tone in mil-
liseconds, passed as an unsigned long. If this parameter is not specii ed, the
tone will be generated indei nitely, or until the program tells the tone genera-
tion to stop.
noTone()
noTone() stops the square wave generation of tone() on the specii ed pin. If
no tone is generated, this function has no effect. This function must be called
before generating another tone on the same pin.
Reading Pulses
Arduinos can be told to react to pulses received on digital pins, reading serial
data when data becomes available, or to call specii c functions when a signal
is received. However, in some cases, it is not the change in the signal that is
important, but the time the signal stays at a logical state.
Imagine a sensor attached to your door. You want to know if the door was
opened, and you want to know exactly how long the door was opened for. By
adding a reed switch to your door, you can have a logical 1 (HIGH) if the door
 
Search WWH ::




Custom Search