I use this script http://devilqube.blogspot.nl/2013/02/using-ultrasonic-range-finder-with.html to get values from my ultrasonic sensor. When I run the code and print the distances it results in a lot zeros with sometimes 165.7 as value. Im sure the GPIO setup is correct. Can someone help me with solving this problem?

  • 2
    why java, what stops you from using python?
    – lenik
    May 27, 2014 at 9:02
  • Im in a project group and our group has more experience with java so we dediced to use java.
    – user15105
    May 27, 2014 at 9:03
  • 1
    Java is probably even worse than Python at this task. Check that your target is a metre or so away from the sensor. A common mistake is for the echo pulse to have finished before the software gets a chance to check it started.
    – joan
    May 27, 2014 at 10:23
  • The java code takes the difference in times and multiplies that by 165.7. When the difference is 0 you get zeros, When it's 1 you get 165.7. May 29, 2014 at 13:42

1 Answer 1


I have written some java classes to use ultrasonic sensor with one of my project. Here it is.


import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.*;

//to control pwm pins
//pwm1 23,24 ; pwm0 1, 26
import com.pi4j.wiringpi.Gpio;
import com.pi4j.wiringpi.SoftPwm;

public class PiJavaUltrasonic{
    //bcm, GPIO_#
    private int PIN_ECHO,PIN_TRIG;
    private long REJECTION_START=1000,REJECTION_TIME=1000; //ns

    private GpioController gpio;//gpio controller ; using io.gpio
    private GpioPinDigitalOutput//gpio output pins; using io.gpio, digital pins
    private GpioPinDigitalInput

    public PiJavaUltrasonic(int ECHO, int TRIG, long REJ_START,long REJ_TIME){ //GPIO

        gpio=GpioFactory.getInstance();// create gpio controller , io.gpio

        //motor_1_left_en=gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01, "motor_1_left_en", PinState.HIGH);
        pin_trig=gpio.provisionDigitalOutputPin(RaspiPin.getPinByAddress(PIN_TRIG), "pin_trig", PinState.HIGH);//pin,tag,initial-state
        pin_trig.setShutdownOptions(true, PinState.LOW);



    public int getDistance() throws Exception{ //in milimeters
            int distance=0; long start_time, end_time,rejection_start=0,rejection_time=0;
            //Start ranging- trig should be in high state for 10us to generate ultrasonic signal
            //this will generate 8 cycle sonic burst.
            // produced signal would looks like, _|-----|
            pin_trig.low(); busyWaitMicros(2);
            pin_trig.high(); busyWaitMicros(10);

            //echo pin high time is propotional to the distance _|----|
            //distance calculation
            while(pin_echo.isLow()){ //wait until echo get high
                busyWaitNanos(1); //wait 1ns
                if(rejection_start==REJECTION_START) return -1; //infinity

            while(pin_echo.isHigh()){ //wait until echo get low
                busyWaitNanos(1); //wait 1ns
                if(rejection_time==REJECTION_TIME) return -1; //infinity

            distance=(int)((end_time-start_time)/5882.35294118); //distance in mm


    public static void busyWaitMicros(long micros){
        long waitUntil = System.nanoTime() + (micros * 1_000);
        while(waitUntil > System.nanoTime()){

    public static void busyWaitNanos(long nanos){
        long waitUntil = System.nanoTime() + nanos;
        while(waitUntil > System.nanoTime()){



public class Test_Ultrasonic{
    public static void main(String[] args) throws Exception{
        PiJavaUltrasonic sonic=new PiJavaUltrasonic(
            0,//ECO 11
            1,//TRIG 22
            1000,//REJECTION_START ; long
            23529411 //REJECTION_TIME ; long
            System.out.println("distance "+sonic.getDistance()+"mm");
            Thread.sleep(1000); //1s


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.