วันศุกร์ที่ 13 กรกฎาคม พ.ศ. 2561

Lab: Servo Motor Control with an Arduino

Originally written on July 21, 2014 by Benedetta Piantella Simeonidis
Last modified on August 27, 2016 by Tom Igoe

Introduction

Contents [show]
In this lab, you’ll control a servomotor’s position using the value returned from an analog sensor. Servos are the easiest way to start making motion with a microcontroller. Even though they don’t turn 360 degrees but only 180, you can use them to create all sorts of periodic or reciprocating motions. Check out some of the mechanisms at Rob Ive’s site for ideas on how to make levers, cams, and other simple machines for making motion.
What You’ll Need to Know
To get the most out of this lab, you should be familiar with the following concepts. You can check how to do so in the links below:
  • What is a Analog Input with Arduino
  • What is an Arduino Library
  • Connect an analog input sensor and a servo
  • Pick any analog input and connect it to Analog pin 0 as you did in the Analog Input Labcovered previously. Then connect an RC servomotor to digital pin 3. The yellow wire of the servo goes to the pin, and the red and black wires go to +5V and ground, respectively. Related video:Intro to Servo Motors
  •     

    Program the Microcontroller

    First, find out the range of your sensor by using analogRead()to read the sensor and printing out the results.
    void setup() {
      Serial.begin(9600);       // initialize serial communications
    }
     
    void loop()
    {
      int analogValue = analogRead(A0); // read the analog input
      Serial.println(analogValue);      // print it
    }
    Now, map the result of the analog reading to a range from 0 to 179, which is the range of the sensor in degrees. Store the mapped value in a local variable called servoAngle.
    void setup() {
      Serial.begin(9600);       // initialize serial communications
    }
     
    void loop()
    {
      int analogValue = analogRead(A0); // read the analog input
      Serial.println(analogValue);      // print it
     
      // if your sensor's range is less than 0 to 1023, you'll need to
      // modify the map() function to use the values you discovered:
      int servoAngle = map(analogValue, 0, 1023, 0, 179);
    }
    Finally, add the servo library at the beginning of your code, then make a variable to hold an instance of the library, and a variable for the servo’s output pin. In the setup(), initialize your servo using servo.attach(). Then in your main loop, use servoAngle to set the servo’s position.
    #include <Servo.h>      // include the servo library
     
    Servo servoMotor;       // creates an instance of the servo object to control a servo
    int servoPin = 3;       // Control pin for servo motor
     
    void setup() {
      Serial.begin(9600);       // initialize serial communications
      servoMotor.attach(servoPin);  // attaches the servo on pin 2 to the servo object
    }
     
    void loop()
    {
      int analogValue = analogRead(A0); // read the analog input
      Serial.println(analogValue);      // print it
     
      // if your sensor's range is less than 0 to 1023, you'll need to
      // modify the map() function to use the values you discovered:
      int servoAngle = map(analogValue, 0, 1023, 0, 179);
     
      // move the servo using the angle from the sensor:
      servoMotor.write(servoAngle);
    }

ไม่มีความคิดเห็น:

แสดงความคิดเห็น