Name: Aditya Branch: Ee ROLL NO.: 2004201
Name: Aditya Branch: Ee ROLL NO.: 2004201
Name: Aditya Branch: Ee ROLL NO.: 2004201
BRANCH: EE
ROLL NO.: 2004201
EE331 LAB QUESTION - 4
AIM:
To Measure the distance of an object in ‘cm’ using a US sensor and rotate the motor with Arduino
Uno as shown in the table below(Image Attached) depending on the distance of an object and display the
direction of the motor on the LCD display..
APPARATUS:
➢ Arduino UNO
➢ US sensor
➢ Motor
➢ Motor driver L298N
➢ LCD
CODE:
//Include the LiquidCrystal library to
interface with the LCD
#include <LiquidCrystal.h>
#define triggerPin 10
#define echoPin 11
//Define the pins for controlling the DC
motors
int enA = 9;
int enB = 3;
int in1 = 8;
int in2 = 7;
int in3 = 6;
int in4 = 5;
void setup() {
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
Serial.begin(9600);
//Initialize the LCD display
lcd.begin(16, 2);
lcd.print("Distance: ");
void loop() {
long highPulseDuration;
int calculatedDistanceCm;
digitalWrite(triggerPin, LOW);
delayMicroseconds(5);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
calculatedDistanceCm = highPulseDuration *
0.034 / 2;
Serial.print(calculatedDistanceCm);
Serial.println(" cm");
lcd.setCursor(10, 0);
lcd.print(calculatedDistanceCm);
lcd.print(" cm ");
analogWrite(enA, 255);
analogWrite(enB, 255);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
Serial.println("Rotating CW");
lcd.setCursor(0, 1);
lcd.print("Rotating CW ");
delay(1000);
analogWrite(enA, 255);
analogWrite(enB, 255);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
Serial.println("Rotating ACW");
lcd.setCursor(0, 1);
delay(1000);
} else {
analogWrite(enA, 255);
analogWrite(enB, 255);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
lcd.setCursor(0, 1);
lcd.print("Stop ");
delay(1000);
Results(Images):
Observations:
1. When the object is less than 10cm away from the sensor:
3. When the object is more than 20cm away from the sensor:
Conclusion:
The conclusion for measuring the distance of an object using a US sensor and rotating the
motor with Arduino Uno based on the conditions mentioned and displaying the direction of the
motor on the LCD display is that the system is capable of accurately detecting the distance of
an object and rotating the motor in different directions based on the distance. The program logic
and hardware components work together to provide a functioning system that can be used in
various applications where distance sensing and motor control are required. The LCD display
provides clear information on the current state of the system and the direction of the motor.
Overall, this system is an effective and efficient solution for controlling the rotation of a motor
based on the distance of an object.