PING ultrasound sensor

Ping sensor is a lot easier to test than PIR, believe me. I swapped parts with my classmate Lauri from Tero’s class

Lauri’s code below:

/*

PING ultraäänisensorin testaus käyttäen lediä

Lauri Soivi
__________________________________________________________

PING ultrasound sensor test with a led by Lauri Soivi

*/

 

const int pingPin = 7;

const int ledPin = 13;

long int duration, distanceCm;

int limitCm = 20
;//distance

 

void setup()

{

  pinMode(ledPin, OUTPUT);

  Serial.begin(115200);

}

 

void loop()

{

  pinMode(pingPin, OUTPUT);

  digitalWrite(pingPin, LOW);

  delayMicroseconds(2);

  digitalWrite(pingPin, HIGH);

  delayMicroseconds(5);

  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);

 

  duration = pulseIn(pingPin, HIGH);

  distanceCm = microsecondsToCentimeters(duration);

 

  checkLimit();

  Serial.println (distanceCm);

  delay(100);

}

 

void checkLimit()

{

  if (distanceCm < limitCm){

    digitalWrite(ledPin, HIGH);  

  } else {

    digitalWrite(ledPin, LOW);

  }

}

 

long microsecondsToCentimeters(long microseconds)

{

  return microseconds / 29 / 2;

}

if you get readings that it is hitting something closer than it should, make sure the sensor isn’t hitting something directly beneath it. I had to place the sensor on the edge of the test board because of “weird” readings.

The led is set to slot 13 and ground directly on the UNO, the ping’s wiring is the following:

UNO Wire Color PING
Slot 7 Orange SIG (Signal)
5V Yellow 5V
GND (Ground) Green GND (Ground)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s