Welcome to our store Learn more

New collections added! Learn more

Announce a promotion

Share something special with your customers.

DISCOUNTCODE

Beginner arduino servo motor project

Beginner arduino servo motor project

Sayon Rai |

Objectives

Make a wall-mounted dial to indicate today's weather conditions, your current location (à la Harry Potter), or power usage in your home. Or build brainwave-controlled wings. Or a robot arm to serve you Bellinis. The power is yours, with SERVOS!

  • Components required
  •  Components Link
    Arduino x 1
    Breadboardx 1 https://orezaar.com/search?q=breadboard&options%5Bprefix%5D=last
    Jumper wires x1
    servos x 1
    switch button 12mm x 1
  • Code 
  • SweepCalibrated
    • /* 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 george;  // 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
      int ledPin = 13;
      int mini = 15; // servo's minimum position
      int maxi = 160; // servo's maximum position
      
      void setup() {
        george.attach(9);  // attaches the servo on pin 9 to the servo object
      }
      
      void loop() {
        for (pos = mini; pos <= maxi; pos += 1) { // goes from 0 degrees to 180 degrees
          // in steps of 1 degree
          george.write(pos);              // tell servo to go to position in variable 'pos'
          delay(15);                       // waits 15ms for the servo to reach the position
        }
        for (pos = maxi; pos >= mini; pos -= 1) { // goes from 180 degrees to 0 degrees
          george.write(pos);              // tell servo to go to position in variable 'pos'
          delay(15);                       // waits 15ms for the servo to reach the position
        }
        digitalWrite(ledPin, !digitalRead(ledPin));
      }
  • Dial Counter sketch
    /*
    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 = 10;    // variable to store the servo position
      int pushButton = 2;
      int ledPin = 13;
      int clicks = 0;
      
      void setup()
      {
        Serial.begin(9600);
        myservo.attach(9);  // attaches the servo on pin 9 to the servo object
        pinMode(pushButton, INPUT_PULLUP);
        digitalWrite(ledPin, LOW);
      }
      
      void loop() {
        int buttonState = digitalRead(pushButton);
        if (buttonState == 0 && pos <= 160) {
          clicks = clicks + 1;
          Serial.print("Clicks: ");
          Serial.println(clicks);
          pos = map(clicks, 0, 29, 15, 160);
          myservo.write(pos);              // tell servo to go to position in variable 'pos'
          delay(100);
        }
        else if (buttonState == 0) {
          digitalWrite(ledPin, HIGH); // LED goes on when the button has been pressed too many times
        }
      }
  • Schematics
    • Arduino with servo on pin 9
    • Arduino with servo & button(use dial counter)