Measure Distance using Ultrasonic Sensor HC – SR04 | Pi4J | JAVA | Raspberry Pi

uLTRASONI pI

HC - SR04This is a simple tutorial showing how to interface HC – SR04  ultrasonic ranging module with Raspberry Pi to measure distance using JAVA. Ultrasonic ranging module HC – SR04 provides 2cm – 400cm non-contact measurement function, the ranging accuracy can reach to 3mm. The modules include ultrasonic transmitters, receiver and control circuit. The basic principle of work:

(1) Using IO trigger for at least 10us high-level signal,
(2) The Module automatically sends eight 40 kHz and detect whether there is a
pulse signal back.
(3) IF the signal back, through high level, time of high output IO duration is the time from sending ultrasonic to returning.


Links :
Java Application on Pi Playlist: bit.ly/2eB1O2K

Github Repository : http://bit.ly/2j95Osp
Schematic : bit.ly/2ihvK5F
Pi4J GPIO Numbering : bit.ly/2ja20av
Download Pi4J Library : bit.ly/2j32blF
Code : http://bit.ly/2oeMhHi

import com.pi4j.io.gpio.*;


public class Main {
  //GPIO Pins
  private static GpioPinDigitalOutput sensorTriggerPin ;
  private static GpioPinDigitalInput sensorEchoPin ;
  
  
  final static GpioController gpio = GpioFactory.getInstance();
  
  public static void main(String [] args) throws InterruptedException{
    new Main().run();
  }
  public void run() throws InterruptedException{
    sensorTriggerPin =  gpio.provisionDigitalOutputPin(RaspiPin.GPIO_00); // Trigger pin as OUTPUT
    sensorEchoPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02,PinPullResistance.PULL_DOWN); // Echo pin as INPUT

    while(true){
      try {
      Thread.sleep(2000);
      sensorTriggerPin.high(); // Make trigger pin HIGH
      Thread.sleep((long) 0.01);// Delay for 10 microseconds
      sensorTriggerPin.low(); //Make trigger pin LOW
    
      while(sensorEchoPin.isLow()){ //Wait until the ECHO pin gets HIGH
        
      }
      long startTime= System.nanoTime(); // Store the surrent time to calculate ECHO pin HIGH time.
      while(sensorEchoPin.isHigh()){ //Wait until the ECHO pin gets LOW
        
      }
      long endTime= System.nanoTime(); // Store the echo pin HIGH end time to calculate ECHO pin HIGH time.
    
      System.out.println("Distance :"+((((endTime-startTime)/1e3)/2) / 29.1) +" cm"); //Printing out the distance in cm  
      Thread.sleep(1000);
      
    } catch (InterruptedException e) {
      e.printStackTrace();
      }
    }
  }
}

 

Buy Sensor: amzn.to/2iJ3Z24 (India)
Buy Raspberry Pi: amzn.to/2jlIOUH (India)

********************************************************************
Subscribe YouTube : goo.gl/FhfdL7

Guys Subscribe to my channel for latest contents into your inbox.
Support me to keep going.

Support me on Patreon : bit.ly/2jcjTSo
___________________________________________

Website : www.weargenius.in
Twitter : twitter.com/geekybikash
YouTube : www.youtube.com/weargenius
Instagram : www.instagram.com/weargenius/
GIT : github.com/oksbwn
Facebook: www.facebook.com/geekybikash

Suvendu Swain
Catch Me On

Suvendu Swain

Blogger/ Educationist at WeArGenius
A person passionate about tech stuffs.
Suvendu Swain
Catch Me On

Latest posts by Suvendu Swain (see all)

Related posts