Skip to content
Chandima's Blog
What you think you become.

Primary

  • SAUSECODE.COM
  • Blog
  • .LinkedIn
  • .Github
  • .Hackerrank
  • .About me

A Java Class to Interface Ultrasonic Sensor (HC-SR04) with Raspberry Pi

Chandima April 16, 2018 embedded, home made, how to, java, pi4j, programming, raspberry pi class, embedded, gpio, hc-sr04, java, pi4j, raspberry pi, sensor, sonar, ultrasonic

I am working on an embedded system project (A Telepresence Robot) which the main program is written using Java. The single board computer we have used for this project is Raspberry Pi 3 model B with Raspbian OS. In order to detect and meaure distances to near by objects we have used few ultrasonic sensors.

I had to write a Java class to interface the HC-SR04 ultrasonic sensor with the Raspberry Pi using GPIO pins. To controll the GPIO pins I have used the pi4j library.

***

For the following sample program, I have used GPIO pin 0 as the ECHO PIN and GPIO pin 1 as the TRIG PIN.

The third argument is REJECTION_START (long). To explain you the purpose of this argument, let me breifly explain you how the distance measurement is done in HC-SR04 module.

First we need to keep the TRIG pin in high state for 10us. If the TRIG pin is kept in high state for 10us, the sensor generate a ultrasonic burst having 8 cycles and put the ECHO pin to high state. The state of ECHO is changed to low state once the reflection of transmitted ultrasonic burst reaches the sensor.

In practical conditions, sometimes eventhough we kept the TRIG pin in high state for 10us the sensor may not triggered to generate that ultrasonic burst. In such cases there is a possibilty that we might get incorrect readings. Therefore, after keeping the TRIG pin in high state for 10us the program will wait for REJECTION_START number of nano seconds and see whether the sensor is actually triggered. If not, it returns a negative value.

HC-SR04 can only be used to measure distances within a certain range. After refering the datasheets and depending on your requirement you could decide the maximum distance you are going to measure, and the associated time for that. You could set that time in nano seconds in the REJECTION_TIME (long) parameter.

See the GitHub Code

Test Program

public class Test_Ultrasonic{  
    public static void main(String[] args) throws Exception{  
         PiJavaUltrasonic sonic=new PiJavaUltrasonic(  
             0,//ECO PIN (physical 11)  
             1,//TRIG PIN (pysical 22)  
             1000,//REJECTION_START ; long (nano seconds)  
             23529411 //REJECTION_TIME ; long (nano seconds)  
         );  
         System.out.println("Start");  
         while(true){  
              System.out.println("distance "+sonic.getDistance()+"mm");  
               Thread.sleep(1000); //1s  
         }  
    }  
}
How to resolve the Opera Browser opening issue in Kali linux 2018PREV
Microsoft Visual Basic 6.0 For GCE O/L ICT, Sri Lanka (සිංහලින්) [Old Post]NEXT

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary

Search the blog!

Recent Posts

  • Familiarize with these tools before going to Industry May 8, 2021
  • How to let the scripts run in the background [ssh] June 6, 2020
  • NoSQL vs SQL Databases in a Nutshell June 5, 2020
  • SSH to a host through multiple jump hosts. [Linux] April 30, 2019
  • GNU – Octave [matlab equivalent for linux] – This is how you can install packages January 22, 2019

Categories

  • android (11)
  • applications (2)
  • apps (4)
  • arduino (2)
  • avr (1)
  • blog-android (2)
  • blog-arduino (1)
  • computer science (1)
  • database (1)
  • embedded (1)
  • emui (1)
  • games (1)
  • home made (2)
  • Honor 3c (6)
  • how to (15)
  • huawei (6)
  • java (4)
  • kali (7)
  • kitkat (1)
  • learn (10)
  • linux (11)
  • networking (6)
  • octave (1)
  • PC Apps (1)
  • pi4j (1)
  • programming (3)
  • pyqt (1)
  • python (2)
  • raspberry pi (1)
  • remote controlled (1)
  • Server (3)
  • tech (4)
  • tutorial (8)
  • Verilog (1)
  • windows (6)

Tags

2018 aero plane android Android 4.4.2 android app player android apps on windows class cross-platform file honor 3c honor 3c custom unicode honor 3c sinhala how how to how to install how to install custom unicode huawei honor 3c install custom font in honor 3c install custom unicode install unicode interfaces java kali language linux network opengl programming pyqt python remote run scripts server sinhala sinhala app sinhala for huawei sinhala installer sinhala unicode sinhala unicode for honor 3c sinhala unicode installer sinhala unicodes Sri Lanka ssh to

Archives

  • May 2021
  • June 2020
  • April 2019
  • January 2019
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • April 2018
  • March 2018
  • October 2017
  • February 2017
  • June 2016
  • July 2015
  • January 2015
  • December 2014
  • November 2014

"I hear and I forget, I see and I remember, I do and I understand." ~ Confucius

© Copyright 2020 SAUSECODE LABS | Chandima Samarasinghe