In many applications, it is important to control the speed of DC motor where precision and protection are essence. Here we will use a technique called PWM (pulse width modulation) to control the speed of DC motor.
We can achieve speed control of DC motor using mechanical or electrical techniques but they require large size hardware to implement but Microcontroller based system provides easy way to control the speed of DC motor.
Earlier, we have already seen how to control the speed of DC motor using PWM without Microcontroller. Here we do the same experiment by using a microcontroller.
For that purpose, here we will use ATmega8 controller to produce PWM wave. By varying the width of this PWM wave, we can control the speed of DC motor. In ATmega8 controller, timer1 and timer2 have PWM mode. In this article we will see how to control the speed of DC motor using timer2 PWM mode.
PWM Based DC Motor Speed Control using Microcontroller Circuit Principle:
The heart of this project is ATmega8 controller. These controllers consist of 2 PWM modes. Now we will see how to generate PWM wave using timer2 PWM mode.
Before writing the program to the PWM mode we need to know the register description of all the registers that are used for PWM mode.
TCCR2 (Timer Counter Control Register):
In Timer 2, we have different modes like CTC mode, normal mode, phase correct PWM mode, Fast PWM mode, etc. Among all of these modes, we have to select Fast PWM mode.
From the above figure, it is clear that
For Fast PWM mode,
To select PWM mode
TCCR2 |= (1<<WGM21)| (1<<WGM20);
Again in PWM mode we have two modes one INVERTING and other is NON – INVERTING.
- To select non-inverting mode,
- COM21=1 COM20=0
- TCCR2 |= (1<<COM21);
- To select inverting mode,
- COM21=1, COM20=1
- TCCR2 |= (1<<COM21)| (1<<COM20);
After that we have to select prescaler value.
CS22 CS21 CS20 Description
0 0 0 No clock source
0 0 1 clk/1
0 1 0 clk/8
0 1 1 clk/32
1 0 0 clk/64
1 0 1 clk/128
1 1 0 clk/256
1 1 0 clk/1024
- To set the prescaler to 8,
CS22=0 CS21=1 CS20=0
OCR2 (Output Compare Register):
OCR2 register contains an 8 bit value that is continuously compared with counter value.
void pwm ( )
TCCR2 |= (1 << COM21); // set none-inverting mode
TCCR2 |= (1 << WGM21) | (1 << WGM20) // set fast PWM Mode
TCCR2 |= (1 << CS21); // set prescaler to 8 and starts PWM
The above program runs the DC motor which is connected to PB3 with half speed.
If you want to vary the speed vary the OCR2 register value.
Also Read the Related Post – How Stepper Motor Driver Circuit Works using 8051 Microcontroller?
Circuit Diagram of PWM Based DC Motor Speed Control using Microcontroller:
- Atmega8 PCB board
- Atmega8 controller
- DC motor
- Serial cable
- 12V battery or adaptor
- Connecting wires
PWM based DC Motor Speed Control using Microcontroller Circuit Design:
The circuit consists of one Atmega8 controller, 2 pull down configuration switches and one DC motor.
Generally we can interface switch to the micro controller in two configurations, one pull up configuration and other is pull down configuration.
Pull up configuration: In pull up configuration initially microcontroller pin is pulled to LOGIC 1. When button is pressed microcontroller pin receives LOGIC 0
Pull down configuration: In pull down configuration initially microcontroller pin pulled to LOGIC 0. When button is pressed controller pin receives LOGIC 1.
In our circuit we are using pull down configuration so we need to check for logic 1 in order to know weather the button is pressed or not.
DC Motor: motor has two terminals. We have to connect one of the motor terminal to the controller pin and other to the ground.
In this project motor runs with full speed when button sw1 is pressed and motor runs with half speed when sw2 button is pressed.
How to Operate PWM based DC Motor Speed Control Circuit using Microcontroller?
- Connect 12V battery or adaptor to the development board.
- Switch on the supply.
- Keep the programming switch in prgm mode.
- Burn hex file to the atmega8 controller with the help of serial cable.
- Now switch off the supply.
- Connect PC0, PC1 pins of controller to the switches.
- Connect PB3 pin to the DC motor.
- Now switch on the supply and press switch sw1 and observe motor. It runs with full speed.
- If you press switch sw2 motor runs with half speed.
- Switch off the supply.
PWM Based DC Motor Speed Control System Advantages:
- Using this PWM method, we can save the power.
PWM Based DC Motor Speed Control System Applications:
- Used in industries to control the speed of motors.
- Used in shopping malls.
- We can use this concept to control the light intensity.