Challenge

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)

Ultrasonic sensor

  • An ultrasonic sensor is an instrument that measures the distance to an object using ultrasonic sound waves. An ultrasonic sensor uses a transducer to send and receive ultrasonic pulses that relay back information about an object's proximity.
  • Ultrasonic transducers operate at frequencies in the range of 30–500 kHz for air-coupled applications. As the ultrasonic frequency increases, the rate of attenuation increases. Thus, low-frequency sensors (30–80 kHz) are more effective for long-range, while high-frequency sensors are more effective for short-range.

Arduino UNO

  • Arduino UNO is a low-cost, flexible, and easy-to-use programmable open-source microcontroller board that can be integrated into a variety of electronic projects.
  • This board can be interfaced with other Arduino boards, Arduino shields, and Raspberry Pi boards and can control relays, LEDs, servos, and motors as an output.

1K potentiometer

  • The 1K Pot Potentiometer is a three-terminal variable resistor that is used to vary or change the resistance.
  • It is normally used to control any device.
  • The 10K Potentiometer is used for adjusting various parameters like voltage, current, etc in an electronic circuit.

Goals:

  • Measuring the distance of an object using Ultrasonic Distance Sensor (HC-SR04)
  • Usage of Input Capture Mode

Key Highlights of the Project:

  • Measuring the distance traveled by the wave to the object and gets bounced back from the object to sensor.
  • Measuring Pulse width using Input Capture Mode

Program Code

#include <avr/io.h>
#define F_CPU 16000000ul
#include <util/delay.h>
#define TRIG PB1
#define ECHO PB0
unsigned int pulse_width,TimerOverflow;
int main(void)
{
float distance = 0;//pulse_width = 0,
DDRB |= (1<<DDB1);//Configure the trig pin as output
while(1)
{
trigger();
init_timer();

distance = (34*pulse_width)/2;
Serial.print(distance);
Serial.println("cm");
_delay_ms(40);

}
}
void trigger()
{
Serial.begin(9600);
PORTB|= (1<<PORTB1);
_delay_us(15);
PORTB &= ~(1<<PORTB1);
}
void init_timer()
{
ACSR |= (0<<ACIC);
TIFR1 = (1<<ICF1);
TCCR1A |= (0<<WGM11) | (0<<WGM10);
TCCR1B |= (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (1<<CS12);
TIMSK1 |= (1<<ICIE1);
sei();
}
ISR(TIMER1_CAPT_vect)
{
pulse_width = ((ICR1 - 12)*10)/625; //usecondition//
}

Deliverables of this project:

  • The distance travelled by a wave to the object for which we want to measure the distance using Ultrasonic Distance Sensor (HC-SR04).
  • Measuring Pulse width using Input Capture Mode of Timer 1

Expected Outcomes:

I got the Knowledge of 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 the 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.

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.