Manually control servo with ATtiny85

If you find yourself wanting to control a servo with an ATtiny85, you will find very few libraries available.  Those that are available, I was unable to get to work properly for full 180 degree rotation of my SG92R servo.  Here is what I did that worked great:

int servoControlMaster = 2400; // SG92R uses 600 for 0 degrees, 2400 for 180 degrees, move in increments of 5
int servoControlRemain = 17600; // servoControlMaster + servoControlRemain must equal 20000 Microseconds

int servoChange = 30; // Change in microseconds
int servoChangeCalc = 20000-servoControlMaster; // calculate remainder of 20000 microseconds

void setup() {

}

void loop() {

// manually send position to servo
for(int i = 0; i<20; ++i) {
digitalWrite(2, HIGH);
delayMicroseconds(servoControlMaster);
digitalWrite(2, LOW);
delayMicroseconds(servoControlRemain);
}

delay (1000);

if (servoControlMaster > (600)) {
servoControlMaster = servoControlMaster-servoChange;
servoControlRemain = servoChangeCalc;
} else {
// reset to beginning position
servoControlMaster = 2400;
servoControlRemain = 17600;
}

}

This entry was posted in Circuits and Electronics. Bookmark the permalink.

Comments are closed.