Education

Education

Education

Ultrasonic Sensor

Robo Explorers

In the second task, we will set up a radar system to detect objects and protect the ship. We use the Ultrasonic sensor HC-SR04 for this application. 

Watch the detailed video of how an actual ultrasonic sensor works, what is the science behind it, and how we control it with arduino.


Watch the video below to understand how the ultrasonic sensor is connected to the arduino and furtehr activated on the Blue Elixir Board. Also learn how the ultrasonic sensor is working!

Here are the code snippets required for the ultrasonic sensor to measure the distance and work all-together.

#define trigPin 10 // WE SET THE TRIG PIN TO BE CONNECTED TO THE D10 OF ARDUINO
#define echoPin 12 // WE SET THE ECHO PIN TO BE CONNECTED TO THE D12 OF ARDUINO

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT); // THE TRIG PIN IS SET TO BE AN OUTPUT 
  pinMode(echoPin, INPUT); // THE ECHO PIN RECEIVES THE ULTRASONIC WAVES OUTPUTED FROM THE TRIG SIDE HENCE IT IS SET AS AN OUTPUT

}

void loop() {

  long duration, distance; // THE CALCULATED DISTANCE WILL BE SAVED IN THIS distance VARIABLE
  // Clear the trigger pin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  // Set the trigger pin high for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Measure the duration of the pulse from the echo pin
  duration = pulseIn(echoPin, HIGH);
  
  // Calculate distance in centimeters
  distance= duration * 0.034 / 2; // Speed of sound is 0.034 cm/µs
  
  // Print the distance to the serial monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

}

Learn from a Professional

Book a Class

Socials

info.jumplabs@gmail.com

Call Us

+4550142069

+919560372931

Damgade 82, 6400 Sønderborg

© Jumplabs Inc. 2023

Reach out to Us

Socials

info.jumplabs@gmail.com

Call Us

+4550142069

+919560372931

Damgade 82, 6400 Sønderborg

© Jumplabs Inc. 2023

Reach out to Us