0% found this document useful (0 votes)
66 views10 pages

Micro Lab 6

This lab report describes controlling DC motors using an Arduino Uno board and L298 motor driver IC. Code 1 generates a 25% duty cycle PWM signal on pin PB0 to control a motor speed. Code 2 uses timers to generate delays and control two motors connected to an L298 IC, producing a 15% duty cycle PWM signal. The second task controls motor speed using phase-correct PWM to vary the duty cycle and speed of two motors connected to an L298 motor driver.

Uploaded by

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

Micro Lab 6

This lab report describes controlling DC motors using an Arduino Uno board and L298 motor driver IC. Code 1 generates a 25% duty cycle PWM signal on pin PB0 to control a motor speed. Code 2 uses timers to generate delays and control two motors connected to an L298 IC, producing a 15% duty cycle PWM signal. The second task controls motor speed using phase-correct PWM to vary the duty cycle and speed of two motors connected to an L298 motor driver.

Uploaded by

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

MtE316 – Microprocessors and Microcontrollers, Fall 2019

Lab Report 06: Dc Motor Control

Submitted by:
Bilal Ahmad (17PWMCT0541)
Musawir Shah (17PWMCT0545)
Muhammad Tufail (17PWMCT054)

Submitted to:
Engr.Shahbaz Khan

DEPARTMENT OF MECHATRONICS ENGINEERING


UNIVERSITY OF ENGINEERING & TECHNOLOGY, PESHAWAR

Date (9 Dec 2019)


OBJECTIVES:

After successfully completing this lab, we should be able to:


1. Describe the basic operation of a DC motor
2. Code to control and operate a DC motor
3. Use PWM and write program to control motor speed and direction

REQUIRED MATERIALS:
1. Arduino UNO board
2. USB Cable
3. Breadboard
4. Power Supply
5. Computer with Atmel Studio 7.
6. Two 5V or 12V DC Motor
7. Transistors
8. L298d Motor-driver IC

MATERIALS DESCRIPTION:
ARDUINO UNO BOARD:
The Arduino UNO is an open-source microcontroller board based on the Microchip Atmega328P
microcontroller and developed by Arduino.cc. The board is equipped with sets of digital and analog
input/output (I/O) pins that may be interfaced to various expansion boards (shields) and other circuits.

TECHNICAL SPECIFICATIONS:

 Microcontroller: Microchip ATmega328P


 Operating Voltage: 5 Volt
 Input Voltage: 7 to 20 Volts
 Digital I/O Pins: 14 (of which 6 provide PWM output)
 Analog Input Pins: 6
 DC Current per I/O Pin: 20 mA
 DC Current for 3.3V Pin: 50 mA Figure 1:Arduino UNO
 Flash Memory: 32 KB of which 0.5 KB used by bootloader
 SRAM: 2 KB
 EEPROM: 1 KB
 Clock Speed: 16 MHz
 Length: 68.6 mm
 Width: 53.4 mm
 Weight: 25 g

L298 MOTOR DRIVER IC:

It is a high voltage, high current, dual full-bridge driver designed


to accept standard TTL logic levels (Control Logic) and drive
inductive loads such as relays, solenoids, DC and Stepper motors.
Two enable inputs are provided to Enable or disable the device
independently of the input signals. The emitters of the lower
transistors of each bridge are connected together and the
corresponding external terminal can be used for the connection
of an external sensing resistor. [1]
FEATURES AND SPECIFICATIONS:

 Operating voltage range: +5 to +46V


 Maximum supply voltage:50V
 Maximum Input and Enable Voltage : +7V
 Maximum current allowed to draw through each output : 3A
 TTL control inputs
 Total power dissipation:25W
 Operating temperature: -23°C to 130°C
 Storage Temperature: -40°C to 150°C

PIN CONFIGURATION:
L298 IC has 15 pins in total, each having different functions
Figure 2 : L298 IC
associated with them.

Figure 3 : L298 IC PIN CONFIGURATION

