Name: Aditya Branch: Ee ROLL NO.: 2004201

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

NAME: ADITYA

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 the trigger and echo pins of the


ultrasonic sensor

#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;

//Create an object of LiquidCrystal class to


communicate with the LCD

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {

//Set the pinMode for all the necessary


pins

pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);

pinMode(enA, OUTPUT);

pinMode(enB, OUTPUT);

pinMode(in1, OUTPUT);

pinMode(in2, OUTPUT);

pinMode(in3, OUTPUT);

pinMode(in4, OUTPUT);

//Turning off all the motors at the start

digitalWrite(in1, LOW);

digitalWrite(in2, LOW);

digitalWrite(in3, LOW);

digitalWrite(in4, LOW);

//Start the serial communication at 9600


baud rate

Serial.begin(9600);
//Initialize the LCD display

lcd.begin(16, 2);

lcd.print("Distance: ");

void loop() {

//Trigger pin to low before operation to


avoid noise

long highPulseDuration;

int calculatedDistanceCm;

digitalWrite(triggerPin, LOW);

delayMicroseconds(5);

//Turning the trigger on for 10 microsec

digitalWrite(triggerPin, HIGH);

delayMicroseconds(10);
digitalWrite(triggerPin, LOW);

//Measuring the time of high pulse to


calculate distance

highPulseDuration = pulseIn(echoPin, HIGH);

calculatedDistanceCm = highPulseDuration *
0.034 / 2;

//Print the calculated distance to serial


monitor and LCD display

Serial.print("Calculated Distance: ");

Serial.print(calculatedDistanceCm);

Serial.println(" cm");

lcd.setCursor(10, 0);

lcd.print(calculatedDistanceCm);

lcd.print(" cm ");

//Control the direction of DC motors based


on the calculated distance

if (calculatedDistanceCm <= 20 &&


calculatedDistanceCm >= 10) {

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);

} else if (calculatedDistanceCm >= 20) {

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);

lcd.print("Rotating ACW ");

delay(1000);

} else {

analogWrite(enA, 255);

analogWrite(enB, 255);

digitalWrite(in1, LOW);

digitalWrite(in2, LOW);

digitalWrite(in3, LOW);

digitalWrite(in4, LOW);

Serial.println("Dengey nen tiragan");

lcd.setCursor(0, 1);

lcd.print("Stop ");
delay(1000);

Results(Images):

Observations:

1. When the object is less than 10cm away from the sensor:

● The motor stops rotating.


● The distance measurement on the sensor reads less than 10cm.
● The program logic ensures that the motor remains at a standstill when the object is
close.
● The LCD display shows the message "Stop".
2. When the object is between 10cm and 20cm away from the sensor:

● The motor rotates in a clockwise direction.


● The distance measurement on the sensor reads between 10cm and 20cm.
● The program logic causes the motor to rotate clockwise when the object is between the
specified distances.
● The LCD display shows the message "Rotating CW".

3. When the object is more than 20cm away from the sensor:

● The motor rotates in an anti-clockwise direction.


● The distance measurement on the sensor reads more than 20cm.
● The program logic causes the motor to rotate anti-clockwise when the object is more
than 20cm away.
● The LCD display shows the message "Rotating ACW".

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.

You might also like