This library is good for any Atmega328P board to generate square waves at controllable volumes. It replaces a mechanical sound pot by programattically setting the volume via a menu and up/down buttons, rotary encoder etc. It can even modulate the sound with attack, sustain and decay to (see examples inlcuded in the library).
This is how it works:
- Timer0 produces a PWM signal at a fixed frequency beyond the audible spectrum (62.5 KHz).
- Timer2 produces a square wave whose frequency can be set by the application.
- TImer2 gates the output of TImer0 (pass if high, clear if low).
- The volume of the square wave of TImer2 will depend on the duty cycle of Timer0 (set by the application).
The sound is output to the OC0B pin (D5). Add a simple RC filter and you're set.

Example code to generate a frequency with a given volume:
To install the library, go to your sketches folder and create a "library" subfolder (if not yet there). Extract the "VolumeSquareWaveNano" folder to said subfolder. Inside there's an "examples" subfolder with three simple demo sketches.
VolumeSquareWaveNano.zip
Heartbeat:
Simple bell-like modulation of two tones:
This is how it works:
- Timer0 produces a PWM signal at a fixed frequency beyond the audible spectrum (62.5 KHz).
- Timer2 produces a square wave whose frequency can be set by the application.
- TImer2 gates the output of TImer0 (pass if high, clear if low).
- The volume of the square wave of TImer2 will depend on the duty cycle of Timer0 (set by the application).
The sound is output to the OC0B pin (D5). Add a simple RC filter and you're set.
Example code to generate a frequency with a given volume:
Code:
#include "VolumeSquareWaveNano.h" VolumeSquareWaveNano v; void setup() { // put your setup code here, to run once: v.init(); } void loop() { // Just a frequency at 50% volume v.setFrequency(2000); v.setVolumePercent(50); while(1); }
VolumeSquareWaveNano.zip
Heartbeat:
Simple bell-like modulation of two tones:
Comment