Most of the AVR microcontroller have a timer with a PWM channel. With this project a tone is generated using the PWM channel of the microcontroller. By changing the PWM duty cycle different tones can be generated. The microcontroller used is the ATTiny2313 miucrocontroller.



The ATTiny2313 has four hardware PWM channels on Timer0 and Timer1, we use the PWM channel 0 (PB2) to drive a transisitor via a 2.2kohm resistor. A loudspeaker is connected to the collector of transistor and a 56ohm serie resistor limits the current. The loudspeaker then emits the sound that is generated by the microcontroller. 



The circuit is build on a breadboard and connected to the ATTiny2313 board wich can be purchased at

 Design code

The code runs at a clock frequency of 20Mhz. First the PWM channel is initiated, then in the infinite loop two tones are generated by changing the duty cycle of the PWM timer.


program name: tone generator
date: 24.04.2015
generate a two tone with PWM channel

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

int main (void)

DDRB = (1<<PB2); // output PB2 (OCA0)
TCCR0B = 0x05; // set prescaler clk/1024
TCCR0A|=((0<<COM0A1)|(1<<COM0A0)|(1<<WGM01)|(1<<WGM00)); // set PWM mode