Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO Controlled Ultrasonic Distance Measurement with Traffic Light Indicator

View and Edit Project In Cirkit DesignerImage of Arduino UNO Controlled Ultrasonic Distance Measurement with Traffic Light Indicator

Circuit Documentation

Summary

The circuit in question is designed to measure distance using an HC-SR04 Ultrasonic Sensor and display the measurement status through a set of three LEDs (red, yellow, and green). The Arduino UNO microcontroller is used to control the sensor and LEDs, as well as to manage the power-saving mode of the system. The circuit includes resistors to limit the current through the LEDs, ensuring their safe operation.

Component List

LEDs

Resistors

Microcontroller

Sensor

Wiring Details

Red LED

Yellow LED

Green LED

HC-SR04 Ultrasonic Sensor

Documented Code

#include <LowPower.h>

const int TRIG_PIN = 13;
const int ECHO_PIN = 12;

#define depthOfTote 100

long duration;
float distance, distanceTot, distanceAvg, speedOfSound;
int count;

int red_led = 5;
int yellow_led = 6;
int green_led  = 9;

void setup() {

  Serial.begin(9600);

  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  pinMode(red_led, OUTPUT);
  pinMode(yellow_led,  OUTPUT);
  pinMode(green_led, OUTPUT);

}

float readDistance() {

  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  duration = pulseIn(ECHO_PIN, HIGH);
  return duration;
}

void measurementCycle(){
  count = count + 1;

  if  (count < 5){
    //int temperature = dht11.readTemperature();
    speedOfSound = 343; //+ (0.6 * temperature);

    duration = readDistance();
    distance = duration * (speedOfSound/10000) / 2;
    delay(500);

    Serial.print("Distance: ");
    Serial.println(distance);
    Serial.println(count);
    distanceTot = distanceTot + distance;

  } else if (count == 6) {

    count = 0;
    distanceAvg = distanceTot / 5;
    
    Serial.print("Distance Average: ");
    Serial.println(distanceAvg);

    distanceTot = 0;
    if (distanceAvg > 68){
      digitalWrite(green_led, LOW);
      digitalWrite(yellow_led, LOW);
      digitalWrite(red_led, HIGH);

      LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
      LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
      

    } else if (distanceAvg > 40 && distanceAvg < 68){
      digitalWrite(green_led, LOW);
      digitalWrite(yellow_led, HIGH);
      digitalWrite(red_led, LOW);
      
      LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
      LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
      

    } else if (distanceAvg < 40){
      digitalWrite(green_led, HIGH);
      digitalWrite(yellow_led, LOW);
      digitalWrite(red_led, LOW);

      LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
      LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);

  }
}

}

void loop() {
  measurementCycle();
  delay(100);

}

The code provided is for the Arduino UNO microcontroller. It initializes the pins connected to the HC-SR04 sensor and LEDs, reads the distance measurements, and then lights up the corresponding LED based on the average distance measured. It also includes a power-down feature to save energy between measurements.