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.
Arduino UNO
HC-SR04 Ultrasonic Sensor
Micro Servo 9G
16x2 I2C LCD
RGB LED Module
Resistor
5V connected to:
GND connected to:
A4 connected to:
A5 connected to:
D9 connected to:
D8 connected to:
D7 connected to:
D6 connected to:
D5 connected to:
D3 connected to:
VCC connected to:
GND connected to:
TRIG connected to:
ECHO connected to:
+5V connected to:
GND connected to:
PWM connected to:
VCC connected to:
GND connected to:
SDA connected to:
SCL connected to:
+VCC Red connected to:
+VCC Green connected to:
+VCC Blue connected to:
GND connected to:
pin1 connected to:
pin2 connected to:
#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.