Week 10: Ultrasonic Sensors and LEDs

Concept

For this weeks assignment I wanted to create a parking space detector system that assists drivers in finding available parking spots quickly and efficiently. It utilizes an ultrasonic sensor to detect the presence of vehicles in front of designated parking spaces. Through a series of LEDs, the system provides visual feedback to indicate the availability of parking spots to approaching drivers.

Technical Details:

I have used the ultrasonic sensor, after looking into it – this sensor emits ultrasonic waves and measures the time it takes for the waves to bounce back after hitting an object. Which means that it can be used to determines the distance of the object from its position.

LED Indicators

Connected the anode (longer leg) of each LED to digital pins 8, 9, and 10 on the microcontroller respectively.

Connected the cathode (shorter leg) of each LED to ground through a resistor ( 220Ω to 330Ω) to limit current and prevent damage.

Green LED: Indicates a vacant parking space. When the distance measured by the ultrasonic sensor exceeds a certain threshold (indicating no object is present), the green LED lights up, signaling to approaching drivers that the parking spot is available for use.

  • Red LED: Represents a partially occupied parking space. If the distance measured falls within a predefined range, suggesting the presence of a vehicle but with some space remaining, the red LED illuminates. This warns drivers that the space is partially occupied and may not be suitable for parking larger vehicles.
  • Blue LED: Signals a fully occupied or obstructed parking space. When the measured distance is very close to the sensor, indicating a fully occupied space or an obstruction such as a wall or pillar, the blue LED turns on. This prompts drivers to avoid attempting to park in the space to prevent potential collisions or damage to vehicles.
  • Ultrasonic Sensor:
    • Trig Pin: Connected  to digital pin 2 on the microcontroller.
    • Echo Pin: Connected to digital pin 3 on the microcontroller.
    • Vcc: Connected to 5V.
    • GND: Connected to ground.
  • Button:
    • One side connects to digital pin 13 on the microcontroller.
    • The other side connects to ground.
Code
// Define LED pins
int ledPin[3] = {8, 9, 10};

// Define Ultrasonic sensor pins
const int trigPin = 2; // or any other unused digital pin
const int echoPin = 3; // or any other unused digital pin

const int buttonPin = 13;
int buttonState = HIGH;
int lastButtonState = HIGH;
long lastDebounceTime = 0;
long debounceDelay = 50;
int pushCounter = 0;
int numberOfLED = 3;

void setup() {
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH); // Activate internal pull-up resistor
  
  // Set up LED pins
  for (int i = 0; i < numberOfLED; i++) {
    pinMode(ledPin[i], OUTPUT);
  }
  
  // Set up Ultrasonic sensor pins
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT);  // Sets the echoPin as an INPUT
}

void loop() {
  int reading = digitalRead(buttonPin);

  // Check if the button state has changed
  if (reading != lastButtonState) {
    // Reset the debounce timer
    lastDebounceTime = millis();
  }

  // Check if the debounce delay has passed
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // If the button state has changed, update the button state
    if (reading != buttonState) {
      buttonState = reading;

      // If the button state is LOW (pressed), increment pushCounter
      if (buttonState == LOW) {
        pushCounter++;
      }
    }
  }

  // Update the last button state
  lastButtonState = reading;

  // Turn off all LEDs
  for (int i = 0; i < numberOfLED; i++) {
    digitalWrite(ledPin[i], LOW);
  }

  // Perform Ultrasonic sensor reading
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration * 0.0343) / 2; // Calculate distance in cm

  // Perform actions based on distance measured
  if (distance < 10) {
    // Turn on first LED
    digitalWrite(ledPin[0], HIGH);
  } else if (distance < 20) {
    // Turn on second LED
    digitalWrite(ledPin[1], HIGH);
  } else if (distance < 30) {
    // Turn on third LED
    digitalWrite(ledPin[2], HIGH);
  }

  // Delay before next iteration
  delay(100); // Adjust as needed
}
Circuit

Video of the Final Circuit

Reference

I watched this video to get familiar to the ultrasonic sensor.

Leave a Reply