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

Ultrasonic Distance Measurement with Servo Control and LCD Display

View and Edit Project In Cirkit DesignerImage of Ultrasonic Distance Measurement with Servo Control and LCD Display

Circuit Documentation

Summary

This circuit integrates an Arduino Uno R3 microcontroller with various components including ultrasonic sensors, a servo motor, an LCD display, a relay, and a fan. The primary function of the circuit is to measure distance using the HC-SR04 ultrasonic sensor, display the measured distance on the LCD, and control the position of a servo motor based on the distance measured. Additionally, the circuit includes a relay to control a 220V fan based on certain conditions.


Component List

Arduino Uno R3

Servo

HC-SR04 Ultrasonic Sensor

220 Fan

Lcd 20x4 I2C

Power Transformer (220V to 24V)

12V Relay

Fuse

Power 220V


Wiring Details

Arduino Uno R3


Servo


HC-SR04 Ultrasonic Sensor


220 Fan


Lcd 20x4 I2C


Power Transformer (220V to 24V)


12V Relay


Fuse


Documented Code

Arduino Uno R3 Code

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

HC-SR04 Ultrasonic Sensor Code

/*
 * This Arduino Sketch controls an HC-SR04 Ultrasonic Sensor, a Servo motor,
 * and an LCD display. The sensor measures the distance to an object, the
 * distance is displayed on the LCD, and the Servo motor rotates based on
 * the measured distance.
 */

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

#define TRIG_PIN A3
#define ECHO_PIN 12
#define SERVO_PIN 6

LiquidCrystal_I2C lcd(0x27, 20, 4);
Servo myServo;

void setup() {
  lcd.begin();
  lcd.backlight();
  myServo.attach(SERVO_PIN);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
}

void loop() {
  long duration, distance;
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  duration = pulseIn(ECHO_PIN, HIGH);
  distance = (duration / 2) / 29.1;
  lcd.setCursor(0, 0);
  lcd.print("Distance: ");
  lcd.print(distance);
  lcd.print(" cm");
  int angle = map(distance, 0, 100, 0, 180);
  myServo.write(angle);
  delay(500);
}

This documentation provides a comprehensive overview of the circuit, detailing each component, its purpose, wiring connections, and the code used to control the system.