DC MOTOR:
A DC motor is any of a class of rotary electrical machines that converts direct current electrical energy
into mechanical energy. The most common types rely on the forces produced by magnetic fields.
Nearly all types of DC motors have some internal mechanism, either electromechanical or electronic,
to periodically change the direction of current flow in part of the motor. [2]

A DC motor is an electromechanical device that translates input electrical power (𝑉×𝐼) into output
mechanical power (𝜏×𝜔). When the positive and negative leads of a DC motor are connected to a DC
voltage source (battery), the motor starts moving continuously in one direction. [2]

TYPES:
Figure 4:DC MOTORS TYPES

TASK 1:
Properly comment each line of code in Program 1 and 2. Explain in theory what the program has
achieved. Explain calculation of values in registers TCNTx, TCCRxA, TCCRxB, and OCCRxA registers (where
x=0 or 1) in both programs. Also explain the duty cycle and frequency of resulting PWM signals. Assume
XTAL=16MHz.
CODE 1:
#define F_CPU 16000000UL
#include <avr/io.h>
/********** Main Program *******************/
int main(void)
{
DDRB = 0x0C; //set PORTB as OUTPUT
PORTB = 0xFF; //set PORTB HIGH
TCCR0A = 0xC1; //set Timer0 in CTC mode, no prescaling
OCR0A = 64; //set OCROA value to 64
while(1);
}
EXPLANATION:
In this Program we set the PORTB as OUTPUT and make it HIGH .We set the timer in CTC(Clear
Timer ON Compare Match) mode and no prescalling by setting TCCR0A to 0xC1(10110001 in binary) .

Figure 5:TCCR0 REGISTER BITS


Figure 6 : Waveform Generation mode bits

As we know that in CTC mode the TCNTx steps-up to value of OCRxA .The value we save in OCRxA will be
the top value for TCNT.We set the value of OCR0A(Output Compare Register) of timer 0 to 64 , which
means timer will give time delay until the TCNT0 become equal to 64.
We can find the duty cycle and frequency of resulting PWM signal as given below
Duty Cycle:
OCR0A=64
Duty Cycle=ON time/OFF time x 100%.
Duty Cycle =64/255 x 100% = 25.09%
Frequency:
1/16MHz=0.0625 micro sec
256-64 = 192
So
Delay = 192 x 0.0625 micro sec = 12 micro sec
Time period = 12 micro sec x 2 = 24 micro sec
Frequency = 1/ 24 micro sec = 42 KHz approx.

PROTEUS CIRCUIT DIAGRAM:


Figure 7:Proteus Circuit diagram for code 1

CODE 2:
#include <avr/io.h>
#define uchar unsigned char
#define uint unsigned int

#define sbit(x,PORT) (PORT) |= (1<<x) //defined sbit as PORT to set a bit to 1


#define cbit(x,PORT) (PORT) &= ~(1<<x) //defined cbit as PORT to set a bit to 0
#define pin(x,PIN) (PIN) & (1<<x) //defined pin as PIN for reading input from a pin

void delays1ms()
{
TCNT1 = 0; //Setting TCNT1 to 0 of timer 1
OCR1A = 10000-1; //Save a top value for TCNT1 in OCR1A of timer 1.
TCCR1A = 0; //Set TCCR1A for setting Timer Mode
TCCR1B = 0x09; //Set the timer to CTC mode and Setting the Clock
while((TIFR1&(1<<OCF1A))==0){} //waiting till the OCF1A goes HIGH
TCCR1B = 0; //Stop the Timer
TIFR1 = 1<<OCF1A; //Set the TIFR1 to run again
}
int main(void)
{
int i;
DDRB = 0xFF; //make PORTB OUTPUT
PORTB = 0xFF; //make PORTB HIGH
sbit(4,PORTB); // set PORTB4 HIGH
cbit(0,PORTB); //Set PORTB0 LOW
sbit(5,PORTB); //Set PORTB5 high
cbit(1,PORTB); //Set PORTB1 LOW

while (1)
{
sbit(3,PORTB); //Set PORTB3 HIGH
sbit(2,PORTB); //Set PORTB2 HIGH
for (i=1;i<=25;i++) //generating large Time delay
{
delays1ms();
}
cbit(3,PORTB); //Set PORTB3 LOW
cbit(2,PORTB); //Set PORTB2 LOW
for (i=1;i<=95;i++)
{
delays1ms();
}
}
}

