0% found this document useful (0 votes)
17 views18 pages

Motor Control Laboratory Activity 3

Uploaded by

Denver Sumbagan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views18 pages

Motor Control Laboratory Activity 3

Uploaded by

Denver Sumbagan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

CP E 12 - MICROPROCESSOR SYSTEMS

Department of Computer Engineering


Instructor: Engr. Daniel Gracias V. Esquejo

LABORATORY ACTIVITY NO. 3


MOTOR CONTROL

I. OBJECTIVES
At the end of the activity the student should be able to:
1. Understand how DC motors, servo motors and stepper motor works.
2. Understand how to control motor using H-Bridge (L293D) circuits.
3. Interface DC motors, servo motors using H-Bridge (L293D) and stepper motor
to an Arduino board.

II. EQUIPMENT AND MATERIALS


• 1 pc IC L293D
• 2 pcs Button Switch
• 1 pc 9 volts Battery
• 1 pc DC motor
• 1 pc Servo motor
• 1 pc DC Motor
• 1 pc Stepper Motor
• 1 pc 5V DC Stepper Motor (28BYJ-48)
• 1 pc LDR/Photoresistor
• 1 pc Arduino Uno
• 1 pc Solderless Breadboard
• 3 pcs 10K resistor
• 2 pcs LED (Red and Green)
• 1 pc 470uf Electrolytic Capacitor, 12V
• 2 pcs 220 Ohms Resistor

III. DISCUSSION

DC Motor
A DC motor is a fairly simple electronic motor that
uses electricity and a magnetic field to produce torque, which
turns the motor. There are two controllable parameters of a
DC motor; direction and speed. To control the direction, the
polarity of the motor is reversed. To control the speed, the
input voltage is varied using pulse width modulation
(PWM).
Figure 1. A DC Motor
Driving DC Motors
DC motors rotate continuously when a DC voltage is applied across them. These
motors are commonly used as the driving motors in radio control (RC) cars, in power drills
and saws, and as the motors that make the discs spin in DVD players. DC motors are
great because they come in a huge array of sizes and are generally very cheap. By
adjusting the voltage you apply to them, you can change their rotation speed. Using a
gearbox, you can trade their speed for torque. By reversing the direction of the voltage
applied to them, you can change their direction of rotation as well. This is generally done
using an H-bridge.
Brushed DC motors, such as the ones used in this activity, employ stationary
magnets (the stator) and a spinning coil (the rotor). Electricity is transferred to the coil
using “brushes,” hence the name brushed DC motors. Unlike brushless DC motors (such
as the stepper motors that will be discussed later in this activity), brushed DC motors are
cheap and offer easier speed control. However, brushed DC motors do not last as long
because the brushes can wear out over time.

1
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

Brushed DC motors work through an inductive force. When current passes through
the spinning coil, it generates a magnetic field that is either attracted to or repelled by the
stationary magnets, depending on the polarity. By using the brushes to swap the polarity
each half-rotation, you can generate angular momentum.

Direction Control
To control a DC motor from a microcontroller,
a switching arrangement known as H- Bridge is used.
When switches 1 and 4 are closed and 2 and 3 are
open, voltage flows from the supply to 1 to the motor to 4 to
ground. When 2 and 3 are closed and 1 and 4 are open,
polarity is reversed, and voltage flows from the supply to 3
to the motor to 2 to ground.
Although an H-bridge is easy to construct, it is
usually easier to use a controller manufactured
specifically for the job. A pre-manufactured H-bridge chip
will include diodes to protect the transistors from back Figure 2. Schematic of an
voltage, sometimes a current sensing pin to sense the H-bridge Circuit
current the motor is drawing, and much more. One available
chip is the one used in this activity, which is the L293D push-pull four channel driver. This
chip is capable of controlling two DC motors at once.

L293D as H-Bridge Circuit


