Introduction
An ultrasonic distance sensor is a device that uses sound waves to measure the distance to an object. It is a non-contact sensor, meaning it can measure the distance to an object without physically touching it. Ultrasonic distance sensors are commonly used in various applications, including robotics, security systems, and automation. So now that you know about this sensor, let us create an Arduino Ultrasonic Distance Sensor.
To make this Arduino project, we will use Ultrasonic Distance Sensor to glow LEDs wrt the object’s distance from the sensor, using Arduino.
Supplies
To make Arduino Ultrasonic Distance Sensor, we will require the following components:
Components
- Arduino Uno R3
- Ultrasonic Distance Sensor
- 1 Small Breadboard
- 3 LEDs
- 3 Resistors
- Connecting wires
Circuit Diagram
Steps To Make Arduino Ultrasonic Distance Sensor
Step 1: Gather the components on the Digital Board or Physical Table.
Step 2: Plug the LED and Ultrasonic Distance Sensor into the BreadBoard.
LED:
Step 3: Connect the Cathode Terminal of the LEDs to the resistor end and the resistor’s second end to the Arduino’s Ground(GND) pin using a black-colored wire.
Step 4: Connect the Anode terminal of the LEDs to the 3, 5, & 7 number pins on the Digital side of Arduino, respectively.
Ultrasonic Distance Sensor:
Step 5: Connect the Power terminal of the Ultrasonic Distance Sensor to the 5V pin of the Arduino at its Analog Side.
Step 6: Connect the Trigger Terminal of it to the 9-number pin of the Arduino.
Step 7: Connect the Echo Terminal of it to the 8-number pin of the Arduino.
Step 8: Connect the Ground Terminal of it to the GND pin of the Arduino.
Source Code
int distance= 0; int cm = 0; long readUltrasonicDistance(int triggerPin, int echoPin) { pinMode(triggerPin, OUTPUT); digitalWrite(triggerPin, LOW); delayMicroseconds(3); digitalWrite(triggerPin, HIGH); delayMicroseconds(10); digitalWrite(triggerPin, LOW); pinMode(echoPin, INPUT); return pulseIn(echoPin, HIGH); } void setup() { Serial.begin(9600); pinMode(3, OUTPUT); pinMode(5, OUTPUT); pinMode(7, OUTPUT); } void loop() { distance = 330; cm = 0.01723 * readUltrasonicDistance(9, 8); Serial.print(cm); Serial.print("cm, "); if (cm > distance) { digitalWrite(7, LOW); digitalWrite(5, LOW); digitalWrite(3, LOW); } if (cm <= distance && cm > distance - 100) { digitalWrite(7, HIGH); digitalWrite(5, LOW); digitalWrite(3, LOW); } if (cm <= distance - 100 && cm > distance - 230) { digitalWrite(7, HIGH); digitalWrite(5, HIGH); digitalWrite(3, LOW); } if (cm <= distance - 230) { digitalWrite(7, HIGH); digitalWrite(5, HIGH); digitalWrite(3, HIGH); } delay(1000); }
Explanation Of The Code
1. At the beginning, we initialized two variables to 0.
2. After that, we use a function that takes two values from the sensor and produces the Ultrasonic distance.
3. In the second function, namely the setup function, we are setting the serial connection speed to 9600 bits per second. And then configuring the pin numbers 3, 5, & for Output purposes.
4. In the last function, we are taking the distance to 330 cm. And finding the distance of the object in cm using the formula.
5. After this, we use the if statement to glow the number of LEDs as per the object’s distance from the sensor. All three LEDs will glow if the distance is less than 100cm. If the distance is between 100 and 230, two will glow, or only one will glow.
6. We have concluded the code with a delay function, which takes time in milliseconds.
Output
On changing the object’s distance from the sensor, the number of LEDs that will glow will also change respectively.
In the above output, the distance is less than 100 cm. Hence all three LEDs are glowing.

Cisco Ramon is an American software engineer who has experience in several popular and commercially successful programming languages and development tools. He has been writing content since last 5 years. He is a Senior Manager at Rude Labs Pvt. Ltd.
0 Comments