0% found this document useful (0 votes)
87 views28 pages

Controlling Servomotors With Microcontrollers

Here is the program: #define F_CPU 1000000UL #include <avr/io.h> #include <util/delay.h> int main(void) { DDRC = 0x01; //PC0 output DDRD = 0x01; //PD0 output PORTD = 0x00; while(1) { if(PINC & 0x04) //PB2 pressed { PORTC = 0x01; _delay_us(1500); PORTC = 0x00; _delay_ms(20); PORTD = 0x01; //LED on } if(PINC

Uploaded by

Abdulla Ashoor
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)
87 views28 pages

Controlling Servomotors With Microcontrollers

Here is the program: #define F_CPU 1000000UL #include <avr/io.h> #include <util/delay.h> int main(void) { DDRC = 0x01; //PC0 output DDRD = 0x01; //PD0 output PORTD = 0x00; while(1) { if(PINC & 0x04) //PB2 pressed { PORTC = 0x01; _delay_us(1500); PORTC = 0x00; _delay_ms(20); PORTD = 0x01; //LED on } if(PINC

Uploaded by

Abdulla Ashoor
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/ 28

College of Engineering

IENF641: Microcontrollers

Chapter 5:

Controlling Servomotors with


Microcontrollers
Edited by:
Dr. Essam Alnatsheh
efalnatsheh@amaiu.edu.bh
Presentation Index
 Microcontroller Motion

 Connecting and Testing the Servo

 Programming Servo Control

 Controlling position with Microcontroller

 Converting Position to Motion

 Servo Control with Pushbuttons

 Standard and Continuous Rotation Servos


Microcontroller Motion
Microcontrollers control the motion of many things in our
daily lives:
 Printer head movement.
 DVD and VCR mechanisms.
 Grocery store automatic doors.
 Robotic movement.

Instead of being simply ON-OFF, many of these motion


devices require very fast pulses of signals for position
control or movement.
Motors

Examples of motors and motion devices:


 DC Motors
 AC Motors
 Stepper Motors
 Servos Motors
Overview

• A servo motor is only capable


of rotating 180 degrees
• It is given an angle to proceed
to, as apposed to a speed or
direction
What is a servo motor

• Servo motors consist of :


1. gear reduction
2. position sensor
3. an electronic circuit that
controls the motor's
operation
Inside a Servo
Feed-back loop
Open- • A servo motor is a
closed-loop system
loop
• It will continuously
attempt to reach the
exact angle
prescribed
• The motor is
Closed- constantly taking
loop note of its location
and deciding how to
best reach its target
angle
Connecting and Testing the Servo

A servo connects to a microcontroller using three wires

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”.

 As its name suggests, pulse width modulation works by


driving the motor with a series of “ON-OFF” pulses and
varying the duty cycle of the pulses while keeping the
frequency constant.
Pulse Width Modulation (PWM)
 The power applied to the motor can be controlled by
varying the width of these applied pulses and thereby
varying the average DC voltage applied to the motors
terminals.
 By changing the timing of these pulses the speed of the
motor can be controlled, ie, the longer the pulse is “ON”,
the faster the motor will rotate and likewise, the shorter
the pulse is “ON” the slower the motor will rotate.
Control signal is a pulse train

The third pin of the servo


connector carries the PWM
signal.

This PWM signal is a pulse train.

Pulse frequency is fixed


Typical: 20 ms (50 Hz)

Pulse width determines position


Typical: 1ms to 2 ms
Pulse Width Modulation (PWM)

With PWM, angle is determined by applying pulses of variable


width.
Control
• The I/O pin of microcontroller
sends a 5v pulse to the motor
• The angle is determined by
the duration of the pulse
• A 1.5 millisecond pulse
means 90 degree position
• Shorter than 1.5 ms means
closer to 0°. Longer means
closer to 180°
Control signal
Programming Servo Control
.

standard servo standard servo standard servo


www.parallax.com www.parallax.com www.parallax.com

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
.

//Rotate Motor to 180 degree 2.0 ms 2.0 ms


PORTC = 0x01; Vdd (5 V)
_delay_us(2000); standard servo
www.parallax.com

PORTC = 0x00;
_delay_ms(20); Vss (0 V)
20 ms

//Rotate Motor to 0 degree


1.0 ms 1.0 ms
PORTC = 0x01;
Vdd (5 V)
_delay_us(1000); standard servo
www.parallax.com

PORTC = 0x00;
_delay_ms(20); Vss (0 V)
20 ms

//Rotate Motor to 90 degree


PORTC = 0x01; 1.5 ms 1.5 ms
_delay_us(1500); Vdd (5 V)
standard servo

PORTC = 0x00; www.parallax.com

_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

Vdd Vin Vss

Vss Vdd Vdd X3


+
P15
P14
P13
P4
PB3 P12
White
P11
Red
220  P10
P9 Black
P8
P7
PB2
P3 P6
P5
220  P4
P3
10 k 10 k P2
standard servo
P1
P0 www.parallax.com
X2

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:

Write an AVR program to implement the above system.


References 

"What's a Microcontroller ?"


By Andy Lindsay
Parallax, Inc

Chapter 4: Controlling Motion

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy