Controlling Servomotors With Microcontrollers
Controlling Servomotors With Microcontrollers
IENF641: Microcontrollers
Chapter 5:
white = signal
red = 5V
black = Gnd
Pulse Width Modulation (PWM)
One simple and easy way to control the position of a motor is
to regulate the amount of voltage across its terminals and
this can be achieved using “Pulse Width Modulation”.
2.0 ms
1.0 ms 2.0 ms 1.5 ms
Vdd (5 V)
Example:
DDRC = 0x01; //Makes PC0 output pin Vss (0 V)
PORTC = 0x00;
while(1)
Vin
{
//Rotate Motor to 180 degree
PC0 White
PORTC = 0x01; P14
Red
_delay_us(2000); Servo
Black
PORTC = 0x00;
_delay_ms(20);
} Vss
Programming Servo Control
Programming Servo Control
DDRC = 0x01; //Makes PC0 output pin
PORTC = 0x00;
What does while(1)
this code do? {
PORTC = 0x01;
_delay_us(1500);
PORTC = 0x00;
_delay_ms(20);
}
Programming Servo Control
.
PORTC = 0x00;
_delay_ms(20); Vss (0 V)
20 ms
PORTC = 0x00;
_delay_ms(20); Vss (0 V)
20 ms
_delay_ms(20); Vss (0 V)
20 ms
What does this
code do?
#define F_CPU 4000000UL // 4 MHz clock speed
#include <avr/io.h>
#include <util/delay.h>
int main(void) {
DDRC = 0x01; //Makes PC0 output pin
PORTC = 0x00;
for(int i=1;i<50;i++) {
PORTC = 0x01; //Generate a pulse of 1.0 ms on
_delay_us(1000);
PORTC = 0x00; // Followed by 20 ms off
_delay_ms(20);
}
}
#define F_CPU 4000000UL // 4 MHz clock speed What does this
#include <avr/io.h> code do?
#include <util/delay.h>
int main(void) {
DDRC = 0x01; //Makes PC0 output pin
PORTC = 0x00;
while(1) {
for(int i=1;i<50;i++) {
PORTC = 0x01; //Generate a pulse of 1.2 ms on
_delay_us(1200);
PORTC = 0x00; // Followed by 20 ms off
_delay_ms(20);
}
for(int i=1;i<50;i++) {
PORTC = 0x01; // 1.8 ms on
_delay_us(1800);
PORTC = 0x00; // followed by 20 ms off
_delay_ms(20);
}
}
}
Converting Position to Motion
You can program the servo to change position at different
rates. By changing position at different rates, you will cause
your servo horn to rotate at different speeds.
You can use this technique to make the servo control motion
instead of position.
DDRC = 0x01; //Makes PC0 output pin DDRC = 0x01; //Makes PC0 output pin
PORTC = 0x00; PORTC = 0x00;
while(1) while(1)
{ {
//Rotate Motor to 180 degree //Rotate Motor to 180 degree
PORTC = 0x01; PORTC = 0x01;
_delay_us(2000); _delay_us(2000);
PORTC = 0x00; PORTC = 0x00;
_delay_ms(1000); _delay_ms(20);
} }
With AVR ATmega16 microcontroller, a circuit has servomotor
(PC0), two pushbuttons and LED. Write a program as:
when PB2 pushbutton pressed the servomotor will rotate
clockwise and the LED (PD0) will be on.
when PB3 pushbutton pressed the servomotor will rotate
counterclockwise and the LED will be off.
Vin PD0
P14
470
PC0 White LED
P14
Red
Servo
Black Vss
Vss Vss
With AVR ATmega16 microcontroller, a circuit has servomotor
(PC0), two pushbuttons and two LEDs. Write a program as:
when PB2 pushbutton pressed the servomotor will rotate
clockwise and green LED (PD0) will be on.
when PB3 pushbutton pressed the servomotor will rotate
counterclockwise and red LED (PD1) will be on.
Vin
PC0
P14 White
Red
Servo
Black
PD1
P15
470
PD0
P14
Vss Vdd Vdd 470
P4 LED LED
PB3
220 Vss Vss
PB2
P3
220
10 k 10 k
Vss Vss
Example
Using AVR ATmega16 microcontroller, write a code for a
servomotor. The motor must be make exactly 8 steps on
one complete rotation. Movement should be stepped and
not smooth.
Example
Using AVR microcontroller, write a code for a servomotor.
The servomotor (PC0) is controlled via three pushbuttons of
the User Interface module.
By pressing down S1 (PD0) the servomotor moves one step
to the right.
By pressing down S3 (PD3) , the servo motor moves one
step to the left and S2 (PD2) makes the servo motor to
move to the initial (middle) position.
The position of the servomotor is displayed live on the 7-
segment display (port B) as each number corresponds to
10 degrees of the turn: middle position equals 5.
Example
The encoder (connected to PD0, PD1 and PD2) is a sensor
that notifies the driver of the speed and position of the
motor. The encoders used with the servomotor (PC0) as
follows: