I was recently asked for some help from someone having problems using 2 PING Ultrasonic range sensors. I promised to send some pictures of the setup as I hadn’t provided information on this in my previous post. So just in case anyone else is having problems, here they are. Hope they can help.





hi
thanks for the reply, is there any chance you can look at my arduino code and see if there is anything noticeably wrong with it.
/* New Variables */
int delay_time = 20; // delay for this amount each write cycle.
byte printing_byte = 0;
int pingpina = 7; // Ultrasound signal pin
int pingpinb = 8; // Ultrasound signal pin
int vala = 0;
int valb = 0;
int pingValuea = 0;
int pingValueb = 0;
int timecounta = 0;
int timecountb = 0;
int ledPin = 13; // LED connected to digital pin 13
void setup() {
beginSerial(9600); // Sets the baud rate to 9600
pinMode(ledPin, OUTPUT); // Sets the digital pin as output
}
void loop() {
timecounta = 0;
timecountb = 0;
vala = 0;
valb = 0;
pinMode(pingpina, OUTPUT);
pinMode(pingpinb, OUTPUT); // Switch signalpin to output
/* Send low-high-low pulse to activate the trigger pulse of the sensor
* ——————————————————————-
*/
digitalWrite(pingpina, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(pingpina, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(pingpina, LOW); // Holdoff
delayMicrosenconds(10); // delay between the sends of pulse to each sensor
digitalWrite(pingpinb, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(pingpinb, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(pingpinb, LOW); // Holdoff
/* Listening for echo pulse
* ——————————————————————-
*/
pinMode(pingpina, INPUT); // Switch signalpin to input
vala = digitalRead(pingpina); // Append signal value to val
while(vala == LOW) { // Loop until pin reads a high value
vala = digitalRead(pingpina);
delayMicroseconds(10);
pinMode(pingpinb, INPUT); // Switch signalpin to input
valb = digitalRead(pingpinb); // Append signal value to val
while(valb == LOW) { // Loop until pin reads a high value
valb = digitalRead(pingpinb);
}
while(vala == HIGH) { // Loop until pin reads a high value
vala = digitalRead(pingpina);
timecounta = timecounta +1;
delayMicroseconds(10);
while(valb == HIGH) { // Loop until pin reads a high value
valb = digitalRead(pingpinb);
timecountb = timecountb +1; // Count echo pulse time
}
/* Writing out values to the serial port
* ——————————————————————-
*/
pingValuea = timecounta; // Append echo pulse time to ultrasoundValue
/* BEGIN EDITED CODE */
pingValuea = pingValuea – 14;
pingValuea = pingValuea / 5;
if(pingValuea > 127) {
}
else {
printing_byte = pingValuea;
Serial.print(printing_byte);
}
/* END EDITED CODE */
/* Lite up LED if any value is passed by the echo pulse
* ——————————————————————-
*/
if(timecounta > 0){
digitalWrite(ledPin, HIGH);
}
/* Delay of program
* ——————————————————————-
*/
delay(40);
}
or can i have a look at yours to see how to do it right please, i’ve been trying to learn c++ from no background what so ever and finding it really hard do achieve what i want somtimes, i can walk but i’ll never be competing in marathon!
To be honest with you, my range sensor experiment was the only project in which I have used Arduino. My code is an edited version of what I found here – http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor.
Hope it helps.
Incidentally the code used to interact with the Arduino microprocessor is Processing – http://processing.org. It is a simplified version of Java built for Artists and Designers to learn Programming.
/* Ultrasound Sensor
*——————
*
* Reads values (00014-01199) from an ultrasound sensor (3m sensor)
* and writes the values to the serialport.
*
* http://www.xlab.se | http://www.0j0.org
* copyleft 2005 Mackie for XLAB | DojoDave for DojoCorp
*
*/
int ultraSoundSignal1 = 6; // Ultrasound signal pin
int ultraSoundSignal2 = 7; // Ultrasound signal pin
int val1 = 0;
int val2 = 0;
int ultrasoundValue1 = 0;
int ultrasoundValue2 = 0;
int timecount1 = 0; // Echo counter
int timecount2 = 0; // Echo counter
int ledPin= 13; // LED connected to digital pin 13
void setup() {
Serial.begin(9600); // Sets the baud rate to 9600 – was Serial.begin(9600);
pinMode(ledPin, OUTPUT); // Sets the digital pin as output
}
void loop() {
getRange( ultraSoundSignal1, val1, ultrasoundValue1, timecount1, ‘A’ );
getRange( ultraSoundSignal2, val2, ultrasoundValue2, timecount2, ‘B’ );
delay(100);
}
void getRange( int ultraSoundSignal, int val, int ultrasoundValue, int timecount, char id )
{
timecount = 0;
val = 0;
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
/* Send low-high-low pulse to activate the trigger pulse of the sensor
* ——————————————————————-
*/
digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff
/* Listening for echo pulse
* ——————————————————————-
*/
pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
val = digitalRead(ultraSoundSignal); // Append signal value to val
while(val == LOW) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
}
while(val == HIGH) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
timecount = timecount +1; // Count echo pulse time
}
/* Writing out values to the serial port
* ——————————————————————-
*/
ultrasoundValue = timecount; // Append echo pulse time to ultrasoundValue
serialWrite(id); // Example identifier for the sensor
printInteger(ultrasoundValue);
//serialWrite(10);
//serialWrite(13);
/* Lite up LED if any value is passed by the echo pulse
* ——————————————————————-
*/
if(timecount > 0){
digitalWrite(ledPin, HIGH);
}
/* Delay of program
* ——————————————————————-
*/
//Serial.print( val );
}
thank you very much, I have been asking around the arduino forum and have got some clear responses as well, thank you for your help and i will post a video over christmas hopefully if i can get it working you have been a god send.
Hi James,
I am currently working on a project which uses 2 ping ultrasensors in combination with Arduino (and processing).
I was wondering if you used some kind of triangulation method in your work, for example the one with the particles?
No I didn’t use triangulation because the signal from the sensor wasn’t wide enough. If for example I placed one sensor on each of the top corners of the monitor, the triangulation area would be a small patch in the centre of the screen. Good luck with it though. I’d be interested to see how you get on. Can you Email me a link when you’re finished?