EXPLANATION:
In this program we are contolling two motors using L298 ic.In this code we generate a time delay of 1ms
in function called delay1ms using Timer 1, as shown from the value of OCR1A i-e 10000 - 1.We use Timer
1 in CTC (Clear Timer on Compare Match) mode and using oscillator Clock frequency with no
prescaler,as we set the value of TCCR1A to 0 and TCCR1B to 0x09(00001001).

Figure 8 TCCR1A Register bits

Figure 9 TCCR1B Register bits

We use 4 pins for L298 IC inputs and 2 pins as PWM pins for Enable A and Enable B of L298 IC.We
calculated the value of Duty Cycle and Frequency as given below

DUTY CYCLE:
OCR1A=9999
ON time/OFF time x 100%.
Duty Cycle =9999/65536 x100% = 15.25%

FREQUENCY:
1/16MHz=0.0625 micro sec
As OCR1=10000
So
Delay = 10000 x 0.0625 = 625 micro seconds
Time period = 625 x 2= 1350 micro seconds
Frequency = 1/1350 micro sec= 740.7 Hz
PROTEUS CIRCUIT DIAGRAM:

Figure 10 : Proteus Circuit diagram for Code 2

TASK 2:
Control speed of motors in Figure 9 using Phase-correct PWM technique.

CODE:
#include <avr/io.h>

void delay1ms()
{
TCNT0 = 156; //Set value for TCNT0
TCCR0A =0x01; //set the timer 0 to phase Correct PWM
TCCR0B =0x01; //set the prescaler
while ((TIFR0 & (1<<TOV0))==0){} //wiat for timer to overflow
TCCR0B =0; //stop the timer
TIFR0 |=(1<<TOV0); //set the TIFR0

}
int main(void)
{
int i;
DDRB=0xFF; //set PORTB as Output
PORTB =0xFF; //set PORTB HIGH

PORTB |=(1<<4);
PORTB &=~(1<<0);
PORTB |=(1<<5);
PORTB &=~(1<<1);

while(1)
{
PORTB |=(1<<3);
PORTB |=(1<<2);
for(i=1;i<=25;i++) //generating large time delay
{
delay1ms();
}
PORTB &=~(1<<3);
PORTB &=~(1<<2);
for(i=1;i<=55;i++) //genarating large time delay
{
delay1ms();
}
}
}

EXPLANATION:
We set the timer 0 in Phase Correct PWM mode .
For this purpose we make a delay function and set the TCCR0A to 0x01 and TCCR0A to 0x01.As the
TCCR0 register bits is given below

Figure 11: TCCR0 Register bits

And the values for waveform generation mode is given below

Figure 12 WaveForm Generation mode bits

We control the Motor speed by giving value to TCNT0 register as here we given the value of 156 which
generates a time delay of 1ms.

TASK 3:
Write a program in Arduino IDE to achieve the same behaviour as in Task 2.

CODE:
Figure 13 Code for Task 3

EXPLANATION :
Outside the loops we define the constants for inputs pin for motors and PWM pins.Pin 12 and 8 is input
pins for motor 1 and pin 10 is PWM pin for motor 1.Pin 13 and 9 is input pins for motor 2 and pin 11 is
PWM pin for motor 2.
In setup loop we set all the pin as Output and set initial direction for motors.The motor 1 will move in
Forward direction and motor 2 will move in backward direction.
In loop we send values to motor using PWM pins.We send the value of 155 so motor will not give full
speed and if we change the value the motor speed will change correspondingly.

PROTEUS CIRCUIT DIAGRAM:

Figure 14 Circuit Diagram for Task 3

You might also like