Skip to Content


Servo Motor Test & Control with Push Buttons





-CODE

//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(15);                       // waits 15 ms 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(15);                       // waits 15 ms for the servo to reach the position
  }





//Servo Motor  & Control with Push Buttons

#include <Servo.h>

// إنشاء كائن للسيرفو
Servo servo;

// أرقام البنات
const int servoPin = 9;
const int buttonRight = 2;
const int buttonLeft = 3;

// زاوية السيرفو
int angle = 90;

void setup() {

  // توصيل السيرفو
  servo.attach(servoPin);

  // بدء السيرفو من المنتصف
  servo.write(angle);

  // تفعيل المقاومة الداخلية للأزرار
  pinMode(buttonRight, INPUT_PULLUP);
  pinMode(buttonLeft, INPUT_PULLUP);

}

void loop() {

  // عند الضغط على الزر الأول تزيد الزاوية
  if (digitalRead(buttonRight) == LOW) {

    if (angle < 180) {
      angle += 10;
      servo.write(angle);
    }

    delay(200);
  }

  // عند الضغط على الزر الثاني تقل الزاوية
  if (digitalRead(buttonLeft) == LOW) {

    if (angle > 0) {
      angle -= 10;
      servo.write(angle);
    }

    delay(200);
  }

}








-CIRCUIT DIAGRAM





-COMPONENTS

• Arduino Uno R3 

https://www.gravityelectronic.com/shop/arduino-uno-r3-clone-original-chips-cable-30-cm-14248 

• SG90 Servo Motor 

https://www.gravityelectronic.com/shop?search=SG90 

• Breadboard (830 Points) 

https://www.gravityelectronic.com/shop/breadboard-830-point-not-split-power-rails-13419

 • 2 × Push Buttons

 https://www.gravityelectronic.com/shop?search=Push%20Button

 • 2 × 10KΩ Resistors 

https://www.gravityelectronic.com/shop?search=10K%20Resistor 

• Jumper Wires

 https://www.gravityelectronic.com/shop/jumper-wires