16
Prince Mohammad University Microprocessors Fall 2018/2019 Motor Speed and Direction Control with Ultrasonic Sensor Prepared by: Abdulaziz Alsaleh 201700157 Instructor: Dr. Ahmed A Hussain

Objectives - aalsaleh393.files.wordpress.com  · Web view• Control the speed of the motor using PWM and perform direction control of the DC motor using Hbridge IC L293D . Control

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Objectives - aalsaleh393.files.wordpress.com  · Web view• Control the speed of the motor using PWM and perform direction control of the DC motor using Hbridge IC L293D . Control

Prince Mohammad University

Microprocessors

Fall 2018/2019

Motor Speed and Direction Control with Ultrasonic Sensor

Prepared by: Abdulaziz Alsaleh 201700157

Instructor: Dr. Ahmed A Hussain

Page 2: Objectives - aalsaleh393.files.wordpress.com  · Web view• Control the speed of the motor using PWM and perform direction control of the DC motor using Hbridge IC L293D . Control

Objectives:

• Interface an Ultrasonic sensor to the Arduino board

• Control the speed of the motor using PWM and perform direction control of the DC motor

using Hbridge IC L293D

Control motor direction from an external switch

Control motor speed from a potentiometer

Introduction

An H bridge is an electronic circuit that enables a voltage to be applied across a load in either

direction. These circuits are often used in robotics and other applications to allow DC motors

to run forwards or backwards The term H bridge is derived from the typical graphical

representation of such a circuit. An H bridge is built with four switches (solid-state or

mechanical). When the switches S1 and S4 are closed (and S2 and S3 are open) a positive

voltage will be applied across the motor. By opening S1 and S4 switches and closing S2 and

S3 switches, this voltage is reversed, allowing reverse operation of the motor.

Ultrasonic Sensor: The HC-SR04/SR05 Ultrasonic Sensor is a very affordable

proximity/distance sensor that has been used mainly for obstacle avoidance in various

robotics projects. It essentially gives your Arduino eyes / special awareness and can prevent

your robot from crashing or falling off a table.

Page 3: Objectives - aalsaleh393.files.wordpress.com  · Web view• Control the speed of the motor using PWM and perform direction control of the DC motor using Hbridge IC L293D . Control

Solutions, Arduino code, and circuit diagrams:

Exercise 1: Motor Direction Control using H-Bridge IC

Steps:

1. Wire up the H-bridge IC L293D to the Arduino Uno according to the diagram shown

below:

Page 4: Objectives - aalsaleh393.files.wordpress.com  · Web view• Control the speed of the motor using PWM and perform direction control of the DC motor using Hbridge IC L293D . Control

2. Connect a potentiometer to the Arduino board with the left pin of the pot to 5 V, middle

pin to analog 0, and the right pinto ground on the Arduino board.

3. Connect an SPDT switch to a digital IO pin on the Arduino board.

4. Write a program to run the motor in forward/reverse direction based on the switch value,

and control the speed of the motor based on potentiometer input.

The code:

#define Trigpin 8

#define Echopin 7

#define motEn 10

#define motIN1 6

#define motIN2 5

#define Gled 4

#define Yled 3

#define Rled 2

int pwmVal=0;

void setup() {

pinMode(Trigpin,OUTPUT);

pinMode(Echopin,INPUT);

pinMode(motEn,OUTPUT);

pinMode(motIN1,OUTPUT);

pinMode(motIN2,OUTPUT);

Page 5: Objectives - aalsaleh393.files.wordpress.com  · Web view• Control the speed of the motor using PWM and perform direction control of the DC motor using Hbridge IC L293D . Control

pinMode(Gled,OUTPUT);

pinMode(Yled,OUTPUT);

pinMode(Rled,OUTPUT);

Serial.begin(9600);

}

void loop() {

long duration , cm ;

digitalWrite(Trigpin, LOW);

delayMicroseconds(2);

digitalWrite(Trigpin, HIGH);

delayMicroseconds(5);

digitalWrite(Trigpin, LOW);

duration = pulseIn(Echopin,HIGH);

cm = duration /48;

Serial.print("cm =");

Serial.println(cm);

digitalWrite(motIN1,HIGH);

digitalWrite(motIN2,LOW);

if (cm>100){

analogWrite(motEn,255);

digitalWrite(Yled,LOW);

Page 6: Objectives - aalsaleh393.files.wordpress.com  · Web view• Control the speed of the motor using PWM and perform direction control of the DC motor using Hbridge IC L293D . Control

digitalWrite(Rled,LOW);

digitalWrite(Gled,HIGH);

}

else if(cm<5){

digitalWrite(Yled,LOW);

digitalWrite(Rled,HIGH);

digitalWrite(Gled,LOW);

analogWrite(motEn,0);

delay(1000);

digitalWrite(motIN1,LOW);

digitalWrite(motIN2,HIGH);

analogWrite(motEn,100);

delay(3000);

}

else if ((cm>5)&&(cm<100))

{

digitalWrite(Yled,HIGH);

digitalWrite(Rled,LOW);

digitalWrite(Gled,LOW);

pwmVal = map(cm,6,100,20,200);

analogWrite(motEn,pwmVal);}}

