Wednesday, March 3, 2021

Arduino and ultrasonic range finder HC-SR04

Overview of HC-SR04 Distance Sensor

HC-SR04 is a ranging module, measuring a distance using sound wave ranging from 20 cm to 400 cm. Its accuracy is 3 mm.

Arduino reading a distance from a non-contact ultrasonic range finder HC-SR04
A running program on bread-board


This sensor come with four pins. The basic components are a mic and speaker built on-board as shown in the picture.

Arduino reading a distance from a non-contact ultrasonic range finder HC-SR04


Arduino reading a distance from a non-contact ultrasonic range finder HC-SR04
Front Panel Of HC-SR04 I bought from TaydaElectronics


Device pin diagram from datasheet


Arduino reading a distance from a non-contact ultrasonic range finder HC-SR04
Signal Timing


To use this device:

  1. Raise Trigger pin high in TTL 5V for 10 uS.
  2. The sensor will send back a 40 kHz signal determine the device is working.
  3. Get the HIGH time response by the sensor, using a the microcontroller timer
  4. Pull Trigger pin low
  5. Calculate for distance

The distance formula (in meters) is (high level time * 340m/S)/2

Arduino Interfacing And Programming

Using the Arduino Advanced I/O function, we can get the timing of any input level fed to Arduino digital pins.

The pulseIn() function store the timing in microsecond of any input high or low.

The syntax is:

pulseIn(pin, value)
pulseIn(pin, value, timeout)

Where, 

  • pin is any digital input pin
  • value is a logic value - HIGH or LOW  
This function is long data type. It return a timing value in micro seconds.

This program tests the module and send distance data to serial terminal.

Arduino reading a distance from a non-contact ultrasonic range finder HC-SR04
Test Program

Now I get the distance from this device, and display the distance to my SPI character LCD.

Arduino reading a distance from a non-contact ultrasonic range finder HC-SR04
Schematic

Arduino Source Code:

#include <ShiftedLCD.h>
#include <SPI.h>

//Select pin 8 for Enable pin
LiquidCrystal lcd(8);

const int trigPin=5;
const int echoPin=3;

/*
PWM OUTPUT 0-25000uS in which 1cm=50uS
*/

long   duration;
int   distance;

void setup()
 { // put your setup code here, to run once:
  pinMode(trigPin,OUTPUT);
  pinMode(echoPin,INPUT);
  Serial.begin(9600);

  lcd.begin(16,4);
  lcd.clear();

  lcd.setCursor(0,0);
  lcd.print("HC-SR04 Ultra");
  lcd.setCursor(0,1);
  lcd.print("Sonic Sensor");
  delay(2000);
 }

void loop()
 {  // put your main code here, to run repeatedly:
  digitalWrite(trigPin,LOW);
  delayMicroseconds(7);
 
  // Set the trigPin high for 10 uS
  digitalWrite(trigPin,HIGH);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin,LOW);
  duration=pulseIn(echoPin,HIGH);
  // Calculation for distance
  if(duration<=25000)
  distance = duration * 0.034 / 2;
  else
  distance=0;
  Serial.println("Distance Measured : ");
  Serial.print(distance);
  Serial.println(" cm");

  //lcd.clear();
  lcd.setCursor(-4,2);
  lcd.print("Distnace: ");

  lcd.setCursor(-4,3);
  lcd.print(String(distance)+" cm   ");
 
  duration=0;
  distance=0;
  delay(1000);
 }

Project picture:

Arduino reading a distance from a non-contact ultrasonic range finder HC-SR04
A running program on bread-board
Arduino sketch could be downloaded here.



No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (54) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (46) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (66) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (2) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (30) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)