Pulse Library

1. Intro

The pulse library can be used to create a signal (square wave) with different frequencies on any pin of Ansteron Board. The library is capable for generating square wave with frequency from 6 Hz to 4 KHz at less than 5% error. Signal with frequency up to 65 KHz can also be created.

Pulse library is software implemented and intended to use for creating sounds and melodies. For example, a small buzzer or speaker can be attached to any pin on Ansteron Board to make beeping sound, or a simple melody. The notes in a music sheet can be converted to a list of frequencies. The list can then be store into Ansteron Board onboard EEPROM or program memory and playback with a speaker.

2. How to use

To add this library into the program, in Ansteron IDE, go to menu Build, select Compiler settings then add library file into Additional libraries section.

For the circuits, a small buzzer (picture below) can connect directly to a pin of Ansteron Board. With a speaker, one transistor or an amplifier circuit is needed to drive it properly.

Basic circuit for a speaker

The library has one primary function:

pulse(pin, frequency, time);

This function generates a pulse at given frequency (in Hz) for a period in millisecond. For example, to make a beep sound at 440Hz on pin D7 for 500 milliseconds, type pulse(PIN_D7, 440, 500);. The program below will make a beep sound every 2 seconds.

Input pin is the same as using by other I/O functions. Frequency and time are int value

The pin input can be any pin on Ansteron Board. This function works well for frequencies from 6 to 2370 Hz. For frequencies above 2370, function pulse_hf(pin, frequency, time); can be used instead for better accuracy. Note that pulse_hf() does not work for frequencies below 2370 Hz. The accuracy of pulse_hf() at high frequencies is not guaranteed, in some cases, pulse() function may be more accurate at certain frequencies. Try each one for the frequency needed to find which one is more accurate.


The example program below running on the first circuit above (the small buzzer). Ansteron Board will print out the frequency and then send that pulse to the buzzer. It let you hear what the sound like at different frequencies.

And the output on Serial Terminal

2. Note

The timing used to generate pulses is based on counting CPU cycles. Other libraries that have background tasks (ie. TimeStamp library) may cause glitches on the output pulse as well as prolong the period time when being used with Pulse library. If the quality of output pulse is not a concern, these errors can be ignored. The time of output pulse should be reduced by certain percentage (depends on the background tasks) to improve time accuracy. Otherwise, temporally disable interrupts or background tasks will ensure the correctness of output pulse.