Aim:
In this article I will describe how to interface a Ultrasonic Range
Finder Module with a AVR ATmega8 microcontroller. I will provide a HEX
file which you can burn into your ATmega8 directly to quickly test this
whole setup.
Description:
We know that sound vibrations can not penetrate through solids. So what
happens is, when a source of sound generates vibrations they travel
through air at a speed of 220 meters per second. These vibrations when
they meet our ear we describe them as sound. As said earlier these
vibrations can not go through solid, so when they strike with a surface
like wall, they are reflected back at the same speed to the source,
which is called echo.
Ultrasonic sensor “HC-SR04” provides an output signal
proportional to distance based on the echo. The sensor here generates a
sound vibration in ultrasonic range upon giving a trigger, after that it
waits for the sound vibration to return. Now based on the parameters,
sound speed (220m/s) and time taken for the echo to reach the source, it
provides output pulse proportional to distance.
As shown in figure, at first we need to initiate the sensor for
measuring distance, that is a HIGH logic signal at trigger pin of sensor
for more than 10uS, after that a sound vibration is sent by sensor,
after a echo, the sensor provides a signal at the output pin whose width
is proportional to distance between source and obstacle.
Block Diagram
Schematic
Code
// ****************************************************** // Project: Ultrasonic sensor using atmega8 // Author: Hack Projects India // Module description: Operate ultrasonic sensor // ****************************************************** #define F_CPU 8000000UL // Set Clock frequency to 8Mhz #include <avr/io.h> #include <util/delay.h> #define Data PORTB #define RS_en() PORTB |=(1<<PB4) #define RS_clr() PORTB &= ~(1<<PB4) #define E_en() PORTB |=(1<<PB5) #define E_clr() PORTB &= ~(1<<PB5) void InitLCD(); void LCDCmd(unsigned char); void LCDData(unsigned char); void LCDWriteString(const char *); void InitLCD() { lcd_cmd(0x02); _delay_ms(2); lcd_cmd(0X28); _delay_ms(2); lcd_cmd(0X06); _delay_ms(2); lcd_cmd(0X01); _delay_ms(2); lcd_cmd(0X0c); _delay_ms(2); lcd_cmd(0X80); _delay_ms(2); } void LCDCmd(unsigned char cmd) { RS_clr(); E_en(); _delay_us(100); Data|=(cmd>>4); _delay_us(100); E_clr(); Data&=~(cmd>>4); _delay_us(100); E_en(); _delay_us(100); Data|=(cmd&0x0f); _delay_us(100); E_clr(); Data&=~(cmd&0x0f); } void LCDData(unsigned char dat) { RS_en(); E_en(); _delay_us(100); Data|=(dat>>4); _delay_us(100); E_clr(); Data&=~(dat>>4); _delay_us(100); E_en(); _delay_us(100); Data|=(dat&0x0f); _delay_us(100); E_clr(); Data&=~(dat&0x0f); _delay_us(100); } void LCDWriteString(const char *str) { while((*str)!='\0') { LCDData(*str); str++; } } void LCDGotoXY(uint8_t x,uint8_t y) { if(x<40) { if(y) x|=0b01000000; x|=0b10000000; LCDCmd(x); } } void LCDClear() { LCDCmd(0x01); } unsigned int ultra(unsigned char pin1,unsigned char pin2) { unsigned int value; DDRB = (1<<PB3)|(0<<PB0); //echo low trigr high PORTB &= 0x00 ; TCCR1B = 0 ; TCNT1 &= 0; PORTB= (1<<pin1); //echo high _delay_ms(10); PORTB = (0<<pin1); //echo LOW while((PINB & pin2) == 0x00); //trigger 0 if((PINB & pin2) == pin2) { TCCR1B = 0x02; } while((PINB & pin2) == pin2); value = TCNT1 ; return(value); } int main(void) { unsigned int f; DDRB=0xff; DDRD=0xff; InitLCD(); _delay_ms(100); while(1) { LCDCmd(0x80); LCDWriteString("Ultrasonic"); f=ultra(PB3,0x01); //taking distance(ultrasonic) _delay_ms(100); LCDCmd(0xc0); LCDWriteInt(f,4); _delay_ms(100); } }
Downloads:
The code was compiled in Atmel Studio 6 and simulation was made in Proteus v7.7.
To download code and proteus simulation click here.
Further Reading suggestions:
You may also like,
- Interfacing keypad with AVR
- nterfacing DAC with AVR
- Interfacing with UART of AVR controller
- Interfacing SPI communication with AVR
- AVR Displaying Custom Characters on LCD
- AVR Graphical LCD
- RTC interfacing using I2C in AVR
- Interfacing GPS Modu with AVR
- Interfacing GSM Module with AVR
- Interfacing PWM in AVR
- Interfacing ADC with AVR
- Scrolling string on LCD using AVR
- Interfacing keypad with AVR
- nterfacing DAC with AVR
- Interfacing with UART of AVR controller
- Interfacing SPI communication with AVR
- AVR Displaying Custom Characters on LCD
- AVR Graphical LCD
- RTC interfacing using I2C in AVR
- Interfacing GPS Modu with AVR
- Interfacing GSM Module with AVR
- Interfacing PWM in AVR
- Interfacing ADC with AVR
- Scrolling string on LCD using AVR
No comments:
Post a Comment