# Controlling Servo motor with ATmega328p microcontroller

For a while, I've been working with Arduino, but about a month ago, I switched over to using solely the ATmega328-PU microcontroller. I use the Atmel Studio IDE. So far, I have successfully blinked an LED, used programs involving buttons/switches, and used the Fast PWM function to dim an LED. Currently, I am attempting to drive a servo motor using Fast PWM, but have had little success. I have not found any source codes that work for the specific needs of my microcontroller, and I don't really have the ability to change around all the parameters, as I am still learning.

The below is the code which I have developed. It does not work, but I was hoping that someone may be able to fix it up. The servo moves in one direction, but does not stop, and just ends up grinding the motor shaft gear, indicating this could be an issue with the wave length. An additional question: is there an equation to convert degree to PWM wavelength? Thanks!

Microcontroller specifics:

• Model - ATmega328-PU
• 16,000,000 hz crystal
• 28 pins
• Using 8 bit timer

Servo:

• Servo model: Tower Pro SG90
• Servo Frequency 50 hz (is this standard?)

Code:

#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{

DDRD |= (1 << DDD6);
// PD6 is now an output - corresponds to OCR0A

TCCR0A |= (1 << COM0A1);
// set non-inverting mode

TCCR0A |= (1 << WGM01) | (1 << WGM00);
// set fast PWM Mode

ICR1 = 39999;

OCR0A = ICR1 - 2000;
// gives servo motor position

TCCR0B |= (1 << CS01);
// set prescaler to 8 and starts PWM

while(1)
{
}
}

• Formally, you should read the data sheet. More practically, write an Arduino sketch that commands the desired position, and then reads back out and prints the hardware register values you've been trying to set. Then you can use those in your own AVR C program as you attempted. – Chris Stratton Jan 12 '18 at 6:06
• @ChrisStratton Thank you! I did check out the Servo motor's datasheet, but unfortunately, it seemed to be more like an advertisement and carried very little useful data for non-Arduino users. – Mischief City Jan 12 '18 at 6:15
• I meant the ATmega data sheet. It's admittedly complicated, but the information is there. Watching what the Arduiono sketches are actually doing with the registers and then looking those up in the data sheet may be more informative than tackling it cold. Also there have been a number of writeups along those lines online that you can easily find. – Chris Stratton Jan 12 '18 at 6:17