With the preceding considerations in
mind, it’s time to build the circuit. The H-
bridge chip you will use is the L293D
quadruple half-H driver. Two half-H drivers
are combined into one full-H driver, such as
the one shown in the Figure 3.
In this activity, we will just use two of
the four half-H drivers to drive one DC
motor. If you want to create something
more advanced, you can use this chip to Figure 3. H-bridge operation
control two DC motors.
For example, to make an RC car,
you would use one for the left
wheels and one for the right wheels.
Before you actually get it wired up,
take a look at the pin-out and logic
table from the part’s datasheet,
shown in Figure 4.
Pin numbering on integrated
circuits (ICs) always starts at the
top-left pin and goes around the part Figure 4. H-bridge pin-out and truth table
counterclockwise. Chips will always have some
kind of indicator to show which pin is pin 1, so that you don’t plug the IC in upside-down.
On through-hole parts, a half circle on one end of the chip indicates the top of the chip
(where pin 1 is located). Some chips may have a small circle marked next to pin 1 on the
plastic casing in addition to, or instead of, the half circle.
Let’s run through the pins and how you’ll be using them.

• GROUND/HEATSINK (pins 4, 5, 12, and 13). The four pins in the middle
connect to a shared ground between your 9V and 5V supplies. They also
heatsink the driver into the ground of your circuit. On a printed circuit board,

2
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

chips are often designed to shunt excess heat to ground because the ground
often has the most copper surface area.
• VCC2 (pin 8). VCC2 supplies the motor current, so you connect it to 9V.
• VCC1 (pin 16). VCC1 powers the chip’s logic, so you connect it to 5V.
• 1Y and 2Y (pins 3 and 6). These are the outputs from the left driver. The motor
wires connect to these pins.
• 1A and 2A (pins 2 and 7). The states of the switches on the left are controlled
by these pins, so they are connected to I/O pins on the Arduino for toggling.
• 1,2EN (pin 1). This pin is used to enable or disable the left driver. It is connected
to a PWM pin on the Arduino, so that speed can be controlled dynamically.
• 3Y and 4Y (pins 11 and 14). These are the outputs from the right driver.
Because you are using the left driver only, you can leave them disconnected.
• 3A and 4A (pins 10 and 15). The states of the switches on the right are
controlled by these pins, but you are using only the left driver in this example,
so you can leave them disconnected.
• 3,4EN (pin 9). This pin is used to enable or disable the right driver. Because
you will not be using the right driver, you can disable it by connecting this pin
directly to ground.

Speed
The speed of a DC motor is proportional to the supplied voltage. If the voltage drops
too far, the motor won’t get enough power to turn, but within a certain range, usually 50%
of the rated voltage, the motor will run at varying speeds. The most effective way to adjust
the speed is by using pulse width modulation (PWM). This means that you pulse the motor
on and off at varying rates, to simulate a voltage. Below are some examples of pulse
widths and the voltages they would simulate.

Figure 5. PWM signal with a 50% duty cycle. A 50% duty cycle results to an
effective voltage which is about half the total voltage.

Figure 6. PWM signal with a 25% duty cycle. A 25% duty cycle results to an
effective voltage which is about a quarter of the total voltage.

A PWM signal could be produced easily in Arduino by using the command


analog Write(). In this activity, speed control is achieved by feeding the PWM signal to
one of the “enable” pins of the motor driver chip L293D, which in turn switches on and off
the DC motor based on the input PWM signal pattern and thus effectively simulating
different voltage levels.

3
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

