Radio shack ultrasonic sensors

Hi everyone I am new to micro controllers and programming so I can really use some help here.
I just purchased a few radioshack ultra sonic sensors model: 2760342 and I plugged them into my arduino uno. Went through the steps of downloading and uploading the code to my arduino and opened the serial monitor to see if the sensor is working properly but it won't give me any distance values except 0inch, 0cm. I don't know what to do! Any help would be appreciated.

Thank you

A drawing of you're ACTUALLY connection and a copy of the code would help.

Weedpharma

Johnny make sure your connections are right. I had the same issue today and came to find that I had the SIG and Ground connections reversed. Changed them up and it now is reading distances.

@Weedpharma

Hey I attached two photos of my connections. I am basically connecting GRD on sensor to GRD on the arduino, VCC on sensor to 5V on arduino and SIG pin on sensor to D7 on arduino. A copy of the codes that I have tried is posted below. The first was a download from the actually radioshack link that was provided when I purchased the sensors. It is supposed to be compatible with arduino. The second one is from the arduino example file itself. Both sets give me the same result...0inch,0cm readings in serial port.

1st from radioshack website:

#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);
}

2nd from arduino program:

const int pingPin = 7;

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);
}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

Thank you

@09train

Oh man I've tried every orientation possible its still not working. At first I realized I had SIG pin and VCC switched up but changed and it and still same result.

The solution here was to add this to the loop ()

pinMode(_pin, OUTPUT);

@dlloyd

Not quite sure how to do that. Im only a noob =/ Can you be more specific?

void setup() {
// initialize serial communication:
Serial.begin(9600);

pinMode (7,OUTPUT);
}

This tells the Arduino that the pin is an output.

Weedpharma

Edit

From link above by dlloyd

James, I condensed the program to a single loop with about 10 statements and got the outputs
that you described. Then I realized that I had only set the pinMode to OUTPUT in the “Setup”
section. So, I actually did get ONE correct reading before it gave me all ZERO’S. After copying
the pinMode to OUTPUT statement into the main loop, it worked GREAT! Good luck.

So also put it at last statement.

From the comments on the previous link, they put pinMode in the loop ... try this (untested):

/***************************************************************************/
//	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()
{
  pinMode(7, OUTPUT);
  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);
}

@weedpharma @dlloyd

Hey guys I tried both and neither one is working.

Try pin 8 for the green wire ...
Otherwise, I'm out of suggestions other than returning the sensor if it's defective.

/***************************************************************************/
//	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(8);
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  pinMode(8, OUTPUT);
  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);
}

@dlloyd

Im sure its not the sensors because I have two others that do the same thing. Do you think it could be the board itself? When I was going through the projects in the kit I received I remember some projects didn't work and I couldn't find a solution as to why but now I'm thinking it must be the board.

Does the "blink" example work? (it uses pin 13).
If blink works, then try this (uses pin 13).

/***************************************************************************/
//	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(13);
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);
}

Alright guys I figured out the issue, but have come to face another. It seems that the arduino GRD and 5V pins are defective. When I use a lab power source I get distance readings in the serial port, but its very inconsistent and it only lasts for a few seconds before every reading turns into 57in. So I went from getting 0inch readings to 57 inch readings everytime. Any suggestions?

Thank You

So for those of you who read my other post, I was having trouble getting readings for the Ultrasonic sensors from radio shack on the arduino. I came to realize that the 5V and GRD pins on the arduino board itself were defective and every time I plugged them in, my readings would go back to 0in,0cm. However, now I am using a lab power source and the D7 pin on the arduino is still connected to the SIG pin on the sensor. I receive a few believable readings in the serial port, but it only lasts a few seconds before every reading turns into 57inch, no matter how far or close I put the sensor to a solid object. A copy of the two codes that I have been testing are printed below. Any help on this would sure make life a whole lot better.

Thank you

Code #1

const int pingPin = 7;

void setup()
{
// initialize serial communication:
Serial.begin(9600);
pinMode(pingPin, OUTPUT);

}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(500);
}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

Code #2

#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()

{
pinMode(7, OUTPUT);
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(500);

}

johnny405:
I came to realize that the 5V and GRD pins on the arduino board itself were defective

Exactly what gives you that impression? No voltage between the two with your meter, or what?

johnny405:
now I am using a lab power source and the D7 pin on the arduino is still connected to the SIG pin on the sensor..... I receive a few believable readings

Got the power supply ground hooked up to the Arduino ground? (Mind you that won't help if the Arduino ground is defective....)

Thanks for the code. I could not find it on RS website.
My US Range Sensor worked perfectly.

First, let me say that I'm very new to the Arduino. As a matter of fact my first attempt is also using the Ultrasonic sensor from Radio Shack. I have read all the posts and the only thing I'm doing different is on the line:
_pin = pin;
I change it to:
_pin = 12; //This is the actual pin number I'm using
Also I did not use on the loop the code pinMode(7, OUTPUT); just because I'm using the same pin for both input and output. This particular sensor only has one signal pin and I believe if I put it as output in the loop i will never get the correct response out of the sensor. Any way my sensor does work as expected. Hope this helps.

HELP PLEASE

I am working on an independent ultrasonic transmitter and receiver
I am able to generate the required pulse in the transmitter but not able to synch and retrieve data from the receiver side
I am using ZIGBEE s2 model for time synch

Thanks
regards
vj