Generating AUDIO ECHO using Atmega32 microcontroller


(video demo of echo generation using atmega32)
Introduction:
Hi,
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...

Working:
 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. 

Observation:
  • 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;
    adc_init();
    pwm_init();
    switch_enable();
    i = 0;j=1;
    while(1) {
        rd = adc_read() +  buf[j]*echo_level;
        rd/=8;
        if(rd>255)rd=255;
        _delay_us(90);
        buf[i] = rd;
        OCR0 = rd;
        if(!(PINB & (1<<PB1))) {
            if(echo_level < 4) {
                echo_level++;
                _delay_ms(300);
            }
            } else if(!(PINB & (1<<PB2))) {
            if(echo_level > 0) {
                echo_level--;
                _delay_ms(300);
            }
        }
        i++;
        if(i==1899)i=0;
        j++;
        if(j==1899)j=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;
    while(!ADIF);
    ADCSRA |= 1<<ADIF;
    retl = ADCL;
    reth = ADCH;
    reth<<=8;
    reth|=retl;
    return reth;
}

void pwm_init()
{
    TCCR0|=(1<<WGM00)|(1<<WGM01)|(1<<COM01)|(1<<CS00);
    DDRB|=(1<<PB3);
}
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...;-)
:100000000C942A000C943C000C943C000C943C0092
:100010000C943C000C943C000C943C000C943C0070
:100020000C943C000C943C000C943C000C943C0060
:100030000C943C000C943C000C943C000C943C0050
:100040000C943C000C943C000C943C000C943C0040
:100050000C943C0011241FBECFE5D8E0DEBFCDBF1D
:1000600017E0A0E6B0E001C01D92AC3CB107E1F79B
:100070000E943E000C94DB000C9400001F93CF9371
:10008000DF9380EC87B982E886B983B7896683BF3E
:10009000BB9A87B3897F87BB86E088BB14E041E0C9
:1000A00050E020E030E0A4E0B0E0369A349A64B149
:1000B00085B1F82FE0E0EA01C05ADF4FC881D0E0F7
:1000C000CA9FC001CB9F900DDA9F900D112470E064
:1000D0006E2B7F2B860F971F969587959695879504
:1000E00096958795C7E6D1E02197F1F700C000000B
:1000F0008F3F910519F010F08FEF90E0E901C05AA1
:10010000DF4F88838CBFB19911C0143051F52F5F38
:100110003F4F97E02B36390709F14F5F5F4FC7E03C
:100120004B365C0711F640E050E0BFCFB299EFCFFD
:1001300011166CF711508FEF95EAAEE081509040A8
:10014000A040E1F700C00000A12FBB27A7FDB0959C
:100150002F5F3F4F97E02B363907F9F620E030E06C
:10016000DCCF1F5F8FEF95EAAEE081509040A0405A
:10017000E1F700C00000A12FBB27A7FDB095C7CFB6
:1001800080EC87B982E886B9089587B3897F87BBF9
:1001900086E088BB0895369A349A24B135B1932FFE
:1001A00080E030E0282B392BC901089583B7896698
:0A01B00083BFBB9A0895F894FFCFB7
:00000001FF

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

44 comments :

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

    ReplyDelete
  2. Hi!

    Could you adapt your code for Arduino please?

    Lazy Guy

    ReplyDelete
  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!

    ReplyDelete
  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!).
    Thanks.

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

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

    ReplyDelete
  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!

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

    ReplyDelete
  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.

    ReplyDelete
  10. What are the fuse bit settings for 16mhz

    ReplyDelete
  11. Whether for employment as an Electronics Engineering Technician or for further pursuance of a Bachelors Degree, it is always better to go for an accredited course. Cleveland Institute of Electronics (CIE), Penn Foster Career School, and Grantham University are three institutions that offer accredited Associate Degrees in the subject.generatore capacitivo alta frequenza

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. Hello, your project, it's really cool.
    Could you use a serial memory to improve the sampling?
      http://www.microchip.com/wwwproducts/en/23K256

    ReplyDelete
  14. A landfill is a terrible place for electronic devices, since they contain components that can pollute when they're damaged. So, you might throw away a water-damaged cell phone, but if its screen is cracked in the process, it'll end up releasing mercury. rc judge

    ReplyDelete
  15. Its Atmega32 or Atmel 32-tqfp

    ReplyDelete
  16. An audio mixer is an important electronic item also popular under the name of a mixing console, mixing desk, audio production console or a mixer. Best Audio Mixers

    ReplyDelete
  17. Other than this, nowadays numerous items are effortlessly accessible and can be gotten to in the online market.you could check here

    ReplyDelete
  18. can use an external ram spi for supply more ram?

    ReplyDelete
  19. Purchasing out an electronic item in a rush may in some cases turn out to be a wrong choice for which you need to atone later. Top questions to ask an entrepreneur

    ReplyDelete
  20. You may not know, but rather this individual is subject to it ordinary to make life simple. robot vacuum cleaner

    ReplyDelete
  21. Thus in the event that we remember the above things, at that point regardless of whether we are assuming the job of a purchaser or seller,we can undoubtedly prevail in the realm of discount.Bobsweep review

    ReplyDelete
  22. This gadget brags of a 3.2 super pixel camera and 2.2 inches wide TFT show board that indicates pictures to it clients in a goals of 240 x 320 pixels.Bobsweep pethair plus review

    ReplyDelete
  23. Science and technology extend society's learning. Science enables people to increase expanded comprehension of how the world functions, while technology enables researchers to make these disclosures.best hearing amplifier reviews

    ReplyDelete
  24. Discussions with different instructors are critical to resolving the subtleties and making ready toward genuine application in your class.source

    ReplyDelete
  25. specialists and representatives don't need to invest valuable energy and exertion looking reports and looking after records; robots Washington

    ReplyDelete
  26. really cool project Vinod! what IDE are you using to program the ATMEGA32?

    ReplyDelete
  27. You will put in a couple of more bucks for this errand. When you are simply leasing, you won't have issues like this one. conference av hire

    ReplyDelete
  28. Numerous sites effectively offer text to speech software to their online visitor, which has turned out to be an extraordinary help with regards to changing over composed archives into a sound arrangement. online text to audio converter

    ReplyDelete
  29. This comment has been removed by the author.

    ReplyDelete
  30. Contracting of this kind of gear requires proficient direction which you will discover at all online rental sites Orlando video production from American Audio Visual

    ReplyDelete
  31. (if without a doubt they ever do), implies that creators and creator side distributing legal counselors and diversion lawyers can exploit this crossroads ever. electrician Perth

    ReplyDelete
  32. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. OMC repair parts

    ReplyDelete
  33. Excellent post. I was reviewing this blog continuously, and I am impressed! Extremely helpful information especially this page. Thank you and good luck. bestsoundbarlab

    ReplyDelete
  34. Changed over electric cars can beIf you are thinking about doing the change to an electric vehicle, at that point there are a few things you ought to consider before beginning.used car dealerships near me

    ReplyDelete
  35. Elegant simplicity combined with state-of-the-art performance makes the Genuine 110 a fine choice for an AM/FM radio. In table top radio

    ReplyDelete
  36. Try not to let your utilized and broken electronics go to squander by giving them a chance to lounge around your home and get considerably increasingly pummeled, or more awful discarding them.Eye Massagers with Heat

    ReplyDelete