Challenge

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

EMBEDDED LIVE BATCH - 19 (ABINAYA MAAM) :

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

Overview of the Project :

In this project, the implementation of code for “Measuring distance of an object using ultrasonic distance sensor (HC-SR04). Here, the distance travelled by the wave to the object and gets bounced back from the object to sensor is measured using Ultrasonic Distance Sensor (HC-SR04), along with the pulse width using Input Capture Mode.

Expected Outcomes :

The students will get the Knowledge on Ultrasonic sensor (HC-SR04) to measure the distance travelled by wave to the object, the hardware connection of Atmega328p and HC-SR04 and how to use input capture mode of Timer1.

ANSWERS :

SIMULATION DIAGRAM :

The hardware selected for simulation are: Atmega 32, LCD, Ultrasonic sensor (HC-SR04), potentiometer and an oscilloscope.

MAIN PROGRAM :

/* Implementation of code for Measuring distance of an object using ultrasonic distance sensor (HC-SR04)
 * Created: 04-04-2023 18:12:26
 * Author : arjun
 */ 

#include <avr/io.h> // Pre-processor directive for I/O registers
#define F_CPU 16000000l // CPU frequency
#include <util/delay.h> // Pre-processor directive for delay functions
#include "avr/interrupt.h" // User defined pre-processor directive for interrupts

#define lcd_port PORTB //	Port B pins used for LCD data
#define RS_high PORTD |= (1<<0) // D0 pin for Register select
#define RS_low PORTD &=~ (1<<0)
#define EN_high PORTD |= (1<<1) // D1 pin for Enable
#define EN_low PORTD &=~ (1<<1)
unsigned int tov=0, count, distance;

void lcd_cmd(unsigned char cmd) // For command register
{
	lcd_port = cmd;
	RS_low;
	EN_low;
	_delay_ms(1);
	EN_high;
}

void lcd_data(unsigned char data) // For data register
{
	lcd_port = data;
	RS_high;
	EN_low;
	_delay_ms(1);
	EN_high;
}

void lcd_init()
{
	lcd_cmd(0x38); // 8 bit LCD mode
	_delay_ms(5);
	lcd_cmd(0x02); // Return cursor to home
	_delay_ms(5);
	lcd_cmd(0x01); // Clear screen
	_delay_ms(5);
	lcd_cmd(0x0C); // Display ON, cursor OFF
	_delay_ms(5);
	lcd_cmd(0x06); // Increment cursor
	_delay_ms(5);
	lcd_cmd(0x80); // Cursor to begin at the 1st line
	_delay_ms(5);
	lcd_cmd(0xC0);
	_delay_ms(5); // Cursor to begin at the 2nd line
}

void cursor_position(char x, char y)
{
	if ((x<1||x>2)||(y>16||y<1))
	{
		x = 1;
		y = 1;
	}
	if (x == 1)
	{
		lcd_cmd(0x7F+y);
	}
	else
	{
		lcd_cmd(0xBF+y);
	}
}

void lcd_out(char x, char y, char *str)
{
	cursor_position(x, y);
	while (*str)
	{
		lcd_data(*str);
		str++;
	}
}

void lcd_print(char row, char column, unsigned int value, int digits)
{
	unsigned int temp, unit, tens, hundred, thousand, million;
	unsigned char flag = 0;
	
	if (row==0 || column==0)
	{
		cursor_position(1,1);
	}
	else
	{
		cursor_position(row, column);
	}
	
	if (digits==5 || flag==1)
	{
		million = value/10000+48;
		lcd_data(million);
		flag = 1;
	}
	if (digits==4 || flag==1)
	{
		temp = value/1000;
		thousand = temp % 10+48;
		lcd_data(thousand);
		flag = 1;
	}
	if (digits==3 || flag==1)
	{
		temp = value/100;
		hundred = temp % 10+48;
		lcd_data(hundred);
		flag = 1;
	}
	if (digits==2 || flag==1)
	{
		temp = value/10;
		tens = temp % 10+48;
		lcd_data(tens);
		flag = 1;
	}
	if (digits==1 || flag==1)
	{
		unit = value % 10+48;
		lcd_data(unit);
	}
	if (digits>5)
	{
		lcd_data('e');
	}
}

int main(void)
{
	DDRB = 0xFF; // Declaring pins B0 to B7 as outputs
	DDRD |= (0<<6); // Declaring D6 pin as input from echo pin of sensor
	DDRC |= (1<<0); // Port C0 as output
	sei(); // Global interrupt
	lcd_init();
	TCCR1A = 0x00; // Normal mode
	TIMSK |= (1<<2);
	 
    while (1) 
    {
		PORTC |= (1<<0); // Trigger sensor
		_delay_ms(15);
		PORTC &=~ (1<<0);
		
		TCCR1B = 0x41; // Enabling ICF
		TCNT1 = 0;
		TIFR = (1<<ICF1);
		TIFR = (1<<TOV1);
		tov = 0;
		
		while ((TIFR & (1<<ICF1))==0); // Checking the timer overflow conditions
		TCNT1 = 0;
		TCCR1B = 0x01;
		TIFR = (1<<ICF1);
		TIFR = (1<<TOV1);
		tov = 0;
		
		while ((TIFR & (1<<ICF1))==0);
		count = ICR1 + (65535*tov); // Calculating the count for the time
		distance = (unsigned int)count/932.87; // Sensor datasheet formula
		lcd_print(1,1,distance,4);
		lcd_out(1,6, "cm");
    }
}

ISR(TIMER1_OVF_vect)
{
	tov++;
}

OUTPUT :

When the potentiometer is varied, the distance of the wave travelled also varies as shown below;

RESULTS :

  • Simulated the model of an Ultrasonic sensor (HC-SR04) to measure the distance travelled by wave to the object, along with the hardware connection of Atmega328p and HC-SR04.
  • To use input capture mode of Timer1.
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.