Sign In

Servo Working On This Program

YASH SHAH shahyash512@gmail.com United States

Hardware: Atmega 328p On Arduino Pro Mini Software: Atmel 7 Visual

the program does not work for me on my servo.I have connected pwm pin 5, ground and vcc for 3 lines of servo which is accurate.But the servo just does a small(1 degree or so) actuation.I am using tg9e servo.

Images

Program

#define F_CPU 16000000
#define ARDUINO 100
#include <avr/wdt.h>

#define soft_reset() 


//#include "Arduino.h"
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)
void wdt_init(void) __attribute__((naked)) __attribute__((section(".init3")));

#define sbi(var, mask)   ((var) |= (uint8_t)(1 << mask))  	// Makes High
#define cbi(var, mask)   ((var) &= (uint8_t)~(1 << mask))	// Makes Low

void runservo (int, bool);
void wait();
//Simple Wait Function
void Wait()
{
	uint8_t i;
	for(i=0;i<50;i++)
	{
		_delay_loop_2(0);
		_delay_loop_2(0);
		_delay_loop_2(0);
	}

}


int main()
{
	//Configure TIMER1
	TCCR1A|=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);        //NON Inverted PWM
	TCCR1B|=(1<<WGM13)|(1<<WGM12)|(1<<CS11)|(1<<CS10); //PRESCALER=64 MODE 14(FAST PWM)

	ICR1=4999;  //fPWM=50Hz (Period = 20ms Standard).

	//DDRD|=(1<<PORTD5);   //PWM Pins as Out
	DDRD|=(1<<PD5); 
	
	runservo (90, true);
	while(1)
	//for(int i = 0; i < 10000; i++)
	{

		OCR1A=97;   //0 degree
		Wait();

		//OCR1A=316;  //90 degree
		//Wait();

		//OCR1A=425;  //135 degree

		//Wait();

		//OCR1A=535;  //180 degree
		//Wait();
	
	}
	
	return (0);
}

void runservo(int servodeg,bool servodir)
{
	float pulsewidth=0;
	if(!servodir)
	{
		pulsewidth=316-(2.433*servodeg);
	}
	else
	{
		pulsewidth=316+(2.433*servodeg);
	}
	OCR1A=pulsewidth;
	_delay_ms(3000);
	OCR1A=316;
}
void wdt_init(void)
{
	MCUSR = 0;
	wdt_disable();

	return;
}

Replies

R Avinash
2016-10-11 07:27:59
This is NOT the way to program Arduino! Use the servo library of Arduino.

Which model servo you are using?
How you are feeding 5v to the servo?
Is that 5v supply have enough current to servo.
On which pin of the ATmega328 you have connected the servo?
R YASH SHAH
2016-10-12 04:31:22

the Atmel program does not work for me on my servo.I have connected pwm pin 9, ground and vcc for 3 lines of servo which should be accurate.But the servo just does 120 degree actuation.I am using tg9e model servo.I am using 5v power from adruino pro mini board.The power is enough for running.I am trying to actuate the servo for specified degree given by the user and retrun to original position.The program is:
#define F_CPU 16000000
#define ARDUINO 100
#include <avr/wdt.h>
#define soft_reset() 

//#include "Arduino.h"
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)
void wdt_init(void) __attribute__((naked)) __attribute__((section(".init3")));

#define sbi(var, mask)   ((var) |= (uint8_t)(1 << mask))  	// Makes High
#define cbi(var, mask)   ((var) &= (uint8_t)~(1 << mask))	// Makes Low

void runservo (int, bool);
void wait();
//Simple Wait Function
void Wait()
{
	uint8_t i;
	for(i=0;i<50;i++)
	{
		_delay_loop_2(0);
		_delay_loop_2(0);
		_delay_loop_2(0);
	}

}

int main()
{
	//Configure TIMER1
	TCCR1A|=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);        //NON Inverted PWM
	TCCR1B|=(1<<WGM13)|(1<<WGM12)|(1<<CS11)|(1<<CS10); //PRESCALER=64 MODE 14(FAST PWM)

	ICR1=4999;  //fPWM=50Hz (Period = 20ms Standard).

	//DDRD|=(1<<PORTD5);   //PWM Pins as Out
	DDRD|=(1<<PD4)|(1<<PD5); 
	runservo (90, true);
	
	//while(1)
	for(int i = 0; i < 20; i++)
	{

		OCR1A=180;   //0 degree
		Wait();

		//OCR1A=316;  //90 degree
		//Wait();

		//OCR1A=425;  //135 degree

		//Wait();

		//OCR1A=535;  //180 degree
		//Wait();
	
	
	//for(i=90;i>0;i--)
	
		//OCR1A=415; 
		//Wait();
	
	}
	
	
	return (0);
}

void runservo(int servodeg,bool servodir)
{
	float pulsewidth=0;
	if(!servodir)
	{
		pulsewidth=316-(2.433*servodeg);
	}
	else
	{
		pulsewidth=316+(2.433*servodeg);
	}
	OCR1A=pulsewidth;
	_delay_ms(1000);
	OCR1A=415;
}
void wdt_init(void)
{
	MCUSR = 0;
	wdt_disable();

	return;
}
R Avinash Gupta
2016-10-12 05:04:21
First of all the 5v voltage regulator of Arduino mini is NOT ENOUGH TO DRIVE A servo motor! You need to use an external voltage regulator.
R YASH SHAH
2016-10-12 07:16:51
Thank you for your reply.Well to let you know,the servo is rotating 180 degree so I don't think power should be an issue.Even i made sure by looking the datasheet.My problem is running the code as I atmel is new environment for me.So basically I am a learner more from hardware side.Can you help me with the code?
R Avinash
2016-10-12 11:29:54
Not unless you can arrange for a voltage regulator that can withstand 1A current.
R YASH SHAH
2016-10-13 08:51:45
I am not able to run the servo anticlkwise.I tried couple of ways but it gives random values though my clockwise is working perfect.Just give your attention to loop and if statement.That is only going to help it work for anticlockwise but I don't know how.My code is:

#define F_CPU 16000000L
#define ARDUINO 100
#include

#include
#include
#include
#include
#include
#include
#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)
void wdt_init(void) __attribute__((naked)) __attribute__((section(".init3")));


void runservo (int, bool);
void wait();
//Simple Wait Function
void Wait()
{
_delay_ms(1000);
}


int main()
{
//Configure TIMER1
TCCR1A|=(1< TCCR1B|=(1<
ICR1=4999; //fPWM=50Hz (Period = 20ms Standard).

//DDRD|=(1< DDRB|=(1<

while(1)
{
runservo (45, true);
//OCR1A=316; //135 degree
//Wait();
OCR1A=535; //0 degree
Wait();
//OCR1A=535; //90 degree
//Wait();
//OCR1A=316; //135 degree
//Wait();
//OCR1A=535; //180 degree
//Wait();
break;
}

}

void runservo(int servodeg,bool servodir)
{
float pulsewidth=0;
if(!servodir)
{
pulsewidth=535-(2.433*servodeg);
}
else
{
pulsewidth=535+(2.433*servodeg);

}

OCR1A=pulsewidth;
_delay_ms(1000);
//OCR1A=535;
}
void wdt_init(void)
{
MCUSR = 0;
wdt_disable();

return;
}

Post a reply to YASH SHAH

Think you can help YASH SHAH ? Then post your thoughts that might help YASH SHAH. You will earn a lot of reputation in the technical community.

If your program is in multiple files, then ZIP the entire folder and attach below

Images and Screenshots

These helps other better understand your suggestion.