Servo Motor
A servo motor is an electromechanical device in which an electrical input
determines the position of the armature of a motor. Servos have integrated gears and a
shaft that can be precisely controlled. They are available in a wide range of sizes and
capabilities, with some modified for continuous rotation, and others designed for rotation
over a small range with high torque.
A servo motor can be a continuous rotation or a standard servo. Unmodified servos
always have a fixed range (usually from 0 to 180
degrees) because there is a potentiometer in
line with the drive shaft, which is used for
reporting the present position. Servo control is
achieved by sending a pulse of a particular
length. In the case of a standard rotation servo,
the length of the pulse determines the absolute
position that the servo will rotate to. If you
remove the potentiometer, however, the servo is
free to rotate continuously, and the pulse length
sets the speed of the motor instead
Unlike their DC motor counterparts, servo
motors have three pins: power (usually red), Figure 7. Servo motors
ground (usually brown or black), and signal or
control (usually white or orange). These wires are color-coded, typically in the same order,
and generally look like the ones shown in Figure 8.
The color-coding might vary slightly between servos, but the color schemes just
listed are the most common. (To make sure always check the servo’s documentation
before experimenting.) Like DC motors, servos can draw quite a bit of a current (usually
more than the Arduino can supply). Although you can sometimes run one or two small
servos directly from the Arduino’s 5V supply, you will generate a separate 5V power
supply for the servos so that you have the option to add more if you need to. Unlike DC
motors, servos have a dedicated control pin that instructs them what position to turn to.
The power and ground lines of a servo should always be connected to a steady power
source.
Servos are controlled using adjustable pulse widths on the signal line. For a
standard servo, sending a 1 ms 5V pulse turns the motor to 0 degrees, and sending a 2
ms 5V pulse turns the motor to 180 degrees, with pulse lengths in the middle scaling
linearly. A 1.5 ms pulse, for example, turns the motor to 90 degrees. Once a pulse has
been sent, the servo turns to that position and stays there until another pulse instruction
is received. However, if you want a servo to “hold” its position (resist being pushed on and
try to maintain the exact position), you just resend the command once every 20 ms. The
Arduino servo commands that you will later employ take care of this for you. The timing
diagram shown in Figure 8 shows how servo control works.

Figure 8. Servo motor timing diagram (Created with MATLAB)

4
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

Standard servos allow the shaft to be positioned at various angles, usually between
0 and 180 degrees. Continuous rotation servos allow the rotation of the shaft to be set to
various speeds.

Arduino Servo Library (Servo.h)


The Servo library allows control of up to 12 Servo motors on most Arduino boards
and 48 on the Arduino Mega. On boards other than the Mega, use of the library disables
the analogWrite() functionality on pins 9 and 10, whether or not there is a Servo on those
pins.

The most commonly used methods in the Servo.h library are listed below.

• attach(pin) – attaches the Servo variable to a pin.


