Generating AUDIO ECHO using Atmega32 microcontroller

(video demo of echo generation using atmega32)
While I was studying at 10th standard, I used to play with small electronic circuits mostly based on transistor bc547 - bc557 pair. At that time, I just asked myself, I can amplify audio signals using few transistor combinations, but how can I make an echo effect which I used to hear in almost all loudspeaker announcements? I can't imagine what I can do with few transistors and resistors to make such an effect! I have no answer at that time because it was beyond my limitation..
  But now I can do this very easily by a simple digital signal processing using a microcontroller. It's concept is very simple, ie we need to apply a proper delayed feedback in digital samples with in a circular buffer. I did this using an atmega32 microcontroller and it worked fine. This is simple but really an interesting project. Not only an echo, but we can do a lot of fun with this type of small DSP experiments if we have considerably large RAM in the mcu...

 I am using an Atmega32 microcontroller for the purpose. It is having RAM of 2KB and an ADC which is enough for demonstrating the concept of echo generation. An electert mic is used for capturing the voice. It is introduced to ADC with proper amplification and level shifting, which are more critical for the perfect operation. Now the ADC module inside the AVR will convert the analog signal to digital signal at a particular sampling rate(which we can decide).  Now a 1900byte circular buffer is introduced. Our first aim is to make a delay in input and output audio. So, we can do one thing, ie we can populate the buffer from one end and we can read the buffer from another point so that there will be a delay! So how to make this delay to it's maximum? I think it will be better to explain it via a diagram.
uint8_t buf[1900]; 

Now, the next process is to make echo effect. For this,we need to provide a feedback from reading point to writing point. In simple words, we need to add a scaled version of reading signal along with the writing sample. The feedback gain should always be <1. So, now the digital signal in the buffer is processed by adding an echo effect. We can provide the reading samples to PWM of AVR to get the pulse width modulated signal. This could be demodulated by using a simple RC filter and can be provided to a power amplifier and we can notice an echo effect in the audio...

In my demo code, I also provided a 5 level echo selection via external push button, ie echo 0 to echo 4. 

We can do a lot of adjustments here. 

  • If we increase the sampling rate, the echo time gap and echo duration will be reduced but the audio quality will be increased. 
  • If we are decreasing the sampling rate, the echo time gap and echo duration will be increased. 
  • If we increase the feedback gain (but always < 1), the echo volume and echo number will be increased..
  • If we increase the feedback gain above 1, a totally disturbed audio is obtained due to oscillation.
  • To obtain a very good audio quality with good echo effect, both the echo buffer (RAM) and the sampling rate should be high..... Atmega2560 may be best for the purpose because it have 8KB RAM.

Circuit diagram:

 Now, for an audio amplifier, just search in google, we will get a lot of circuits which suits for our speaker specifications. So, I am not putting the diagram here. Still couldn't find it, just mail me. I will suggest suitable amplifier for your speaker.

 Source code:
#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>

uint8_t buf[1900];

void pwm_init();
void adc_init();
uint16_t adc_read();
void switch_enable();

void main()
    int i,j;
    int8_t echo_level = 4;
    uint16_t rd;
    i = 0;j=1;
    while(1) {
        rd = adc_read() +  buf[j]*echo_level;
        buf[i] = rd;
        OCR0 = rd;
        if(!(PINB & (1<<PB1))) {
            if(echo_level < 4) {
            } else if(!(PINB & (1<<PB2))) {
            if(echo_level > 0) {

void adc_init()
    ADMUX = 0b11000000;
    ADCSRA =0b10000010;

void switch_enable()
    DDRB &= ~((1<<PB1)|(1<<PB2));
    PORTB = (1<<PB1)|(1<<PB2);

uint16_t adc_read()
    uint16_t retl,reth;
    ADCSRA |= 1<<ADSC;
    ADCSRA |= 1<<ADIF;
    retl = ADCL;
    reth = ADCH;
    return reth;

void pwm_init()
To compile it in linux, check my previous post of the atmega32 based wav player. I have explained it in detail about the command line, the makefile etc...

Here is the hex for lazy guys...;-)

If you have any questions, you can ask me at [vinodstanur at gmail dot com]


  1. Very nice work! I saw your project on!

  2. Hi!

    Could you adapt your code for Arduino please?

    Lazy Guy

  3. Really cool!

    I see you use a 324 opamp, so you have three unused opamps, if you want to increase the audio quality (with this chip) you could split up the signal, so the Dry signal, the original one is not running trough the AVR, so it's quality is independent of the sampling rate/bit depth, and run the output from the avr and the original signal into a virtual earth mixer.

    Then you could even decrease the bit depth/sampling rate and give a longer lo-fi delay effect with the original quality dry signal.

    Or even use analog feedback of the delay signal, this could give a much better control over the amount of the delay and you can put additional filter in the feedback loop, so the "older" signal is less high freqs, like in an analog delay.

    And that would also give the ability to chain more smaller,cheaper AVR-s after each other for longer delays!

  4. Nice job ! Very inspiring. I did it on an atmega328 and I have some remarks.
    1. Why using two counters ? You don't need to start writing at 0 and start reading at 1. Both can be done from 0 by using only one counter (i in your case).
    The other remarks are a bit silly:
    2. By doing the test of the counters after incrementation at1899 you are loosing one sample. The test should be at 1900.
    3. On your figure, it should be 1899 not 1889. (actually, that's more than silly!).

  5. Great work. but how could be extend the delay? in audio buffer? which is buf[1900]? should i increase the array?

  6. Здравствуйте. Собрал я это устройство. Но у меня все гудит и звенит. Что я мог сделать не так? Подскажите какие фьюз биты нужно установить? Я использовал стандартные. И на входе у меня не микрофон, а гитара. Когда я бью по струнам гитару если если слышно в очень искаженно виде. Помогите. Я начинающий.

  7. Hi! I'm Rolando from Peru, I saw your video and it really impressed me!...My question is wheter I can generate a 8 bit song with an atmega88. I'm planing to implement a guitar hero simulator. Help me please! Any idea you can give me will be helpful, well I'll wait for your answer; thanks!

  8. Hi ,can you implemen on a tiva c launchpad? (tm4c123)
    thanks in advance

  9. oh THANKYOU. :) i've been banging my head on the wall trying t work out circular buffers and delays for a project adding reverb (echos) to a wav file playing from an SD card. Your example has solved my problem!! Thanks again.