1

Тема: SERVO MOTOR SG90

Основная статья - http://rcl-radio.ru/?p=132542


int servoPin = 9; // сигнальный провод от серво на порт 9
int val;

void setup()
{
  pinMode(servoPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("Servo is ready");
}

void loop(){
      val = 0;
      for (int i = 0; i <= 50; i++){servoPulse(servoPin, val);}
      delay(1000);
      val = 90;
      for (int i = 0; i <= 50; i++){servoPulse(servoPin, val);}
      delay(1000);
      val = 180;
      for (int i = 0; i <= 50; i++){servoPulse(servoPin, val);}
      delay(1000);
      val = 90;
      for (int i = 0; i <= 50; i++){servoPulse(servoPin, val);}
      delay(1000);
}

// define a servo pulse function
void servoPulse(int pin, int angle){
  int pulseWidth = map(angle, 0, 180 , 544, 2480); 
  Serial.println(pulseWidth);
  digitalWrite(pin, HIGH); // set the level of servo pin as high
  delayMicroseconds(pulseWidth); // delay microsecond of pulse width
  digitalWrite(pin, LOW); // set the level of servo pin as low
  delayMicroseconds(20000 - pulseWidth);
}
/* Sweep
 by BARRAGAN <http://barraganstudio.com>
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {

    myservo.write(0);    
    delay(1000);  
    myservo.write(90);    
    delay(1000); 
    myservo.write(180);    
    delay(1000);  
    myservo.write(90);    
    delay(1000);     
  
 
}

2

Re: SERVO MOTOR SG90

/* Sweep
 by BARRAGAN <http://barraganstudio.com>
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(30);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(30);                       // waits 15ms for the servo to reach the position
  }
}
int servoPin = 9; // сигнальный провод от серво на порт 9

void setup()
{
  pinMode(servoPin, OUTPUT);
  Serial.begin(9600);
}

void loop(){

for(int i=0;i<=180;i++){
      servoPulse(servoPin, i);
      delay(30);
}
delay(100); 
for(int i=180;i>=0;i--){
      servoPulse(servoPin, i);
      delay(30);
}    
  
}

// define a servo pulse function
void servoPulse(int pin, int angle){
  int timess;
 // for (int i_ = 0; i_ <= 5; i_++) {
  timess = map(angle, 0,180,  544, 2400); 
  digitalWrite(pin, HIGH); // set the level of servo pin as high
  delayMicroseconds(timess); // delay microsecond of pulse width
  digitalWrite(pin, LOW); // set the level of servo pin as low
  delay(20 - timess / 1000);
}//}

3

Re: SERVO MOTOR SG90

http://forum.rcl-radio.ru/uploads/images/2024/09/374a1a618579d08c813cbcac20dd04d6.png

http://forum.rcl-radio.ru/uploads/images/2024/09/06ff8a9bd7d1db5f1c15ce38ffc49a04.png

http://forum.rcl-radio.ru/uploads/images/2024/09/d88391f85971bb70c77479741d268196.png

http://forum.rcl-radio.ru/uploads/images/2024/09/a6c3c6ef5209eac7415c293d2f23369a.png

http://forum.rcl-radio.ru/uploads/images/2024/09/2b2a1f19d4ea57cb591ce093e6927769.png

http://forum.rcl-radio.ru/uploads/images/2024/09/01a5e9f1fabf77df3206f2a25023607a.png

http://forum.rcl-radio.ru/uploads/images/2024/09/17c50a01f6bc66aa8f54e7bae794fae0.png

4

Re: SERVO MOTOR SG90

Attiny13 примеры для SG90

Выход управления PB0

Медленное изменения угла поворота сервопривода:

#include <avr/io.h>
#include <util/delay.h>


int main(void) {
   DDRB |=(1<<PB0);        
 
while(1) { 
   for(int i=0;i<=170;i++){
      servoPulse(i);
      _delay_ms(30);
  }
  
  
  for(int i=170;i>=0;i--){
      servoPulse(i);
      _delay_ms(30);
  }  
  }
}

void servoPulse(int angle){
  int times = ((angle * 11) + 500)/10; 
  PORTB |= (1<<PB0);
  for(int n=0;n<times;n++){_delay_us(10);} 
  PORTB &=~ (1<<PB0);
  for(int n=0;n<2000-times;n++){_delay_us(10);} 
}

Быстрое изменения угла поворота сервопривода:

#include <avr/io.h>
#include <util/delay.h>

int val;

int main(void) {
   DDRB |=(1<<PB0);        
 
while(1) { 
      val = 0;
      for (int i = 0; i <= 50; i++){servoPulse(val);}
      _delay_ms(1000);
      val = 90;
      for (int i = 0; i <= 50; i++){servoPulse(val);}
      _delay_ms(1000);
      val = 160;
      for (int i = 0; i <= 50; i++){servoPulse(val);}
      _delay_ms(1000);
      val = 90;
      for (int i = 0; i <= 50; i++){servoPulse(val);}
      _delay_ms(1000);
}}

void servoPulse(int angle){
  int times = ((angle * 11) + 500)/10; 
  PORTB |= (1<<PB0);
  for(int n=0;n<times;n++){_delay_us(10);} 
  PORTB &=~ (1<<PB0);
  for(int n=0;n<2000-times;n++){_delay_us(10);} 
}