• write(angle) – writes a value to the servo, controlling the shaft accordingly. On a
standard servo, this will set the angle of the shaft (in degrees), moving the shaft
to that orientation. On a continuous rotation servo, this will set the speed of the
servo (with 0 being full-speed in one direction, 180 being full speed in the other,
and a value near 90 being no movement).
• read() – read the current angle of the servo (the value passed to the last call to
write().
• detach() – detach the Servo variable from its pin. If all Servo variables are
detached, then pins 9 and 10 can be used for PWM output with analogWrite().

Sample Program:
#include <Servo.h> //import Servo.h library
Servo myservo; //instantiate a Servo object void setup()
{
myservo.attach(9); // attach the servo on pin 9
}

void loop()
{
//set the angle of the servo to 90 degrees myservo.write(90);
}

If Servo Misbehaves
Your servo may behave erratically, and you may find that this only happens when
the Arduino is plugged into certain USB
ports. This is because the servo draws quite
a lot of power, especially as the motor is
starting up, and this sudden high demand
can be enough to drop the voltage on the
Arduino board, so that it resets itself.
If this happens, then you can usually
cure it by adding a high value capacitor
(470uF or greater) between GND and 5V as
shown in figure 9.
The capacitor acts as a reservoir of
electricity for the motor to use, so that when
it starts, it takes charge from the capacitor as
well as the Arduino supply. The longer lead of Figure 9. Servo with capacitor

5
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

the capacitor is the positive lead and this should be connected to 5V. The negative lead
is also often marked with a '-' symbol.

Stepper Motor
Stepper motors are used to move in
discrete steps, which means you can control the
steeper motors with precision. There are many
usages of stepper motors, which includes
medical scanners, 3D printers, in camera for
auto focus, and many more. In this activity, a
28BYJ-48 Stepper motor will be used to
interface to Arduino Uno using a ULN2003
driver.

Figure10. 28BYJ-48 Stepper Motor


There are two basic types of stepper motors, unipolar stepper motors and bipolar
stepper motors. Unipolar stepper motors typically have two coils per phase: one for
each direction of magnetic field. Bipolar stepper motors typically have one coil per
phase, and current flows in both directions through this coil. Thus, of two motors of
identical size, the bipolar would be able to produce twice as much torque, since at any
given time, the unipolar motor is only using half of its windings.

Features of a stepper motor


What makes a stepper motor different from regular motor? Stepper motors can
move in discrete steps, where each step is a fixed angle increment. This is rather similar
to servo motors as the motion of the servo is controlled by telling it how much angle you
want it (the servo motor) to move, but the motion of servos are usually constrained from
0 to 180 degrees. Therefore, if you need to rotate continuously while maintaining
precision, stepper motor would be our choice. However, stepper motors are a bit harder
to control than regular motors & servos.

The 28BYJ-48 Stepper Motor Datasheet


The 28BYJ-48 is a small, cheap, 5 volt geared stepping motors. These stepping
motors are apparently widely used to control things like automated blinds, A/C units and
are mass produced.

Here are the specifications of this stepper motor:


Rated voltage : 5V DC
Number of Phase: 4
Speed Variation Ratio: 1/64
Step Angle (8-Step sequence: Internal Motor alone): 5.625° (64 steps per
revolution)
Step Angle (4-Step sequence: Internal Motor alone): 11.25° (32 steps per
revolution)
Frequency: 100Hz

Since the stepper motor has a stride angle of 5.625° and a gear ratio of 1:64, we
can determine to total number of steps by multiplying the number of steps in one revolution
with its gear ratio. In the case of this motor:

Steps in One Revolution: (360°/5.625°) = 64 Steps


Total steps (8 steps sequence) = Steps in One Revolution x Gear Ratio
= 64 x 64 =4096 steps

6
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

Total steps (4 steps sequence) = Steps in One Revolution x Gear Ratio


= 32 x 64 =2048 steps

Therefore, we can conclude that this stepper motor has a 4096 Steps in one
revolution while 2048 steps in half revolution.

Control
Step motor converts pulse to angle displacement. So, when the stepper driver
receives a certain pulse signal, it will move the step motor to a certain angle. With this,
the angle of the stepper moved is determined by the number of the pulse. On the other
hand, the speed of the stepper motor is determined by the frequency of the pulse. The
following image is the switching sequence of the stepper motor.

Figure 11. Stepper motor sequence

Referring to the table above, we can drive the motor by enabling the pins in an 8-phase
order from left to right. The Lead wire color can be found on the stepper motor. (Clockwise
movement):

• Drive IN4 only


• Drive IN4 and IN3
• Drive IN3 only
• Drive IN3 and IN2
• etc.
For anti-clockwise motion, we will simply reverse the sequence.

ULN2003 stepper motor driver


The ULN2003 stepper motor driver board allows
you to easily control the 28BYJ-48 stepper motor
from a microcontroller, like the Arduino Uno.
One side of the board side has a 5-wire socket
where the cable from the stepper motor hooks
up and 4 LEDs to indicate which coil is currently
powered. The motor cable only goes in one way,
which always helps. UNL2003 board. On the
side you have a motor on / off jumper (keep it on
to enable power to the stepper). The two pins
below the 4 resistors, is where you provide
power to the stepper. Note that powering the
stepper from the 5 V rail of the Arduino is not
recommended. A separate 5-12 V 1 Amp power
supply or battery pack should be used, as the
motor may drain more current than the
microcontroller can handle and could potentially
Figure 12. ULN2003 Stepper motor driver

7
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

damage it. In the middle of the board, we have the ULN2003 chip. At the bottom are the
4 control inputs that should be connected to four Arduino digital pins.

IV. DIAGRAM

Figure 13(a). DC Motor Control Connection using H-Bridge IC

Figure 13(b). DC Motor Control Connection with Button Switch using H-Bridge

Figure 13(c). DC Motor Control Connection with LDR using H-Bridge

8
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

Figure 13(d). Servo Motor Connection

Figure 13(e). Servo Motor Connection with Button Switch

Figure 13(f). Stepper motor breadboard layout wiring diagram

Figure 13. Breadboard Layout Wiring Diagram

9
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

V. PROCEDURES
Part I. Driving DC Motor

A. Forward/Reverse direction control using serial monitor (H-Bridge based)


1. Construct the breadboard connection in Figure 13(a).
2. Once connected, Encode the sketch below and upload it to the Arduino board.
int inPin1 = 5;
int inPin2 = 4;

void setup()
{
Serial.begin(9600);
pinMode(inPin1, OUTPUT);
pinMode(inPin2, OUTPUT);
Serial.println("+ - to set direction, any other key stops motor");
}

void loop()
{
if(Serial.available()){
char ch=Serial.read();
if(ch == '+')
{
Serial.println("CW");
digitalWrite(inPin1, LOW);
digitalWrite(inPin2, HIGH);
}
else if(ch == '-')
{
Serial.println("CCW");
digitalWrite(inPin1, HIGH);
digitalWrite(inPin2, LOW);
}
else
{
Serial.println("STOP");
digitalWrite(inPin1, LOW);
digitalWrite(inPin2, LOW);
}
}
}

3. Open the Serial Monitor and type plus ‘+’ and minus ‘-‘sign one at a time and
observed the movement of the DC motor.

B. Forward/Reverse direction control using switch (H-Bridge based)


1. Construct the breadboard connection in Figure 13(b).
2. Once connected, Encode the sketch below and upload it to the Arduino board.

//tact switch input pin


int inputPin = 2;

//output pins used to control the DC motor


int motorPin1 = 4;
int motorPin2 = 5;

//direction of rotation of the DC motor


int dir = LOW;

//used for detecting the state change of the tact switch


int prevState = 0;
int currentState = 0;

10
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

void setup()
{
//initialize I/O pins
pinMode(inputPin, INPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
}

void loop()
{
//detect state change in the tact switch
currentState = digitalRead(inputPin);

if (currentState != prevState)
{
//if the current state is HIGH then the button
//went from off to on
if (currentState == HIGH)
{
//if the tact switch is pressed, toggle the
//direction of rotation of the DC motor
dir = !dir;
}
}

prevState = currentState;

//drive the DC motor clockwise or counterclockwise


//as indicated by dir
if (dir == HIGH)
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
}
else
{
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
}
}

3. Push the button switch connected to the Arduino. Observe the DC motor as you
push the button switch.

C. Variable speed control (H-Bridge based)


1. Construct the breadboard connection in Figure 13(c).
2. Once connected, Encode the sketch below and upload it to the Arduino board.

//analog input pin


int LDRPin = A1;
//output pins used to control the DC motor
int motorPin1 = 4, motorPin2 = 5, motorEnable = 9;
//stores PWM value used to control speed of the motor
int motorSpeed;
//used for calibration
int sensorMin, sensorMax, sensorReading;

int Ledpin = 13;

void setup()
{
//initialize I/O pins
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorEnable, OUTPUT);
pinMode(Ledpin, OUTPUT);
//signal start of calibration
digitalWrite(Ledpin, HIGH);

11
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

//calibrate during the first five seconds


while (millis() < 5000)
{
sensorReading = analogRead(LDRPin);
if (sensorReading > sensorMax)
{
sensorMax = sensorReading;
}

if (sensorReading < sensorMin)


{
sensorMin = sensorReading;
}
}

//signal the end of calibration


digitalWrite(Ledpin, LOW); //set the pin13 to LOW

//run the DC motor


digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
}

void loop()
{
//read LDR value
int x = analogRead(LDRPin);

//map to valid PWM output range


motorSpeed = map(x, sensorMin, sensorMax, 0, 255);

//output PWM signal


analogWrite(motorEnable, motorSpeed);

//ADC settling time delay


delay(15);
}

3. Place your hand over the LDR (Photoresistor) and calibrate the light intensity
moving your hand with varying distance for 5 seconds and after 5 observed the
behavior of the DC motor by placing again your hand over the LDR.

Part II. Servo Motor Control


A. Sweep
1. Construct the breadboard connection in Figure 13(d).
2. Once connected, Encode the sketch below and upload it to the Arduino board.

#include <Servo.h>

//instantiate a Servo object


Servo myservo;

//variable to store the servo position


int pos = 0;
void setup()
{
//attaches the servo on pin 9 to the Servo object
myservo.attach(9);
}

void loop()
{
//go from 0 degrees to 180 degrees
//in steps of 1 degree
for(pos = 0; pos < 180; pos += 1)
{

12
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

//tell servo to go to position in variable ‘pos'


myservo.write(pos);
//waits 15ms for the servo to reach the position
delay(15);
}
//go from 180 degrees to 0 degrees
//in steps of 1 degree
for(pos = 180; pos>=1; pos-=1)
{
myservo.write(pos);
delay(15);
}
}

