Arduino nano-based distance sensor | Projects

Materials needed:

  • Arduino Nano
  • HC-SR04 ultrasonic sensor
  • Breadboard
  • Jumper wires
  • USB cable
  • Computer with Arduino IDE installed

Steps:

  1. Connect the HC-SR04 ultrasonic sensor to the breadboard.
  2. Connect the VCC pin of the HC-SR04 to the 5V pin on the Arduino Nano.
  3. Connect the GND pin of the HC-SR04 to the GND pin on the Arduino Nano.
  4. Connect the Trig pin of the HC-SR04 to the D10 pin on the Arduino Nano.
  5. Connect the Echo pin of the HC-SR04 to the D11 pin on the Arduino Nano.
  6. Connect the Arduino Nano to your computer using the USB cable.
  7. Open the Arduino IDE on your computer.
  8. Select the correct board and port in the Tools menu.
  9. Create a new sketch by clicking on File -> New.
  10. Copy and paste the following code into the new sketch:

Code

#define trigPin 10
#define echoPin 11

void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
Serial.print("Distance: ");
Serial.println(distance);
delay(500);
}
  1. Verify and upload the code to the Arduino Nano by clicking on the Verify and Upload buttons in the Arduino IDE.
  2. Open the Serial Monitor by clicking on Tools -> Serial Monitor.
  3. The distance measurement in centimeters should now be displayed in the Serial Monitor.
  4. Test the sensor by placing an object in front of it and observe the distance measurement on the Serial Monitor.

Related Posts

2 thoughts on “Arduino nano-based distance sensor | Projects

  1. You can use this to make it beep if there is an obstacle around 1 meter in front of the sensor

    Materials needed:

    Arduino Nano
    HC-SR04 ultrasonic sensor
    Buzzer
    Breadboard
    Jumper wires
    USB cable
    Computer with Arduino IDE installed
    Steps:

    Connect the HC-SR04 ultrasonic sensor and buzzer to the breadboard.
    Connect the VCC pin of the HC-SR04 to the 5V pin on the Arduino Nano.
    Connect the GND pin of the HC-SR04 to the GND pin on the Arduino Nano.
    Connect the Trig pin of the HC-SR04 to the D10 pin on the Arduino Nano.
    Connect the Echo pin of the HC-SR04 to the D11 pin on the Arduino Nano.
    Connect the positive pin of the buzzer to the D9 pin on the Arduino Nano.
    Connect the negative pin of the buzzer to the GND pin on the Arduino Nano.
    Connect the Arduino Nano to your computer using the USB cable.
    Open the Arduino IDE on your computer.
    Select the correct board and port in the Tools menu.
    Create a new sketch by clicking on File -> New.
    Copy and paste the following code into the new sketch:

    #define trigPin 10
    #define echoPin 11
    #define buzzerPin 9

    void setup() {
    Serial.begin(9600);
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    pinMode(buzzerPin, OUTPUT);
    }

    void loop() {
    long duration, distance;
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = (duration / 2) / 29.1;
    Serial.print(“Distance: “);
    Serial.println(distance);

    Part 1 of 2

    1. if (distance < 100) {
      digitalWrite(buzzerPin, HIGH);
      delay(1000);
      digitalWrite(buzzerPin, LOW);
      delay(1000);
      }
      delay(500);
      }

      Verify and upload the code to the Arduino Nano by clicking on the Verify and Upload buttons in the Arduino IDE.
      Open the Serial Monitor by clicking on Tools -> Serial Monitor.
      The distance measurement in centimeters should now be displayed in the Serial Monitor.
      Test the sensor by placing an object in front of it and observe the distance measurement on the Serial Monitor. If the distance is less than 1 meter, the buzzer should beep for 1 second, then wait for another second before taking another distance measurement.

Leave a Reply

Your email address will not be published. Required fields are marked *