Project 1 - Controlling a DC motor using PWM and monitoring its Running status
CONTROLLING A DC MOTOR USING PWM TECHNIQUE
AIM
Using PWM technique controlling the DC motor and printing the results.
BILL OF MATERIALS
DC MOTOR – 1 no
LCD DISPLAY – HD44780 – 1 no
MOTOR DRIVER – L293 – 1 no
PUSH SWITCH – 3 Nos
RESISTOR – 10K ohm – 1 no
OSCILLOSCOPE – 1 no
COMPONENTS WORKING PRINCIPLE
DC MOTORS WORK
A current carrying wire in the presence of a magnetic field experiences a mechanical force that acts in a particular direction.
To determine the focus of this force, Sir John Ambrose Fleming established a simple way that uses one’s left hand to visualize the relationship between the flow of current, the direction of the magnetic field, and the direction of the force. This simple yet highly effective technique became known as the Fleming Left-hand rule.
In a DC motor, a shaft is connected to a wire coil that has current running through it. There are also circular magnets surrounding the wire coil. When the motor is supplied power, the current flows through the wire coil, and the magnetic field causes the coil to rotate and turn the shaft.
PROPERTIES OF DC MOTORS
When selecting a DC motor, you need to consider several characteristics that specify the electrical properties of the motor. This includes:
· Voltage: You can find DC motors that operate from as few as 1.5V all the way up to 100V.
· Torque: Torque is the amount of rotational force that a motor produces (in Newton-meters). DC motors for hobby projects can range from as low as 2.8 g-cm to as high as 58 kg-cm.
· Starting torque: The maximum torque a motor produces to begin the rotational movement of the load. DC motors have a high starting torque.
· Motor speed: The rate of motor rotation in revolutions per minute (RPM). Typical no-load speeds for DC motors range from 1-20,000 RPM.
HOW TO CONTROL MOTOR SPEED WITH PWM
The speed of a DC motor is directly proportional to the supply voltage. A simple way to control the speed of a DC motor is to regulate the supply voltage with pulse width modulation (PWM).
Pulse Width Modulation (PWM)
The basic idea behind PWM is that it switches the supply voltage ON and OFF very quickly. By adjusting the length of the ON/OFF pulses, we can set the voltage to anywhere between 0V and the maximum voltage. We will use this PWM signal to power the motor directly.
FEATURES OF L293:
The L293 is a popular motor driver IC (Integrated Circuit) commonly used to control the direction and speed of DC motors. It's designed to provide bidirectional control for up to two DC motors or a single stepper motor. The L293 allows you to interface low-level logic signals from a microcontroller to drive higher current loads, making it ideal for motor control in various applications. Here's an overview of the L293 motor driver:
Dual H-Bridge Configuration: The L293 consists of two independent H-bridges, each capable of controlling the direction of one motor. H-bridges are circuits that allow you to control the polarity of the voltage applied to the motor, enabling both forward and reverse motion.
High Current Capability: The L293 can handle relatively high current loads, making it suitable for driving medium-sized DC motors or stepper motors.
Logic-Level Inputs: The IC is designed to interface with standard logic-level signals (TTL or CMOS), which are commonly generated by microcontrollers.
Built-in Diodes: The L293 includes built-in diodes (also known as flyback diodes or snubber diodes) across the motor outputs. These diodes help protect the IC and other components from voltage spikes generated when the motor is turned off.
PIN CONFIGURATION OF L293:
Input Pins: The L293 has two input pins for each motor channel, labeled as IN1 and IN2 for channel 1, and IN3 and IN4 for channel 2. These pins control the direction of the motor by setting their logic levels.
Enable Pins: Each channel also has an enable input pin (EN1 and EN2). Applying a logic high to this pin enables the corresponding motor channel, allowing you to control the speed by varying the voltage level on this pin.
Output Pins: The motor outputs are labeled as OUT1 and OUT2 for channel 1, and OUT3 and OUT4 for channel 2. These pins are connected to the motor terminals and provide the current required for motor operation.
Motor Control with L293: To control the motion of a DC motor using the L293, you can follow these steps:
Set the direction of the motor by providing appropriate logic levels to the IN1 and IN2 pins (for channel 1) or IN3 and IN4 pins (for channel 2).
Apply a PWM signal to the EN1 or EN2 pin to control the motor's speed. Increasing the PWM duty cycle increases the effective voltage applied to the motor, thus increasing the speed.
Applications of L293: The L293 motor driver is commonly used in robotics, remote-controlled vehicles, automation systems, and various projects where precise control of motor direction and speed is required. It's especially useful when you need to interface low-power microcontroller outputs with higher-current motors, as it provides the necessary electrical isolation and protection.
ATMEGA32
AVR microcontrollers are known to be based on the advanced RISC architecture. ATmega32 is a low-power CMOS 8-bit microcontroller based on the AVR enhanced RISC architecture. With a cycle frequency of 1MHz, the AVR can execute one million instructions per second.
Main Features
32×8 general-purpose working registers
32K bytes of system self-programmable flash program memory
2K bytes of internal SRAM
1024 bytes of EEPROM
Available in 40-pin DIP, 44-pin QTFP, 44-pad QFN/MLF packages
32 programmable I/O lines
8-channel, 10-bit ADC
Two 8-bit timers/counters with independent prescaler and compare modes
One 16-bit timer/counter with independent prescaler, compare mode, and capture mode
Four PWM channels
System programming via on-chip bootloader
Programmable watchdog timer with independent on-chip oscillator
Programmable serial USART
Master/slave SPI serial interface
Special Features
Six sleep modes: idle, ADC noise reduction, power saving, power down, standby and extended standby
Internally calibrated RC oscillator
External and internal interrupt sources
Power-on reset and programmable undervoltage detection
All 32 registers are directly connected to the Arithmetic Logic Unit (ALU), allowing access to two independent registers in a single instruction executed in one clock cycle.
Power-down save function, all other chip functions will be disabled until the next external interrupt occurs. Asynchronous timer allows the user to maintain a timer in power-down mode while the rest of the device is in sleep mode.
ADC degradation mode stops the CPU and all I/O modules except the ADC and asynchronous timer. In standby mode, the rest of the device is in hibernation except for the crystal oscillator. The main oscillator and asynchronous timer continue to operate in extended standby mode.
HD44780 – LCD DISPLAY
|
Sr. No |
Pin No. |
Pin Name |
Pin Type |
Pin Description |
Pin Connection |
|
1 |
Pin 1 |
Ground |
Source Pin |
This is a ground pin of LCD |
Connected to the ground of the MCU/ Power source |
|
2 |
Pin 2 |
VCC |
Source Pin |
This is the supply voltage pin of LCD |
Connected to the supply pin of Power source |
|
3 |
Pin 3 |
V0/VEE |
Control Pin |
Adjusts the contrast of the LCD. |
Connected to a variable POT that can source 0-5V |
|
4 |
Pin 4 |
Register Select |
Control Pin |
Toggles between Command/Data Register |
Connected to a MCU pin and gets either 0 or 1. 0 -> Command Mode 1-> Data Mode |
|
5 |
Pin 5 |
Read/Write |
Control Pin |
Toggles the LCD between Read/Write Operation |
Connected to a MCU pin and gets either 0 or 1. 0 -> Write Operation 1-> Read Operation |
|
6 |
Pin 6 |
Enable |
Control Pin |
Must be held high to perform Read/Write Operation |
Connected to MCU and always held high. |
|
7 |
Pin 7-14 |
Data Bits (0-7) |
Data/Command Pin |
Pins used to send Command or data to the LCD. |
In 4-Wire Mode Only 4 pins (0-3) is connected to MCU In 8-Wire Mode All 8 pins(0-7) are connected to MCU |
|
8 |
Pin 15 |
LED Positive |
LED Pin |
Normal LED like operation to illuminate the LCD |
Connected to +5V |
|
9 |
Pin 16 |
LED Negative |
LED Pin |
Normal LED like operation to illuminate the LCD connected with GND. |
Connected to ground |
PROGRAM:
#include <avr/io.h> // AVR Input/Output header file
#define F_CPU 16000000ul //define the CPU frequency
#include "avr/interrupt.h" // AVR interrupt header file
#include <util/delay.h> //Delay header file
#define sw_direction (PIND&(1<<1)) //d1 pin is declared switch for direction control
#define sw_speedcontrol (PIND&(1<<2)) //d2 pin is declared as push button for speed control
#define sw (PINC&(1<<3)) //c3 pin is declared as switch for power on/off.
#define lcd_port PORTB //portB pins are for LCD data
#define RS_high PORTD|=(1<<5) //d5 pin is used for resistor select pin of LCD (high)
#define RS_low PORTD&=~(1<<5)
#define EN_high PORTD|=(1<<6) //d6 pin is used for enable pin of LCD
#define EN_low PORTD&=~(1<<6)
unsigned char i; // Creating a global variable
void lcd_cmd(char cmd) // This function is to use LCD in command Mode
{
lcd_port=(cmd&0xf0); //Masking 4 bits for 4 bit LCD mode//
RS_low; // RS is low in command mode
EN_low; // setting EN bit low
_delay_ms(1); // make delay
EN_high; // again setting high to same EN bit
lcd_port=((cmd<<4)&(0xf0));
RS_low; // RS is low in command mode
EN_low; // making EN pin of LCD low
_delay_ms(1); // make delay
EN_high; // again setting HIGH to same bit
}
void lcd_data(char data) //This function is to use LCD is data mode
{
lcd_port=(data &0xf0); //masking 4 bits for bit LCD mode//
RS_high; //RS is high in data mode
EN_low; // setting EN bit low
_delay_ms(1); // make delay
EN_high; // again set high
// reapting
lcd_port=((data<<4)&(0xf0));
RS_high;
EN_low;
_delay_ms(1);
EN_high;
}
void lcd_init()
{
lcd_cmd(0x28);
_delay_ms(5); //used for 4 bits LCD mode //
lcd_cmd(0x02);
_delay_ms(5); //return the cursor to home//
lcd_cmd(0x01);
_delay_ms(5); //clear screen //
lcd_cmd(0x0C);
_delay_ms(5); //display ON cursor OFF//
lcd_cmd(0x06);
_delay_ms(5); //increment cursor//
lcd_cmd(0x80);
_delay_ms(5); //force cursor to begin 1st line //
}
void cursorpos(char x, char y)
{
if ((x<1||x>2)||(y>16)||(y<1)) //condition will check the rows 2 and 16 columns
{
x=1; //x=rows = 1
y=1; //y=columns = 1
}
if (x==1)
{
lcd_cmd(0x7F+y); //{80-1=0B}for starting the first list hex decimal value(80){y we are the 7F+y means we need to start from that column
}
else
{
lcd_cmd(0xBF+y); //{0C-1=0B} for starting the second line, 2nd row
}
}
void lcd_out(char x, char y, char *str) //here we are checking which position we want to print
{
cursorpos(x,y); // setting at our desire location
while (*str) // printing string till
{
lcd_data(*str);
str++; // printing word by word
}
}
int main(void)
{
DDRB=0xFF; // declaring all 8 pins as output
DDRA|=((1<<0)|(1<<1)); // declaring A0 and A1 as output
DDRD&=~(1<<1); //declaring D1 pin as input
DDRD&=~(1<<2); //declaring D2 pin as input
DDRD|=(1<<5); //declaring D5 pin as output
DDRD|=(1<<6); //declaring D6 pin as output
DDRC&=~(1<<3); //declaring C3 pin as input
PORTD|=(1<<2); //enabling internal pull up resistor for D2
PORTC|=(1<<3); //enabling internal pull up resistor for D3
i=64; // initializing i=64 because we are running the motor with 25%duty cycle
TCCR0=0X61; // non inverting mode, PWM phase correct mode, no pre scalar
GICR=(1<<INT0); // using into for interrupt
MCUCR=0X02; // make interrupt negative edge triggered
sei(); //enabling interrupts
lcd_init(); // initialized LCD
while (1)
{
while(sw==0) //continue till switch is released
{
if (sw_direction==0) // if the direction control switch is released
{
PORTA=0b00000001; //0000 0001/0x01/respective 1A 2A pins to rotate in clockwise direction
lcd_out(1,1,"Motor ON-CLW"); // running clockwise direction
}
else
{
PORTA=0b00000010; //0000 0010/0x02/respective 1A 2A pins to rotate in anti-clockwise direction
lcd_out(1,1,"Motor ON-ACLW"); // running anti-clockwise direction
}
OCR0=i; // i increase in ISR, the corresponding value is to be loaded into OCR0
}
PORTA=0X00; //0000 0000// respective 1A 2A pins to stop condition
lcd_out(1,1,"Motor is_OFF");
}
}
ISR(INT0_vect) // interrupts
{
i=i+50; //as i increases the duty cycle also increases .i increases by 50
//in each push and the corresponding value is to be loaded into
//OCR0 in while loop
}