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


void setup()


  pinMode(ledPin, OUTPUT);




void loop()


  pinMode(pingPin, OUTPUT);

  digitalWrite(pingPin, LOW);


  digitalWrite(pingPin, HIGH);


  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);


  duration = pulseIn(pingPin, HIGH);

  distanceCm = microsecondsToCentimeters(duration);



  Serial.println (distanceCm);




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)


Leave a Reply

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

You are commenting using your 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