-
Notifications
You must be signed in to change notification settings - Fork 1
Arduino library to generate square waves on all pins
gregfriedland/SoftwareTone
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
SoftwareTone: an Arduino library to generate square wave tones on pins 2-13. Written by Greg Friedland, November 20, 2011. With hardware PWM, you could make square waves on up to 6 pins by using up all the hardware timers. This can be limiting in some cases, such as when you want to use the timers for other things including some of the built-in Arduino functionality. The motivation for this library is to allow creation of square wave tones on more pins than is to avoid these limitations and to generate sqaure waves on more pins than is possible with hardware PWM alone, up to 12 pin excluding 0 and 1 (which are important for serial communication). Since this library uses software instead of hardware to generate the tones, it has limitations at high frequencies. The the pin states are updated every 50 microseconds but, which is not fast enough to create frequencies above 10kHz (since a square wave needs two pin state changes for each cycle). Also, higher frequencies will be rounded to a nearby frequency. For example, for 6kHz you would want a half period of 1000000us / 2 / 6000Hz = 83.3us which is obviously not a multiple of 50. The way this is handled is to take the floor of the number of the desired half period divided by 50us. Thus, setting a frequncy of 6kHz will result in an output frequency of 10kHz. (Note: the library could be altered to handle an arbitrary high frequency below 10kHz by changing the update time; however, this would break other frequencies.) If you have thoughts about how to improve this behavior, let me know. To install this library, first download the code from https://github.com/gregfriedland/SoftwareTone/zipball/master. Unzip the downloaded zip file and copy the SoftwareTone directory to <ArduinoDirectory>/libraries. On Mac OS X, this would be at ~/Documents/Arduino/libraries. Usage is: init(): initialize the library before using it. setFreq(pin, frequency): set the frequency of the given pin. to turn off, set the frequency to 0. This library depends on the TimerOne library and you must include <TimerOne.h> in your sketch. (This should be unnecessary because this file is included in the library also but it's an idiosyncrasy of the Arduino environment AFAIK.) If you don't include this header, you will get a compile timer error such as: SoftwareTone/SoftwareTone.cpp:8:22: error: TimerOne.h: No such file or directory SoftwareTone/SoftwareTone.cpp: In member function 'void SoftwareToneClass::init()': SoftwareTone/SoftwareTone.cpp:21: error: 'Timer1' was not declared in this scope An example sketch is available in the examples directory. Happy tone generation!
About
Arduino library to generate square waves on all pins
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published