Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino UNO-Based Smart Distance Sensing and Display System with RGB LED and Servo Control

Image of Arduino UNO-Based Smart Distance Sensing and Display System with RGB LED and Servo Control

Circuit Documentation

Summary

This circuit involves an Arduino UNO microcontroller interfacing with several components: an HC-SR04 Ultrasonic Sensor, a Micro Servo 9G, a 16x2 I2C LCD, and an RGB LED module. The circuit is designed to measure distance using the ultrasonic sensor, display messages on the LCD, control a servo motor based on the measured distance, and change the color of the RGB LED module accordingly.

Component List

  1. Arduino UNO

    • Description: A microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. HC-SR04 Ultrasonic Sensor

    • Description: A sensor used to measure distance by using ultrasonic waves.
    • Pins: VCC, TRIG, ECHO, GND
  3. Micro Servo 9G

    • Description: A small servo motor used for precise control of angular position.
    • Pins: GND, +5V, PWM
  4. 16x2 I2C LCD

    • Description: A 16x2 character LCD display with I2C interface.
    • Pins: GND, VCC, SDA, SCL
  5. RGB LED Module

    • Description: An LED module with red, green, and blue LEDs.
    • Pins: +VCC Red, +VCC Green, +VCC Blue, GND
  6. Resistor

    • Description: A 220 Ohm resistor used to limit current.
    • Pins: pin1, pin2
    • Properties: Resistance: 220 Ohms

Wiring Details

Arduino UNO

  • 5V connected to:

    • Micro Servo 9G (+5V)
    • 16x2 I2C LCD (VCC)
    • HC-SR04 Ultrasonic Sensor (VCC)
  • GND connected to:

    • Resistor (pin1)
    • Micro Servo 9G (GND)
    • 16x2 I2C LCD (GND)
    • HC-SR04 Ultrasonic Sensor (GND)
  • A4 connected to:

    • 16x2 I2C LCD (SDA)
  • A5 connected to:

    • 16x2 I2C LCD (SCL)
  • D9 connected to:

    • HC-SR04 Ultrasonic Sensor (TRIG)
  • D8 connected to:

    • HC-SR04 Ultrasonic Sensor (ECHO)
  • D7 connected to:

    • Micro Servo 9G (PWM)
  • D6 connected to:

    • RGB LED Module (+VCC Blue)
  • D5 connected to:

    • RGB LED Module (+VCC Green)
  • D3 connected to:

    • RGB LED Module (+VCC Red)

HC-SR04 Ultrasonic Sensor

  • VCC connected to:

    • Arduino UNO (5V)
  • GND connected to:

    • Arduino UNO (GND)
  • TRIG connected to:

    • Arduino UNO (D9)
  • ECHO connected to:

    • Arduino UNO (D8)

Micro Servo 9G

  • +5V connected to:

    • Arduino UNO (5V)
  • GND connected to:

    • Arduino UNO (GND)
  • PWM connected to:

    • Arduino UNO (D7)

16x2 I2C LCD

  • VCC connected to:

    • Arduino UNO (5V)
  • GND connected to:

    • Arduino UNO (GND)
  • SDA connected to:

    • Arduino UNO (A4)
  • SCL connected to:

    • Arduino UNO (A5)

RGB LED Module

  • +VCC Red connected to:

    • Arduino UNO (D3)
  • +VCC Green connected to:

    • Arduino UNO (D5)
  • +VCC Blue connected to:

    • Arduino UNO (D6)
  • GND connected to:

    • Resistor (pin2)

Resistor

  • pin1 connected to:

    • Arduino UNO (GND)
  • pin2 connected to:

    • RGB LED Module (GND)

Code Documentation

#include <Servo.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4); 

const int trigPin = 9;
const int echoPin = 8;

Servo myServo;
const int servoPin = 7;

const int distanceThreshold = 20;
bool isDriving = false;

const int redPin = 3;   
const int greenPin = 5;
const int bluePin = 6;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

  myServo.attach(servoPin);
  myServo.write(0); 

  lcd.init();                    
  lcd.backlight();
  lcd.setCursor(4,0);
  lcd.print("Welcome!");

  Serial.begin(9600);
}

void loop() {
  long duration = measureDistance();
  int distance = duration * 0.034 / 2;

  Serial.print("Расстояние: ");
  Serial.print(distance);
  Serial.println(" см");

  if (distance > 0 && distance <= distanceThreshold) {
    myServo.write(90); 
    isDriving = true;
  } else {
    myServo.write(0); 
    isDriving = false;
  }

  delay(500); 

  lcd.setCursor(0, 1);
  lcd.print("                ");
  lcd.setCursor(0, 1);
  if (isDriving) {
    lcd.print("Drive");
    setColor(255, 0, 255); 
    delay(3000); 
  } else {
    lcd.print("Wait");
    setColor(0, 255, 255); 
    delay(3000);
  }

  delay(500); 
}

long measureDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  return pulseIn(echoPin, HIGH);
}

void setColor(int red, int green, int blue) {
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}

This code initializes the components, measures distance using the HC-SR04 sensor, controls the servo motor based on the measured distance, displays messages on the LCD, and changes the color of the RGB LED module.