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: http://bit.ly/2eB1O2K

Github Repository : http://bit.ly/2j95Osp
Schematic : http://bit.ly/2ihvK5F
Pi4J GPIO Numbering : http://bit.ly/2ja20av
Download Pi4J Library : http://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: http://amzn.to/2iJ3Z24 (India)
Buy Raspberry Pi: http://amzn.to/2jlIOUH (India)

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

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

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

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

Related posts

Leave a Comment