Education

Education

Ultrasonic Sensor

What is an Ultrasonic Sensor?

An ultrasonic sensor is a special device that can measure distance without even touching anything! It works by sending out sound waves (that we can’t hear) and waiting to hear them bounce back—just like a bat or a dolphin navigating in the ocean!

Think of it like shouting into a cave and waiting for your echo to return. The longer it takes for the sound to come back, the further away the cave walls are!

  • Senses objects without touching them!

  • Works in the dark, unlike cameras!

  • Used in robots, cars, and even smart gadgets!

The Physics Behind Ultrasonic Sensors!

Let’s break it down!

1. Sending Out Sound Waves
  • The Ultrasonic Sensor has two small cylinders—one is a speaker (Transmitter), and the other is a microphone (Receiver).

  • The transmitter sends out an ultrasonic pulse (like a tiny clicking sound).

2. Waiting for the Echo
  • The sound wave travels through the air until it hits an object (like your hand ✋).

  • The sound bounces back to the sensor’s receiver (microphone).

3. Measuring the Time
  • The sensor calculates the time it took for the sound wave to return.

  • Using the speed of sound (343 m/s), the Arduino calculates how far away the object is!

Formula Used:


Distance=(Time Taken×0.034)÷2

Why divide by 2? Because the sound wave travels to the object and back!

Real-Life Use Cases of Ultrasonic Sensors!

  • Car Parking Sensors:

    • Ever heard a car beep faster as you get close to a wall? That’s an ultrasonic sensor!

  • Robot Navigation:

    • Robots use ultrasonic sensors to avoid obstacles so they don’t crash!

  • Automatic Doors:

    • Supermarkets use ultrasonic sensors to detect when someone is nearby and open the door!

  • Game Controllers:

    • Some VR and motion-based games use ultrasonic sensors to track hand movements!

  • Blind Assist Devices:

    • Some smart walking sticks use ultrasonic sensors to help visually impaired people detect obstacles!

How to connect Ultrasonic Sensor on the Blue Elixer Board

Code Breakdown: Ultrasonic Sensor + SMD LEDs

Now, let's use an ultrasonic sensor with SMD LEDs to create a light-up distance tracker!

1. Defining Sensor and LED Pins
#define trigPin 10
#define echoPin 12
#define ledPin1 4
#define ledPin2 5
#define ledPin3 6
#define ledPin4 7
#define ledPin5 8

We define the pins where the ultrasonic sensor and LEDs are connected.

2. Setting Up the Pins in setup()
void setup() {
  Serial.begin(9600);  // Start serial communication

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
}

What’s happening?

  • The trigger pin sends ultrasonic waves (OUTPUT).

  • The echo pin listens for the returning wave (INPUT).

  • The LEDs are set to OUTPUT so we can control them.

3. Measuring Distance with the Ultrasonic Sensor
long duration, distance;

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;

How It Works?

  • A small pulse (10µs) is sent from the trigger pin.

  • The echo pin listens for the reflected sound wave.

  • The Arduino calculates the distance using the speed of sound.

4. Lighting Up LEDs Based on Distance
if (distance < 10) {
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin4, HIGH);
  digitalWrite(ledPin5, HIGH);
}
else if (distance >= 10 && distance < 40) {
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);
}
else {
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);
}

What’s happening?

  • If an object is very close (<10cm)ALL LEDs light up!

  • If it’s a bit far (10-40cm)3 LEDs light up.

  • If it’s far away (>40cm)Only 1 LED stays on.

This is how parking sensors work in cars!

What You’ll See!

Video of Happening

  • Move your hand or an object in front of the ultrasonic sensor.

  • The SMD LEDs will light up based on how close you are!

Fun Activity!

In this activity, we’ll create a magic gate that opens and closes automatically based on how close an object (or your hand) is to an ultrasonic sensor!

How does it work?

  • If nothing is nearby (distance > 40cm) → The servo stays at 0° (gate is closed ).

  • If something is very close (distance < 10cm) → The servo moves to 180° (gate opens! ).

It’s just like an automatic door at the supermarket!

The Code: Ultrasonic Sensor + Servo Motor!

#include <Servo.h>

#define trigPin 10
#define echoPin 12
#define servoPin 9

Servo myServo;  // Create a Servo object

void setup() {
  Serial.begin(9600);  // Start serial communication

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  myServo.attach(servoPin);  // Attach the servo to pin 9
  myServo.write(0);  // Start with the servo at 0° (closed position)
}

void loop() {
  long duration, distance;

  // Send ultrasonic pulse
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the time taken for the pulse to return
  duration = pulseIn(echoPin, HIGH);

  // Convert time to distance (in cm)
  distance = duration * 0.034 / 2;

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // Control the servo based on distance
  if (distance < 10) {
    myServo.write(180);  // Open the gate (move servo to 180°)
  }
  else if (distance > 40) {
    myServo.write(0);  // Close the gate (move servo to 0°)
  }

  delay(500);  // Wait before the next measurement
}

Code Breakdown (How It Works!)

Pin Setup
#define trigPin 10
#define echoPin 12
#define servoPin 9
  • We define the ultrasonic sensor’s pins (trigPin and echoPin).

  • We define the servo motor’s pin (9) to control it easily.

Setting Up the Components in setup()
void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  myServo.attach(servoPin);
  myServo.write(0);  // Keep the servo at 0° (closed) initially
}
  • The ultrasonic sensor and servo motor are initialized.

  • The servo starts at 0° (closed position).

Measuring Distance with the Ultrasonic Sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;


The trigger pin sends a 10-microsecond ultrasonic pulse.
The echo pin listens for the returning wave.
The Arduino calculates the distance using the speed of sound!

Moving the Servo Based on Distance
if (distance < 10) {
  myServo.write(180);  // Open the gate (move servo to 180°)
}
else if (distance > 40) {
  myServo.write(0);  // Close the gate (move servo to 0°)
}
  • If something is very close (<10cm) → The servo moves to 180° (gate opens ).

  • If nothing is nearby (>40cm) → The servo moves to 0° (gate closes ).

Socials

info.jumplabs@gmail.com

Call Us

+4550142069

+91 9910566229

Damgade 82, 6400 Sønderborg

© Jumplabs Inc. 2023

Reach out to Us

Socials

info.jumplabs@gmail.com

Call Us

+4550142069

+91 9910566229

Damgade 82, 6400 Sønderborg

© Jumplabs Inc. 2023

Reach out to Us

Socials

info.jumplabs@gmail.com

Call Us

+4550142069

+91 9910566229

Damgade 82, 6400 Sønderborg

© Jumplabs Inc. 2023

Reach out to Us