Schematic and board layout potmeter function

servo motor potmeter sch

C-Code for potmeter function

// program name: servo motor controller with potmeter
// date: 08-mrt-2017
// author: www.avrprojects.net
// target device : ATMEGA328

#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#define F_CPU = 16000000;

#define FOSC 16000000 // clock speed
#define BAUD 9600 // baud rate
#define MYUBRR FOSC/16/BAUD-1


// this function read the value of ADC channel 0
int readADC(char channel)
{
//adc init
ADMUX = channel; // set ADC channel
ADCSRA = 0b10000111; //set ADEN, precale by 128
ADCSRA |= 1<<ADSC; //start conversion;
while (ADCSRA&(1<<ADSC)); //wait conversion end
int ADC_temp = ADCL; // read out ADCL register
ADC_temp += (ADCH << 8); // read out ADCH register
return ADC_temp;
}

int main (void)

{
DDRB = 0xFF; // set portb for output
PORTB = 0;

while(1)
{
// potmeter function
int pos = readADC(0);
pos = (pos * 2) + 500;

PORTB = 0x00000001;
_delay_us (pos);
PORTB = 0x00000000;
_delay_us (20000 - pos);
}
}