Introduction: Arduino Nano Distance Sensor

I've made an easy arduino nano distance sensor

Supplies

  1. Arduino Nano board — Amazon
  2. Ultrasonic sensor — Amazon
  3. Breadboard — Amazon
  4. Jumper Wires — Amazon



Step 1: Materials

First gather materials listed above

Step 2: Assembly

Connect the ultrasonic sensor and the Arduino board to the breadboard. After connect the ultrasonic sensor to the Arduino board using the jumper wire. For that, see the circuit diagram below. Connect the TRIG pin of the ultrasonic sensor to the D2 pin and the ECHO pin to the D4 pin. The GND pin connects to the GND pin and the VCC pin connects to the VIN or 5v pin.

Step 3: Code

void setup() {

pinMode(2, OUTPUT);//define arduino pin

pinMode(4, INPUT);//define arduino pin

Serial.begin(9600);//enable serial monitor


}

void loop() {

//pulse output

digitalWrite(2, LOW);

delayMicroseconds(4);

digitalWrite(2, HIGH);

delayMicroseconds(10);

digitalWrite(2, LOW);


long t = pulseIn(4, HIGH);//input pulse and save it veriable

long inches = t / 74 / 2; //time convert distance

long cm = t / 29 / 2; //time convert distance

String inch = " inches \t";

String CM = " cm";


Serial.print(inches +inch);//print serial monitor inches

Serial.println(cm +CM);//print serial monitor cm

Serial.println();//print space

delay(100);//delay




}

Step 4: My Result

https://youtube.com/shorts/9LmdkbMFhrE?feature=share