ULTRASONIC SENSOR
We all know that ultrasonic vibrations are used in an ultrasonic sensor. They are transmitted from one side and received from the other. The sensor on its own has various capabilities. For instance, we can use the sensor to make a working radar(in fact that's how they work) or make ourselves a silent pet that follows us around. The capabilities are unlimited when it comes to an ultrasonic sensor. The only thing that matters is how you use the sensor. All in all, the ultrasonic sensor maps the distance between itself and its surroundings and gives us a value we can use to our benefit.
How it works?
One side of the ultrasonic sensor sends ultrasonics waves, while the other side receives them.This constant sending and receiving help us determine the distance between the object and the sensor.
The principle of the ultrasonic is simple, just like dolphins, whales, and bats who transmit and receive ultrasonic waves the sensor sends waves and receives them.
We can use the following formula to calculate distance in an ultrasonic sensor:
Distance= speed*time.
We will be using the pulsein function for this sensor which readily converts the duration to distance according to our needs. I will be talking about the HC-SR04 ultrasonic sensor.
THE CONNECTIONS
- Connect VCC to +5v of the Arduino.
- Connect GND to GND of the Arduino.
- Connect trigpin to number 2 pin of the Arduino.
- Connect echopin to number 3 pin of the Arduino.
THE CODE
const int trigPin = 2;
const int echoPin = 3;
double duration, distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration*.0343)/2;
Serial.print("Distance: ");
Serial.println(distance); delay(100);}
YOU CAN CALL ME OR SEND ME A MAIL TO GET YOUR PARTS FOR THE CHEAPEST PRICE... ANY TIME.
P.S.: If you have any doubt please comment or email me at robotherapyy@gmail.com
I am waiting for more articles
ReplyDelete