Arduino XMAS hitcounter

Christmas is coming closer, so here is my contribution to put you in the right mood.

It is a blog hitcounter, that rings a bell. Literally. It puts a smile on your face, every time someone hits your blog. And it is a great way to annoy your colleages or your girl friend.

It consists of an Arduino board, a bell, a servo and a couple of lines of code in c, python and php.

Arduino XMAS hitcounter

Continue reading

DIY Tengu on a breadboard

When I first saw Crispin Jones Tengu, I was sure, I must have one. If you don’t know tengu and don’t want to follow the link, it’s a small face, made of LEDs, that reacts to music and sound.
It did not take long until I decided to clone this funny little device. All it needs is a microcontroller, an LED matrix and a sound sensor.

Tengu clone

Continue reading

Cheap Sound Sensor for AVR

I searched the web for a cheap method to let the ATmega respond to sound. My knowledge in analog circuits is very limited, but what I do remember, is that you can not attach a electret mircophone to a controller pin. The signal of the microphone is just too small and has to be amplified. There is much of information out there, especially on diy amplifiers. I stumbled upon this little circuit here. It consists only of a handful of components:

  • 2 resistors 10k
  • 1 resistor 100k
  • transistor 2N3904
  • 1 capacitor 0.1u
  • electret microphone

Prototyping the amplifier

Putting things together on a breadboard.

IMGP1255

Actually I had no 2N3904 around, so I replaced it with a BC337. The circuit is a emitter circuit with voltage degeneration (I dont know if that exists in english). I dropped the couple capacitor and took the signal right away at the collector.

Prototyping with the ATmega

The sound sensing is done with the ADC of the ATmega. A simple program reads the analog value of the amplifier over and over. If the value (loudness) exeeds a specific level, an LED is lit.
IMGP1253

The schematic for rebuilding.
mic-schematic

Here is the code. It is just hacked together and has lots of room for improvements.

/* -----------------------------------------------------------------------
 * Title:    sound sensor
 * Author:   Alexander Weber
 * Date:     19.05.2007
 * Hardware: ATmega8
 * Software: WinAVR 20060421
 * 
 */

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

#define LED_BIT PD4

/*
 * get_adc
 * Return the 10bit value of the selected adc channel.
 */
uint16_t get_adc() {

	uint16_t value;

	// warm up the ADC, discard the first conversion
	ADCSRA |= (1 << ADSC);
	while (ADCSRA & (1 << ADSC)); 
	value = ADCW;
	
	ADCSRA |= (1 << ADSC);				// start single conversion
	while (ADCSRA & (1 << ADSC)); 		// wait until conversion is done

	return ADCW;
}

int main(void) {

	uint8_t i = 0;
	
	DDRD |= 0x1c;	// PD2-PD3: col 6-7, PD4: debug LED

	// select channel
	ADMUX = 5;

	// ADC setup
	ADCSRA = 
		(1 << ADEN) |						// enable ADC
		(1 << ADPS1) | (1 << ADPS0);		// set prescaler to 8	
			
	// say hello	
	for (i = 0; i < 5; i++) {
		PORTD |= (1 << LED_BIT);
		_delay_ms(10);
		_delay_ms(10);
		_delay_ms(10);
		_delay_ms(10);
		_delay_ms(10);
		PORTD &= ~(1 << LED_BIT);
		_delay_ms(10);
		_delay_ms(10);
		_delay_ms(10);
		_delay_ms(10);
		_delay_ms(10);
	}
	_delay_ms(10);
	_delay_ms(10);


	while (1) {
		
		if (get_adc() > 180) {
			PORTD |= (1 << LED_BIT);
			_delay_ms(10);
			PORTD &= ~(1 << LED_BIT);
		}			
		
	}

	return 0;

}

Video

The video got lost :(

Conclusion

It was easy and worked pretty well. I enjoyed looking at the LED responding to the music. I haven’t recorded anything with this amplifier, it might sound awfull. Next steps could be playing the sound back or be able to analyze the sound (FFT). And (re-)learning more on analog circuit design.

Links