Controlling a Hobby Servo using an AVR Microcontroller
We will control a hobby servo using an AVR microcontroller by outputting a PWM (Pulse Width Modulation) signal from the
microcontroller to the servo. We will be using much of the information from the previous
video and information on the fundamentals of PWM. We will also learn some new jargon that is specific to the AVR
First, we will need to select a timer and the correct WGM (Waveform Generation Mode) within this timer that will be
appropraite for the servo. Next, we need to select the PWM period that will work with the servo. The servo used in the
video accepts a period 20 miliseconds long and we will take advantage of the ICR1 register to create this period. To do this,
we will need to determine a prescaler so the timer uses the microcontroller's clock source properly, we will need
to set the PWM to be in the correct mode, inverted or non-inverted, where the pulse happens at the beginning of the period,
or at the end of the period and finally, we will need to derermine which OCR (Output Capture Register) we will use, 1A or 1B.
Introduction to PWM for the AVR (Atmel) Microcontrollers
PWM stands for Pulse Width Modulation and is the method to produce variable voltages using digital means. Typically,
variable voltages come from analog circuits, and digital circuits produce only two voltages, the high (5v, 3.3v, 1.8v, etc.)
or low (0v). So how it is possible that digital circuits can produce avoltage that is between the high and the low voltages?
If you bring a digital signal up and down, in a consistent manner, you will get a proportion of the voltage between the high
and low voltage. Imagine if a digital signal was pulsed high (5v) and low (0v) evenly, say the signal was in the high state
for 1 microsecond and in the low state for 1 microsecond, add a capacitor to smooth the signal, the voltage would measure 2.5
volts. Now, change the high voltage in the high state for 9 microseconds and in the low state for 1 microseconds, the voltage
would measure 90% of 5 volts, or 5v x .9 = 4.5 volts. The 90% is significant because the duty cycle is represented as a
percentage (%). The applications associated with PWM could be: thecontrol of motors, sound output, dimming LEDs, and producing
approximated analog waveforms. Read on...
Joe Z.'s Unique Hello World Project
Everyone knows the first program is called the "Hello World" project. The Hello
World program is intended for the newbie programmer get their feet wet and be as
simple as possible. This is why, in the embedded realm, the Hello World programs
is to simply turn on an LED. Joe didn't want to settle for the typical Light up,
or blink the LED. He wanted to literally make a single
LED communicate. How would a single LED communicate? Using morse code, obviously.
Making a Library for the Button
The button code with the accompanying software debouncing is a lot to put into a
main routine. It's best that these sorts of code become tucked away in a library
and re-used in all of the programs that you will be implementing puch buttons in
your circuits. The new library
that we put together include features of using multiple button switches
and varying the debouncing threshold.
Intro to Servos
The full explanation and narrative of the introduction to servos and understanding
of torque is being added.
Additions to the List of Future Tutorials
From an email request, I have added three new tutorials to the list: From Breadboard
to PCB Prototype, Using a CNC Machine to create the PCB and Using Transistors with
Microcontrollers: A Beginner's Guide (Video Series)
to the video series that I produced on how to build a CNC machine from the ground
up (published at BuildYourCNC.com), I will be producing a similar set of videos
on the use of the Atmel AVR Atmega32 Microcontroller. The video serieswill start as an introduction, but will then switch rapidly
to example-based projects for the microcontroller.
I have been enthralled with microcontrollers and robotics, and have embarked on
a specific endeavor to create a bipedal robot balancing on point feet--that is,
balancing in all directions without falling. This has yet to be accomplished, but
has been tried many times over. There is one exception (PETMAN from Boston Dynamics)that has been successful, using
a human scale robot with human scale feet. The really cool thing with the PETMAN
is that they have achieved great locomotion with a human-like walking gait.
So, as I wander towards my endeavor, I will be explaining the process from scratch
on the use, programming and construction of circuits fusing the AVR Atmega32 microcontroller;
hopefully overcoming many challenges along the way.
Most Recent Video Clips:
If you find these video clips usefull, consider creating your own. They are searchable
and very focused. It's a great way to get the fluff out!
SPI Communication (Chip to Chip)
Demonstration of the funtamental signal requirements and timings and then show examples of circuit and code to establish
PID and Encoder Control for DC Motor (Servo Basics)
Explain the fundamentals of encoders and how encoders can determine position for a greater mechanical system. This typically
requires PID (Proportional Integral Derivative) closed loop control and a thorough detailed explanation of this type of
control will be investigated. Various encoder types will be examined along with various types of motors and mechanical
advantage (gears, pulleys, chain, etc.). This will require some prerequisites in signal theory, which I will explain.
Introduction to Servos (video complete, official page soon to come)
I will do a run-down of the different types of servos out there and how they function.
I will also get into the details of closed-loop control.
Servo Control Using the Standard PWM Output (video complete, official page soon to
PWM is the method used to control non-digital hobby servomechanisms. I will show
how to simply control the servo using the 16-bit timer and the PWM channels.
Servo Control Using the Output Pins (video complete, official page coming soon)
PWM will still be the method to control the hobby servo, but we may want the microcontroller
to drive more servos than the number of standard PWM channels will allow. Therefore
we will investigate a method that uses standard pins as PWM output to control many
Servo Control For Servos That Use Encoders
We will investigate the programming necessary to create a closed-loop system that
reads an encoder and positions a motor to create an overall servomechanism.
Controlling High AC Voltage Devices Using Relays
Do you want to control a household device? Using a relay and a spare output pin
on your microcontroller can do the trick. In this tutorial, we will control the
stuff that would be plugged into the wall outlet.
Controlling Higher DC Voltage Devices Using Mosfets
Enough of this 5v limit. With Mosfets, you can send higher DC voltages to devices
that would function better above the 5v limit that comes out of the microcontroller.
LED Control Using PWM
This tutorial is a precursor to motor control using PWM. We will control the brightness
of an LED and make it fade in and out softly.
LCD 4-Bit Mode
Is your LCD taking up too many pins? In 4-bit mode, the LCD will only need 4 data
lines--so your microcontroller can free-up 4 of the pins for other uses.
Introduction to Serial Communication
The basic fundamentals of serial communication will be explained in this tutorial.
Configuring and Communicating with the USART
Universal Synchronous Asynchronous Receiver Transmitter is the serial communication
function in the microcontroller. We will communicate to and from the computer via
RS-232 (with the help of the Max232 chip to adjust voltage levels), and we will
also communicate with a digital servo with a single wire to serve as both TX (Transmit)
and RX (Receive).
Motor Control Using PWM
This tutorial will delve into motor control. Using PWM, we will be able to increase
and decrease the speed of a motor and even change motor direction.
From Breadboard to PCB Prototype
This tutorial will explain the process necessary to create a schematic in Cadsoft
Eagle, layout the PCB (Printed Circuit Board) and build the prototype.
Using a CNC Machine to create the PCB
If you have access to a CNC Machine, PCB routing is a great way to create chemical
Using Transistors with Microcontrollers
Learn how to use transistors in your microcontroller projects.
Control of a Small Low-Current Stepping Motor
In this tutorial, a small low-current stepping motor will be controlled by the pins
of the microcontroller.
Control of Higher Current and Higher Torque Stepping Motors
A larger stepping motor will be controlled using Mosfets and a separate higher voltage
source; however the microcontroller will still be providing the control logic to
Control of Stepping Motors Using A Driver
As an alternative to Mosfets, I will select various driver chips on the market today
and use those to provide the high-powered driving current, again under the direction
of the microcontroller.
Control of Stepping Motors Using the Translator/Driver Combination
This is where things get interesting with stepping motor control, since the microcontroller
now becomes a processor for much broader control--with only the need to send out
step pulses and a high/low signal for direction control.
Using Alternative Clocks
Alternative clocks such as crystals, RC circuits and others will be explained and
connected to the microcontroller. We will use these devices to adjust the frequency
of the microcontroller.
Interfacing a Shift Register to Expand the Number of Output Pins
Imagine what you could do with many output pins! Do I hear...LED array? We can also
investigate using this device to communicate to the LCD with only a few wires.
Read a PIR Sensor
PIR (Passive Infra-Red) sensors will be explained and we learn to control these
devices with the microcontroller.
Read an Ambient Light Sensor
We will connect an ambient light sensor to the microcontroller, and check its reading
on the LCD.
Introduction to Interfacing with I2C
Inter-Integrated Circuit is a type of communication that is common with many sensors
and devices. I will probably start with an accelerometer that communicates using
Read an IR Distance Sensor
An Infra-Red distance sensor will be connected to the microcontroller, and the information
will then be displayed to the LCD.
UltraSonic distance Sensor Reading
An ultrasonic distance sensor will be connected to the microcontroller, and the
distance displayed to the LCD.
I will do a survey of the wireless options that can be used with microcontroller
and explain each of them.
Radio Frequency Wireless Communication
We will communicate with another device using wireless radio frequency communication.
Communication using Xbee and Zigbee Devices
We will investigate the use of Xbee and Zigbee products for wireless network communications.
A GPS module will be interfaced to the microcontroller. The information will be
parsed and delivered to the LCD. We may do some other interesting things with GPS
Other Microcontrollers, such as the ARM and PIC will get tutorials of their own.
When I begin with these tutorials, I will start another list.