How do Ultrasonic sensors work? Java

What value is the Ultrasonic sensor looking for? Generally Analog or Digital Inputs/Outputs are looking for a channel integer but the Ultrasonic sensor requires two integers a pingChannel and an echoChannel, where do I find these values?

I’m assuming you’re using something like the vex ultrasonic sensor (otherwise you should be able to treat an ultrasonic like any other sensor).

In this case, I believe that the pingChannel is the PWM out pin that goes into the input wire and the echoChannel is the analog channel you read the final value on.

If he is in fact referring to the vex one, or the venerable HS-SR04, then the genreal process is to sent a 10 microsecond pulse on the trigger and then get the width of the pulse sent back out on the echo pin (both digital). The width of that response pulse is proportional to the distance.

We have an Arduino ultrasonic sensor.

http://www.radioshack.com/radioshack-ultrasonic-range-sensor/2760342.html#.VLWn7SvF-oE

It doesn’t have an input and an output is there a way we can still use it?

So there are several types of ultrasonic rangefinders (at least electrical output-wise). There are digital sensors with an output pin (echo channel) and input pin (ping channel). With this type you trigger the input pin and the output pin will respond with a pulse whose duration is proportional to the measured distance (this is the kind of sensor the Ultrasonic class in the WPILib is mainly concerned with). There are also analog sensors which constantly stream distance through a scaled voltage signal from an analog pin. The kind you have is yet another type: a digital sensor that uses the SIG pin as both an input and output pin. You have to trigger the pin, and then the same pin will respond with a pulse whose duration is proportional to the measured distance.

Implementing this code-wise: you may get away with using the same channel for the echo and ping pins in the constructor for the Ultrasonic object (or this might throw an error, never tried it before). I don’t have experience with this type of sensor, but that would be my first bet.

Electrically, just plug it into a DIO port.

This is the sample source code referenced in that sensor’s user guide:



/***************************************************************************/	
//	Function: Measure the distance to obstacles in front and print the distance
//			  value to the serial terminal.The measured distance is from 
//			  the range 0 to 400cm(157 inches).
//	Hardware: Ultrasonic Range sensor
//	Arduino IDE: Arduino-1.0
//	Author:	 LG		
//	Date: 	 Jan 17,2013
//	Version: v1.0 modified by FrankieChu
//	by www.seeedstudio.com
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
/*****************************************************************************/
#include "Arduino.h"
class Ultrasonic
{
	public:
		Ultrasonic(int pin);
        void DistanceMeasure(void);
		long microsecondsToCentimeters(void);
		long microsecondsToInches(void);
	private:
		int _pin;//pin number of Arduino that is connected with SIG pin of Ultrasonic Ranger.
        long duration;// the Pulse time received;
};
Ultrasonic::Ultrasonic(int pin)
{
	_pin = pin;
}
/*Begin the detection and get the pulse back signal*/
void Ultrasonic::DistanceMeasure(void)
{
    pinMode(_pin, OUTPUT);
	digitalWrite(_pin, LOW);
	delayMicroseconds(2);
	digitalWrite(_pin, HIGH);
	delayMicroseconds(5);
	digitalWrite(_pin,LOW);
	pinMode(_pin,INPUT);
	duration = pulseIn(_pin,HIGH);
}
/*The measured distance from the range 0 to 400 Centimeters*/
long Ultrasonic::microsecondsToCentimeters(void)
{
	return duration/29/2;	
}
/*The measured distance from the range 0 to 157 Inches*/
long Ultrasonic::microsecondsToInches(void)
{
	return duration/74/2;	
}

Ultrasonic ultrasonic(7);
void setup()
{
	Serial.begin(9600);
}
void loop()
{
	long RangeInInches;
	long RangeInCentimeters;
	ultrasonic.DistanceMeasure();// get the current signal time;
    RangeInInches = ultrasonic.microsecondsToInches();//convert the time to inches;
	RangeInCentimeters = ultrasonic.microsecondsToCentimeters();//convert the time to centimeters
	Serial.println("The distance to obstacles in front is: ");
	Serial.print(RangeInInches);//0~157 inches
	Serial.println(" inch");
	Serial.print(RangeInCentimeters);//0~400cm
	Serial.println(" cm");
	delay(100);
}
1 Like