In this article I will describe how to interface a Ultrasonic Range Finder Module with a AVR ATmega8microcontroller. I will provide a HEX file which you can burn into your ATmega8 directly to quickly test this whole setup.

Ultrasonic range finder modules helps find the exact distance to an object. This can have vast application in robotics.

These are more expensive than IR based obstacle detectors but the advantage is that they give accurate distance to the obstacle and also their performance is not affected by the colour of obstacle.

Connect the Ultrasonic Sensor Module
Connect the Ultrasonic Sensor Module with AVR Dev Board

  Sensor AVR Dev Board
  1 VCC------------------ VCC
  2 Trig ------------------PC0
  3 Echo----------------- PC1
  4 GND---------------- GND
Schematic

Part List

S. No. Reference Value Part Qty
1 R1 100 Ohms Resistor 1/4 watt 1
2 R2 330 Ohms Resistor 1/4 watt 1
3 R3 4.7K Ohms Resistor 1/4 watt 1
4 C1 470uF Electrolytic Capacitor 25V 1
5
C2, C5, C6, C7

0.1uF (100nF) Ceramic disk type capacitor 4
6 C3, C4 22pF Ceramic disk type capacitor 2
7 D1 1N4007 Diode 1
8 D2 - 5mm LED - Round - RED 1
9 U1 ATmega8 ATmega8 MCU 1
10 U2 7805 5V Voltage Regulator 1
11 L1 10uH Inductor 1
12 RV1 10K Ohms Preset (Variable Resistor) 1
13 SW1 - On / Off Switch 1
14 X1 - DC Socket 1
15x3 16MHz Crystal 1
16 X4 HC-SR04 Sensor 1
17 - - 28 PIN IC Socket 1
18 - - 16 PIN Female Connecter 1
19 - - 4 PIN Female Connecter 1
20 - - 16 PIN Male Connecter 1
21 DISP1 - LCD Module 16 × 2 GREEN B / L 1


Code
WRITTEN BY:
Mechatronics Lab
********************************************************************/

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

#include "lib/lcd/lcd.h"

/********************************************************************

Configuration Area.
UltraSonic (US) sensor connection.

in this example it is connected to as follows

Sensor | MCU
_____________
Trig   | PC0
Echo   | PC1

********************************************************************/

#define US_PORT PORTC
#define US_PIN PINC
#define US_DDR DDRC

#define US_TRIG_POS PC0
#define US_ECHO_POS PC1


/********************************************************************

This function measures the width of high pulse in micro second.

********************************************************************/

#define US_ERROR -1
#define US_NO_OBSTACLE -2

void HCSR04Init();
void HCSR04Trigger();

void HCSR04Init()
{
US_DDR|=(1<<US_TRIG_POS);
}

void HCSR04Trigger()
{
//Send a 10uS pulse on trigger line
US_PORT|=(1<<US_TRIG_POS); //high
_delay_us(15); //wait 15uS
US_PORT&=~(1<<US_TRIG_POS); //low
}

uint16_t GetPulseWidth()
{
uint32_t i,result;

//Wait for the rising edge
for(i=0;i<600000;i++)
{
if(!(US_PIN & (1<<US_ECHO_POS))) 
continue; //Line is still low, so wait
else 
break; //High edge detected, so break.
}

if(i==600000)
return US_ERROR; //Indicates time out
//High Edge Found

//Setup Timer1
TCCR1A=0X00;
TCCR1B=(1<<CS11); //Prescaler = Fcpu/8
TCNT1=0x00; //Init counter

//Now wait for the falling edge
for(i=0;i<600000;i++)
{
if(US_PIN & (1<<US_ECHO_POS))
{
if(TCNT1 > 60000) break; else continue;
}
else
break;
}

if(i==600000)
return US_NO_OBSTACLE; //Indicates time out

//Falling edge found

result=TCNT1;

//Stop Timer
TCCR1B=0x00;

if(result > 60000)
return US_NO_OBSTACLE; //No obstacle
else
return (result>>1);
}
void main()
{
uint16_t r;
_delay_ms(100); //Let the LCD Module start

//Initialize the LCD Module
LCDInit(LS_NONE);
//Set io port direction of sensor
HCSR04Init();

LCDClear();
LCDWriteString("Ultra Sonic");
LCDWriteStringXY(0,1,"Sensor Test");

_delay_ms(2500);
LCDClear();


while(1)
{
//Send a trigger pulse
HCSR04Trigger();

//Measure the width of pulse
r=GetPulseWidth();

//Handle Errors
if(r==US_ERROR)
{
LCDWriteStringXY(0,0,"Error !");
}
else if(r==US_NO_OBSTACLE)
{
LCDWriteStringXY(0,0,"Clear !");
}
else
{
int d;

d=(r/58.0); //Convert to cm

LCDWriteIntXY(0,0,d,4);
LCDWriteString(" cm");

_delay_ms(500);
}
}

}

Full Project Click Here

No comments

Theme images by Dizzo. Powered by Blogger.