Ping)) Sensor Problem

Hey All,
I just got my Ping))) sensor in the mail and hooked it up. Everything seemed to work ok at my desk but when I took it downstairs to impress my wife with it, it didn't quite work. I've got 3 led's connected to it to show the distance detected; red light is on when the object is < 12" away from the sensor, yellow light shows 12" - 36" and green light is on when sensor detects +36" distance. I'm printing out the distance to the Serial port in my code also.

So when this is powered through my usb cable, things work great but when I power it through my 5v power supply that came with the Arduino Mega, everything works except for the green light (the one that detects +36"). The red and yellow lights work but the green doesn't.

Here's a video of the problem: Ping Sensor Problem - YouTube

Here's the code:

int trigPin = 2;
int echoPin = 3;
int redPin = 10;
int yellowPin = 11;
int greenPin = 12;

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(greenPin, 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:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  if(inches < 12) {
    
    digitalWrite(redPin, HIGH);
    digitalWrite(yellowPin, LOW);
    digitalWrite(greenPin, LOW);
  
  } else if(inches > 11 && inches < 36) {
    
    digitalWrite(redPin, LOW);
    digitalWrite(yellowPin, HIGH);
    digitalWrite(greenPin, LOW);    
  
  } else if(inches > 35) {
    
    digitalWrite(redPin, LOW);
    digitalWrite(yellowPin, LOW);
    digitalWrite(greenPin, HIGH); 
  
  } else {
    // There's an error so have all of them come on.
    digitalWrite(redPin, HIGH);
    digitalWrite(yellowPin, HIGH);
    digitalWrite(greenPin, HIGH); 
  }
  
  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;
}

Here's something else I just discovered:
I disconnected my Mega and hooked this setup up to my Ardiono Nano. When I hook it up to my computer via mini usb, it works and when I hook it up through a mini usb power cable to the wall, it works! So this tells me it's got something to do with being hooked up to the Mega using external power, right?

Hi,

Sounds like you're starving for power. On the spec page for the Mega it says it needs 6V-20V at the limits (http://arduino.cc/en/Main/ArduinoBoardMega2560) so I'm thinking if your wall wart is 5V as you've typed, it's struggling to power your circuit.

Cheers !
Geoff

But those specs you posted say "Operating Voltage 5V". I guess I'm not sure what the difference is between "Operating Voltage" and "Input Voltage (limits)"

emooney:
But those specs you posted say "Operating Voltage 5V". I guess I'm not sure what the difference is between "Operating Voltage" and "Input Voltage (limits)"

operating voltage refers to what powers the pins on the board, the ATmega processor, etc. The input voltage is what you need to feed it so it can maintain that. Of course when it is fed into the board it is first passed through a voltage regulator, and there are some losses which is why 5V input wont do the job. The exception to the rule is the USB which doesn't go through the voltage regulator so doesn't lose any voltage before being used by the arduino.

Hope that makes sense, Geoff