3. Observed the movement of the servo motor

B. Position control using Button Switch


1. Construct the breadboard connection in Figure 13(e).
2. Once connected, Encode the sketch below and upload it to the Arduino board.
#include <Servo.h>

//create a Servo object to control a servo motor


Servo myservo;

//indicates position of the servo


int dir1=LOW, dir2=LOW;
int Sbutton1 = 2;
int Sbutton2 = 3;

void setup()
{
//initialize I/O pins
pinMode(Sbutton1, INPUT);
pinMode(Sbutton2, INPUT);

//attaches the servo on pin 9 to the Servo object


myservo.attach(9);
//initialize servo to 0 degrees
myservo.write(0);
}

void loop()
{
//call the function posControl1 when signal received
//from pin 2 changes from LOW to HIGH

attachInterrupt(0, posControl1, RISING);

//call the function posControl2 when signal received


//from pin 3 changes from LOW to HIGH
attachInterrupt(1, posControl2, RISING);
}

void posControl1()
{
dir1 = !dir1; //toggle dir1
if (dir1 == HIGH)
{
myservo.write(90);//position servo to 90 degrees
}
else
{
myservo.write(180);//position servo to 180 degrees
}
}

13
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

void posControl2()
{
dir2 = !dir2; //toggle dir2
if (dir2 == HIGH)
{
myservo.write(45);//position servo to 45 degrees
}
else
{
myservo.write(135);//position servo to 135 degrees
}
}

3. Pressed the switch one after another and observed the movement of the servo
motor

Part III. Stepper Motor control


A. Half stepping mode
1. Construct the breadboard connection in Figure 13(f).
2. Once connected, Encode the sketch below and upload it to the Arduino board.

#define IN1 8
#define IN2 9
#define IN3 10
#define IN4 11
int Steps = 0;
boolean Direction = true;//
unsigned long last_time;
unsigned long currentMillis ;
int steps_left=2048;
long time;
void setup()
{
Serial.begin(115200);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
// delay(1000);

}
void loop()
{
while(steps_left>0){
currentMillis = micros();
if(currentMillis-last_time>=1000){
stepper(1);
time=time+micros()-last_time;
last_time=micros();
steps_left--;
}
}
Serial.println(time);
Serial.println("Wait...!");
delay(2000);
Direction =!Direction;
steps_left = 2048;
}

void stepper(int xw){


for (int x=0;x<xw;x++){
switch(Steps){
case 0:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);

14
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
case 1:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, HIGH);
break;
case 2:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 3:
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 4:
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 5:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;

case 6:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 7:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
default:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
}
SetDirection();
}
}
void SetDirection(){
if(Direction==1){ Steps++;}
if(Direction==0){ Steps--; }
if(Steps>7){Steps=0;}
if(Steps<0){Steps=7; }
}

