[Arduino] Ultrasonic Distance Sensor



Hi,

this is my second experiment with Arduino, and this time i wanted to do something with ultrasonic distance sensor that a colleague gave me.
Ultrasonic distance works in this way: there is an ultrasonic transmitter that pulse ultrasonic and there is an ultrasonic reciver that listens ultrasonic come back
calculating time that ultrasonic go forward and come back is possible to how far an object is placed.

Doing this with Arduino is very simple. What we need is:

- Ultrasonic Distance sensor
- 4 wire cables
- nothing else....


Ok, this is the connection schema:

Arduino GND ------> Sensor GND pin
Arduino 5v  ------> Sensor positive pin
Arduino pin 12 ---> Sensor Echo pin
Arduino pin 13 ---> Sensor Trigger pin

that's all!

a pictures of schema:



this is the code:

////////////////////////////////////////////////
int emitterPin = 13;
int receiverPin = 12;

long duration, distance_inch, distance_cm;


void setup() {
    Serial.begin(9600);


    pinMode(emitterPin, OUTPUT);
    pinMode(receiverPin, INPUT);
}

void loop() {
 
    digitalWrite(emitterPin, HIGH);   
    delayMicroseconds(10);           
    digitalWrite(emitterPin, LOW);   


    duration = pulseIn(receiverPin, HIGH) / 2;

    distance_inch = duration / 74;
    distance_cm = duration / 29;


    Serial.print(distance_inch);
    Serial.print(" inch; ");
    Serial.print(distance_cm);
    Serial.print(" cm");
    Serial.println("");

    delay(1000);

}

////////////////////////////////////////////////



and this is final result:


pretty easy :D

Commenti