Arduino Nano Distance Sensor

by Thala_07 in Circuits > Arduino

20 Views, 0 Favorites, 0 Comments

Arduino Nano Distance Sensor

A000073_00.front_1000x750__45992.jpg

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



Materials

First gather materials listed above

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.

Downloads

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




}

My Result

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