3. Observed what happened to the Stepper motor.

15
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

B. Speed Control
1. Using the previous connection (half stepping), Encode the sketch below and
upload it to the Arduino board.

int motorPin1 = 8; // Blue - 28BYJ48 pin 1


int motorPin2 = 9; // Pink - 28BYJ48 pin 2
int motorPin3 = 10; // Yellow - 28BYJ48 pin 3
int motorPin4 = 11; // Orange - 28BYJ48 pin 4
// Red - 28BYJ48 pin 5 (VCC)

int motorSpeed = 1200; //variable to set stepper speed


int count = 0; // count of steps made
int countsperrev = 512; // number of steps per full revolution
int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011,
B00001, B01001};

void setup() {
//declare the motor pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
Serial.begin(9600);
}

void loop(){
if(count < countsperrev )
clockwise();
else if (count == countsperrev * 2)
count = 0;
else
anticlockwise();
count++;
}

//set pins to ULN2003 high in sequence from 1 to 4


//delay "motorSpeed" between each pin setting (to determine speed)
void anticlockwise()
{
for(int i = 0; i < 8; i++)
{
setOutput(i);
delayMicroseconds(motorSpeed);
}
}

void clockwise()
{
for(int i = 7; i >= 0; i--)
{
setOutput(i);
delayMicroseconds(motorSpeed);
}
}

void setOutput(int out)


{
digitalWrite(motorPin1, bitRead(lookup[out], 0));
digitalWrite(motorPin2, bitRead(lookup[out], 1));
digitalWrite(motorPin3, bitRead(lookup[out], 2));

16
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

digitalWrite(motorPin4, bitRead(lookup[out], 3));


}

2. Observed what happened to the Stepper motor and try to vary the speed.

VI. PROGRAMMING ACTIVITY. Create the connections and the


program of the following activities.

a. Create a program that controls the speed and direction of rotation of a DC motor.
Use a button switch to toggle between the forward and reverse rotation of the DC
motor and an LDR to vary its speed. Use the RGB LED to indicate its current
direction of rotation.
b. Using a LDR as an input, create a program that controls the position of the servo
motor. If the ambient light is “dark”, turn the servo motor at 0 degrees; “medium
dark”, 45 degrees; “medium”, 90 degrees; “medium bright”, 135 degrees; and
“bright”, at 180 degrees. Print the detected ambient light intensity and servo’s
angle to the LCD Display.
c. Another stepping mode used to drive stepper motors is the high torque stepping.
Create a program that utilizes this mode for a stepper motor. Use a tact switch to
toggle the stepper’s direction of rotation (clockwise or counterclockwise) and LED
to its direction of rotation.

ASSESSMENT RUBRICS (ACTIVITY)

Name: ______________________________________ Course and Year: _________


Due Date: _____________________

(PROGRAMMING ACTIVITY 1a) ( PROGRAMMING ACTIVITY 1b)


Date Checked: __________________ Date Checked: __________________

CRITERIA POINTS CRITERIA POINTS

Functionality (35) Functionality (35)


Code Efficiency (35) Code Efficiency (35)
Breadboard Component Breadboard Component
Layout/Fritzing Design Layout/Fritzing Design
Connections (20) Connections (20)
Wiring Wiring
(Neatness, color coding) (5) (Neatness, color coding) (5)
Timeliness (5) Timeliness (5)
TOTAL EARNED POINTS TOTAL EARNED POINTS

17
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

( PROGRAMMING ACTIVITY 1c)


Date Checked: __________________
CRITERIA POINTS

Functionality (35)
Code Efficiency (35)
Breadboard Component
Layout/Fritzing Design
Connections (20)
Wiring
(Neatness, color coding) (5)
Timeliness (5)
TOTAL EARNED POINTS

18

You might also like