Introduction: Ultrasonic Sensor With the Arduino Chip

About: Omar Silva-Zapata is co-founder of Robo-Geek Inc.

This tutorial will walk you through on how to set up, connect, and program the Ultrasonic/Distance sensor.

But first lets learn a little bit about how it works.

You can see an image of the sensor above. There are two speaker looking things on a board. They are both used to determine the distance from an object directly in front of them.

The left "speaker" is a transmitter. It transmits ultrasonic waves. The waves propagate outward from the transmitter, hit an object, then bounce back to the sensor. This is where the right side comes in.

The right "speaker" is the receiver. It simply received the ultrasonic waves which the transmitter emits and bounce back.

Step 1: The Wiring

The wiring is done exactly like how it is in the image above. You will need an Arduino chip, the Ultrasonic sensor, 2 LEDs, and 2 resistors for the LEDs.

The wiring is as follows:

  • VCC on the Ultrasonic sensor goes to 5V
  • Trig on the Ultrasonic sensor goes to a Digital Pin
  • Echo on the Ultrasonic sensor goes to a different Digital Pin
  • GND on the Ultrasonic sensor goes to GND

Thats all for wiring the actual sensor. However, in order for us to see if something has reached in range of the sensor or not, we will need to connect LEDs:

  • LED short side goes to a resistor which goes to ground
  • LED long side goes to a Digital Port

Just setup 2 separate LEDs like that and we are good to go!

Step 2: The Code

Below is pasted the code used to make the Ultrasonic sensor work with the Arduino chip. The far LED will be on when there are no objects near the sensor, and off while there is an object within range. Vice-versa for the near LED.

Make sure to read the comment in order to fully understand what each line of the code does.

//Define the variables for the pins
#define trigPin 12 #define echoPin 10 #define nearLED 5 #define farLED 3

//Setup each pin with a respective Input or Output void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(nearLED, OUTPUT); pinMode(farLED, OUTPUT); }

void loop() { long duration, distance; ////////////////////////////////////////////// //This part triggers the transmitter to send out short bursts digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); //////////////////////////////////////////////

//Calculations to get the distance in centimeters duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1;

//If distance is less than 4 CENTIMETERS if (distance < 4) { // This is where the LED On/Off happens digitalWrite(nearLED,HIGH); digitalWrite(farLED,LOW); } else { digitalWrite(nearLED,LOW); digitalWrite(farLED,HIGH); } delay(200); }

Step 3: Troubleshooting

If your LEDs are not turning on and off as expected after following the above instructions, then try the steps below in order to troubleshoot your circuit.

The Wiring

  • Make sure all the wiring is correct
  • GND to GND
  • VCC to 5V
  • Double check if the LEDs are connect the correct way (short leg is negative/GND)

The Code

  • Make sure your code compiles without any errors
  • Make sure you have the LED pins, trig pin, and echo pin set up to the right number
  • Make sure the Arduino IDE is setup properly to work with your chip