Project Overview:

This project uses an ultrasonic distance sensor to measure the distance to an object and controls a series of LEDs based on the distance. The closer the object, the more LEDs light up, providing a visual indication of proximity.

How It Works:

An ultrasonic sensor sends out sound waves and measures the time it takes for the echo to return. The time is then converted to a distance in centimeters. Based on the measured distance, LEDs are lit up to indicate how close the object is to the sensor.

Components List:

  1. Arduino Board (e.g., Uno, Nano, etc.)

  2. Ultrasonic Sensor (e.g., HC-SR04)

  3. 3 LEDs (Optional: Different colors like green, yellow, and red)

  4. 3 220-ohm Resistors

  5. Jumper Wires

  6. Breadboard (optional)

  7. Power Supply (for Arduino)


Software List:

  • Arduino IDE (Integrated Development Environment)

Circuit Diagram and Connection:

Component

Arduino Pin

Trigger Pin (Ultrasonic Sensor)

Digital Pin 7

Echo Pin (Ultrasonic Sensor)

Digital Pin 6

LED 1 (Closest Distance)

Digital Pin 2

LED 2

Digital Pin 3

LED 3 (Farthest Distance)

Digital Pin 4

Resistors (220-ohm)

Between each LED and GND

  1. Connect the trigger pin of the ultrasonic sensor to Digital Pin 7.

  2. Connect the echo pin of the ultrasonic sensor to Digital Pin 6.

  3. Connect the anode (long leg) of the first LED to Digital Pin 2, the second LED to Digital Pin 3, and the third LED to Digital Pin 4 on the Arduino.

  4. Connect the cathode (short leg) of each LED to GND through a 220-ohm resistor.

Project Code:

int distanceThreshold = 350;

int cm;

long readUltrasonicDistance(int triggerPin, int echoPin){

  pinMode(triggerPin, OUTPUT);

  digitalWrite(triggerPin, LOW);

  delayMicroseconds(2);

  digitalWrite(triggerPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(triggerPin, LOW);

  pinMode(echoPin, INPUT);

  return pulseIn(echoPin, HIGH);

}

void setup(){

  pinMode(2, OUTPUT);

  pinMode(3, OUTPUT);

  pinMode(4, OUTPUT);

}

void loop(){

  cm = 0.01723 * readUltrasonicDistance(7, 6);

  

  if (cm < distanceThreshold){

    digitalWrite(2, LOW);

    digitalWrite(3, LOW);

    digitalWrite(4, LOW);

  }

  if (cm <= distanceThreshold && cm > distanceThreshold - 100){

    digitalWrite(2, HIGH);

    digitalWrite(3, LOW);

    digitalWrite(4, LOW);

  }

  if (cm <= distanceThreshold - 100 && cm > distanceThreshold - 250){

    digitalWrite(2, HIGH);

    digitalWrite(3, HIGH);

    digitalWrite(4, LOW);

  }

  if (cm <= distanceThreshold - 250 && cm > distanceThreshold - 350){

    digitalWrite(2, HIGH);

    digitalWrite(3, HIGH);

    digitalWrite(4, HIGH);

  }

  if (cm <= distanceThreshold - 350){

    digitalWrite(2, HIGH);

    digitalWrite(3, HIGH);

    digitalWrite(4, HIGH);

  }

  

  delay(100);

}

Explanation of the Code:

  • Distance Threshold:

    • int distanceThreshold = 350; sets the distance threshold in centimeters. This is the maximum distance at which the LEDs will begin to light up.

  • Ultrasonic Sensor Function:

    • readUltrasonicDistance() handles the reading from the ultrasonic sensor by sending a trigger pulse and measuring the duration of the echo pulse. This duration is then converted to a distance.

  • Setup Function:

    • pinMode(2, OUTPUT); through pinMode(4, OUTPUT); set the pins connected to the LEDs as outputs.

  • Loop Function:

    • The loop() function continuously measures the distance using the ultrasonic sensor and determines which LEDs to turn on based on the distance.

    • If the object is closer than the threshold minus 350 cm, all LEDs are on.

    • As the distance increases, fewer LEDs are turned on, indicating that the object is farther away.

Test and Troubleshooting:

  1. Testing: Upload the code to your Arduino and place an object at different distances from the sensor to see how the LEDs respond.

  2. Troubleshooting:

    • Ensure the ultrasonic sensor is correctly wired, with the trigger and echo pins connected to the specified digital pins.

    • Adjust the distanceThreshold if the LEDs are not responding as expected.

Summary:

This project uses an ultrasonic sensor to measure the distance to an object and visually indicates the distance by lighting up LEDs. It's an effective way to learn about distance sensors and LED control with Arduino.


No comments

Theme images by Dizzo. Powered by Blogger.