Challenge

Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)

Measuring distance of an object using ultrasonic sensor (HC-SR04)

THEORY:

PROJECT OVERVIEW:

  • We are going to implementation of code for “Measuring distance of an object" using ultrasonic distance sensor (HC-SR04) and Atmega32 Microcontroller in which real time distance is sensed by an ultrasonic sensor and displays measured distance on an LCD display by using the SimulIDE.

GOALS:

Measuring the distance of an object using Ultrasonic Distance Sensor (HC-SR04):

  • The ultrasonic distance sensor has 4 Pins: Vcc, GND, Trigger and Echo. The Vcc and GND are used to power up the sensor and are connected to power and ground rails on breadboard. Trigger is connected to PIN 40 (ADC0/PA0) and Echo is connected to PIN 0 (T0/PB0) of the microcontroller.

PROJECT KEY HIGHLIGHTS:

Measuring the distance travelled by the wave to the object and gets bounced back from the object to sensor:

  • Controller has to trigger the sensor to start transmitting the ultrasonic waves. To trigger the ultrasonic sensor,controller has to send a high signal to the Trig pin.Trig pin - connected to PA0, Echo pin - connected to PBO(ICSPI)
    In pin - That pin is for simulating the object distance.The input voltage in this pin is directly proportional to the
    object distance.
  • Now the ultrasonic transmitter on the sensor will emit a short burst automatically.If there is an object in the range of ultrasonic sensor, then the transmitted wave will bounce back towards the sensor and the ultrasonic receiver will receive that echo.
  • When the ultrasonic wave transmission starts,the Echo pin is held high by the sensor until the ultrasonic sensoy, receives the echo pulse.
  • After it receives the echo, Echo pin is pulled down. The pulse width of that HIGH pulse at Echo pin is the time
    taken by the wave to travel to the object and gets bounced back from the object to the sensor.
  • we have to capture the pulse width in order to measure the distance of the object.

PROJECT DELIVERABLES:

  • By this project we used to monitor the distance between ultrasonic sensor and object and it will display that monitored distance value on LCD display.
  • Microcontroller ATmega32 is used to control the whole process.
  • Ultrasonic sensor will be having transmitter and receiver in it. Transmitter continuously transmits the signal and whenever obstacle approaches the sensor then that transmitted signal hits the object, bounce back and received by ultrasonic receiver.
  • That sensor sensed signal with respect to distance is then send to microcontroller, microcontroller in turn displays the measured distance value on LCD.

CODE:

#include <avr/io.h>
#define F_CPU 16000000ul
#include <util/delay.h>
#include <avr/interrupt.h>
#include <string.h>
#include <stdlib.h>


#define lcd_data_dir DDRD //Define LCD data port direction
#define lcd_command_dir DDRC //Define LCD command port direction register
#define lcd_data_port PORTD //Define LCD data port
#define lcd_command_port PORTC //Define LCD data port
#define EN PC2 //Define Enable signal pin
#define RW PC3 //Define Read/Write signal pin
#define RS PC4 //Define Register Select(data reg./command reg.)signal pin

void lcd_command(char);/LCD command write function
void lcd_char(char);/LCD data write function
void lcd_init(void);/LCD Initialize function
void lcd_string(char*);/Send string to LCD function
void lcd_string_xy (char,char,char*); //Send row,position and string to LCD function

void lcd_command (char cmd) //LCD command write function
{
lcd_data_port = cmd; //Write command data to LCD data port
lcd_command_port&=~((1<<RS)|(1<<RW)); //Make RS LOW(command reg.),RW LOW(Write)
lcd_command_port|= (1<<EN); //High to Low transition on EN(Enable)
_delay_ms(1);
lcd_command_port&=~(1<<EN);
_delay_ms(2); //Wait little bit

}
void lcd_char(char char_data) //LCD data write function
{
lcd_data_port= char_data; //Write data to LCD data port
lcd_command_port&=~(1<<RW); //Make RW LOW(Write)
lcd_command_port|=((1<<EN)|(1<<RS)); //Make RS HIGH(data reg.)and High to Low transition on EN(Enable)
_delay_ms(1);
lcd_command_port&=~(1<<EN);
_delay_ms(1);
}
void lcd_init(void) //LCD Initialize function
{
lcd_command_dir|= (1<<RS)|(1<<RW)|(1<<EN); //Make LCD command port direction as o/p
lcd_data_dir = 0xff; //Make LCD data port direction as o/p
_delay_ms(20); //LCD power up time to get things ready,it should always>15ms
lcd_command(0x38); //Initialize 16X2 LCD in 8bit mode
lcd_command(0x0C); //Display ON,Cursor OFF command
lcd_command(0x06); //Auto Increment cursor
lcd_command(0x01); //Clear LCD command
lcd_command(0x80); //8 is for first line and0is for 0th position
}
void lcd_string(char *str) //Send string to LCD function
{
int i;
for(i=0;str[i]!=0;i++) //Send each char of string till the NULL
{
lcd_char (str[i]); //Call LCD data write
}

}
void lcd_string_xy(char row,char pos,char *str) //Send string to LCD function
{
if (row == 1)
lcd_command((pos & 0x0f)|0x80); //Command of first row
else if (row == 2)
lcd_command((pos & 0x0f)|(0xC0)); //Command of Second row
lcd_string(str); //Call LCD string function
}
#define trigger_pin PA0 //Trigger pin
int TimerOverFlow = 0;
ISR(TIMER1_OVF_vect)
{
TimerOverFlow++; //Increment Timer Overflow count
}
int main (void)
{
char string[10];
long count;
double distance;

DDRA = 0x01; //Make trigger pin as output
PORTB = 0xff; //Turn on Pull-up

lcd_init();
lcd_string_xy(1,0,"ultrasonic");

sei(); //Enable global interrupt
TIMSK = (1<< TOIE1); //Enable Timerl overflow interrupts
TCCR1A = 0; //Set all bit to zero Normal operation

while(1)
{
//Give 10us trigger pulse on trig.pin to HC-SR04
PORTA |= (1<< trigger_pin);
_delay_ms(10);
PORTA &=(~(1<< trigger_pin));

TCNT1 = 0; //Clear Timer counter
TCCR1B = 0x41; //Capture on rising edge,No prescaler
TIFR = 1<<ICF1; //Clear ICP flag(Input Capture flag)
TIFR = 1<<TOV1; //Clear Timer Overflow flag
//Calculate width of Echo by Input Capture(ICP)
while ((TIFR & (1<<ICF1)) == 0); //Wait for rising edge
TCNT1 = 0; //Clear Timer counter
TCCR1B = 0x01; //Capture on falling edge,No prescaler
TIFR = 1<<ICF1; //Clear ICP flag(Input Capture flag)
TIFR = 1<<TOV1; //Clear Timer Overflow flag
TimerOverFlow = 0; //Clear Timer overflow count

while ((TIFR & (1<< ICF1))== 0); //Wait for falling edge
count = ICR1 +(65535 * TimerOverFlow); //Take count
distance = (double)count/466.47;
//8MHz Timer freq,sound speed=343m/s
dtostrf(distance,2,2,string); //distance to string
strcat(string,"cm"); //Concat unit Le.cm
lcd_string_xy(2,0,"dist= ");
lcd_string_xy(2,7,string); //Print distance
_delay_ms(200);
}
}

OUT PUT:

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.