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

Arduino-Controlled Distance Monitoring System with Multi-Color LED Feedback

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled Distance Monitoring System with Multi-Color LED Feedback

Circuit Documentation

Summary

This circuit is designed to interface with multiple sensors and output devices, including ultrasonic sensors, LEDs, servos, a GSM module, and a GPS module. The primary microcontroller used in this circuit is an Arduino UNO, which controls LEDs based on the distance measurements from the ultrasonic sensors and communicates with an ESP32 microcontroller. The ESP32 handles more complex tasks such as driving servos, interfacing with the GSM module for cellular communication, and receiving GPS data. The circuit is powered by the 5V output from the Arduino UNO, which is distributed to the various components that require power.

Component List

LEDs

Resistors

Microcontrollers

Sensors

Communication Modules

Actuators

Wiring Details

LEDs

Resistors

Microcontrollers

Sensors

Communication Modules

Actuators

Documented Code

// Define pins for Ultrasonic Sensor 1
const int trigPin1 = A4;
const int echoPin1 = A5;

// Define pins for Ultrasonic Sensor 2
const int trigPin2 = A2;
const int echoPin2 = A3;

// Define pins for Ultrasonic Sensor 3
const int trigPin3 = A0;
const int echoPin3 = A1;

// Define pins for LEDs (3 for each sensor)
const int greenLED1 = 2;
const int yellowLED1 = 3;
const int redLED1 = 4;

const int greenLED2 = 5;
const int yellowLED2 = 6;
const int redLED2 = 7;

const int greenLED3 = 8;
const int yellowLED3 = 9;
const int redLED3 = 10;

// Variables for sensor timing and distance
long duration1, duration2, duration3;
int distance1, distance2, distance3;

void setup() {
  // Initialize the pins for ultrasonic sensors
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);

  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);

  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);

  // Initialize the pins for all LEDs
  pinMode(greenLED1, OUTPUT);
  pinMode(yellowLED1, OUTPUT);
  pinMode(redLED1, OUTPUT);

  pinMode(greenLED2, OUTPUT);
  pinMode(yellowLED2, OUTPUT);
  pinMode(redLED2, OUTPUT);

  pinMode(greenLED3, OUTPUT);
  pinMode(yellowLED3, OUTPUT);
  pinMode(redLED3, OUTPUT);
  
  // Begin serial communication for debugging
  Serial.begin(9600);
}

void loop() {
  // Get distance from Ultrasonic Sensor 1
  distance1 = getDistance(trigPin1, echoPin1);
  Serial.print("Distance 1: ");
  Serial.print(distance1);
  Serial.println(" cm");

  // Set LED states for Sensor 1
  setLEDs(distance1, greenLED1, yellowLED1, redLED1);

  // Get distance from Ultrasonic Sensor 2
  distance2 = getDistance(trigPin2, echoPin2);
  Serial.print("Distance 2: ");
  Serial.print(distance2);
  Serial.println(" cm");

  // Set LED states for Sensor 2
  setLEDs(distance2, greenLED2, yellowLED2, redLED2);

  // Get distance from Ultrasonic Sensor 3
  distance3 = getDistance(trigPin3, echoPin3);
  Serial.print("Distance 3: ");
  Serial.print(distance3);
  Serial.println(" cm");

  // Set LED states for Sensor 3
  setLEDs(distance3, greenLED3, yellowLED3, redLED3);

  // Small delay to avoid bouncing
  delay(500);
}

// Function to calculate distance for each ultrasonic sensor
int getDistance(int trigPin, int echoPin) {
  // Send out the ultrasonic signal
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Read the echo pin to get the signal's duration
  long duration = pulseIn(echoPin, HIGH);
  
  // Calculate the distance in centimeters
  int distance = duration * 0.034 / 2;
  
  return distance;
}

// Function to control LEDs based on distance
void setLEDs(int distance, int greenLED, int yellowLED, int redLED) {
  if (distance > 30) {
    // Green LED for distance greater than 30 cm
    digitalWrite(greenLED, HIGH);
    digitalWrite(yellowLED, LOW);
    digitalWrite(redLED, LOW);
  } else if (distance <= 30 && distance > 10) {
    // Yellow LED for distance between 10 cm and 30 cm
    digitalWrite(greenLED, LOW);
    digitalWrite(yellowLED, HIGH);
    digitalWrite(redLED, LOW);
  } else if (distance <= 10) {
    // Red LED for distance less than 10 cm
    digitalWrite(greenLED, LOW);
    digitalWrite(yellowLED, LOW);
    digitalWrite(redLED, HIGH);
  }
}

This code is designed to run on the Arduino UNO microcontroller. It initializes the pins for the ultrasonic sensors and LEDs, reads distances from the sensors, and sets the LED states based on these distances. The getDistance function sends an ultrasonic pulse and calculates the distance based on the time taken for the echo to return. The setLEDs function controls the state of the LEDs based on the measured distance.