Sonar distance sensor

This invention demonstrates how to connect a sonar distance sensor to your ThinkerShield. A sonar distance sensor sends out an ultrasonic sound which bounces off objects and returns to the sensor to tell how far away any objects are, very similar to echolocation in animals. Sonar sensors can be used for greater distances than infrared sensors but are less detailed.

This invention uses the NewPing library¬†which is not automatically enabled when you install Arduino. You will need to install the NewPing library by going to Tools > Manage Libraries > search “NewPing” > click “Install”. For this invention I used alligator clip to female jumpers to attach the sensor. You may use normal alligator clips for this but you will need to bend the sensor pins to fit them neatly.

Please note – The NewPing library does not work if you are also using the buzzer.

 

Step 1

Step 1

Connect first alligator clip to GND (ground) on your ThinkerShield and to the GND pin on your sonar sensor.

Step 2

Step 2

Connect second alligator clip to external pin D4 on your ThinkerShield and to the echo pin on your sonar sensor.

Step 3

Step 3

Connect third alligator clip to external pin D2 on your ThinkerShield and to the trigger pin on your sonar sensor.

Step 4

Step 4

Connect fourth alligator clip to external pin VCC on your ThinkerShield and to the VCC pin on your sonar sensor.

Step 5

// include the sonar library
#include <NewPing.h>

//declare the sonar sensor pins
int trigger = 2;
int echo = 4;

//declare the sonar sensor
NewPing sonar (trigger, echo);

void setup() {
 // put your setup code here, to run once:
 Serial.begin(9600);
}

void loop() {
 // put your main code here, to run repeatedly:
 int distance = sonar.ping_cm();
 Serial.println (distance);
 
}

Add a comment