Page 7: Objectives - aalsaleh393.files.wordpress.com  · Web view• Control the speed of the motor using PWM and perform direction control of the DC motor using Hbridge IC L293D . Control

Exercise 2 & 3: Ultrasonic sensor and DC Motor

Steps:

1. Connect the DC motor and H-bridge to the Arduino board as in Exercise 1. Connect a

green and red LED to the board. 2. Connect the ultrasonic sensor to the Arduino board

as in Exercise 2 (make sure there are no conflicts with IO pins). 3. Write a program to

control the motor operation and speed based on the distance of an object from the

ultrasonic sensor. – When an object gets too close (< 5 cm), slow down and stop the

motor, and then run it in reverse direction for 3 seconds. Turn ON the red LED. –

When the object is far (> 100 cm), run the motor at high speed. Turn ON the green

LED. 4. Upload the sketch to the board. 5. Show the working system to your

instructor for credit.

Page 8: Objectives - aalsaleh393.files.wordpress.com  · Web view• Control the speed of the motor using PWM and perform direction control of the DC motor using Hbridge IC L293D . Control

The code:

#define Trigpin 8

#define Echopin 7

#define motEn 10

#define motIN1 6

#define motIN2 5

#define Gled 4

#define Yled 3

#define Rled 2

int pwmVal=0;

void setup() {

pinMode(Trigpin,OUTPUT);

pinMode(Echopin,INPUT);

pinMode(motEn,OUTPUT);

pinMode(motIN1,OUTPUT);

pinMode(motIN2,OUTPUT);

pinMode(Gled,OUTPUT);

pinMode(Yled,OUTPUT);

pinMode(Rled,OUTPUT);

Serial.begin(9600);

}

Page 9: Objectives - aalsaleh393.files.wordpress.com  · Web view• Control the speed of the motor using PWM and perform direction control of the DC motor using Hbridge IC L293D . Control

void loop() {

long duration , cm ;

digitalWrite(Trigpin, LOW);

delayMicroseconds(2);

digitalWrite(Trigpin, HIGH);

delayMicroseconds(5);

digitalWrite(Trigpin, LOW);

duration = pulseIn(Echopin,HIGH);

cm = duration /48;

Serial.print("cm =");

Serial.println(cm);

digitalWrite(motIN1,HIGH);

digitalWrite(motIN2,LOW);

if (cm>100){

analogWrite(motEn,255);

digitalWrite(Yled,LOW);

digitalWrite(Rled,LOW);

digitalWrite(Gled,HIGH);

}

else if(cm<5){

digitalWrite(Yled,LOW);

Page 10: Objectives - aalsaleh393.files.wordpress.com  · Web view• Control the speed of the motor using PWM and perform direction control of the DC motor using Hbridge IC L293D . Control

digitalWrite(Rled,HIGH);

digitalWrite(Gled,LOW);

analogWrite(motEn,0);

delay(1000);

digitalWrite(motIN1,LOW);

digitalWrite(motIN2,HIGH);

analogWrite(motEn,100);

delay(3000);

}

else if ((cm>5)&&(cm<100))

{

digitalWrite(Yled,HIGH);

digitalWrite(Rled,LOW);

digitalWrite(Gled,LOW);

pwmVal = map(cm,6,100,20,200);

analogWrite(motEn,pwmVal);

}

}

Page 11: Objectives - aalsaleh393.files.wordpress.com  · Web view• Control the speed of the motor using PWM and perform direction control of the DC motor using Hbridge IC L293D . Control

Results and Analysis

Motor stops and reverse and red LED is turned ON:

Page 12: Objectives - aalsaleh393.files.wordpress.com  · Web view• Control the speed of the motor using PWM and perform direction control of the DC motor using Hbridge IC L293D . Control

Motor moves slowly and yellow LED is turned ON:

Motor go Maximum Speed and green LED is turned ON:

Page 13: Objectives - aalsaleh393.files.wordpress.com  · Web view• Control the speed of the motor using PWM and perform direction control of the DC motor using Hbridge IC L293D . Control

Conclusions

L293D is a motor driver IC which allows a DC motor to rotate in clockwise or anticlockwise

direction. L293D is a 16-pin IC which can control a set of two DC motors simultaneously in

any direction. • You can control two DC motors with a single L293D IC. • It works on the

concept of H-bridge. So in the help of am Hbrige and an ultrasonic sensor and the right

program for it to run we implemented a motor controlling its speed depending on the

ultrasonic sensors output for determining the distance of the facing objects