2016年8月25日星期四

Connecting a Servo Motor

Servo motors typically have three wires. The power wire, usually red, is connected to teh power rail. The ground wire, ususally black or brown, is connected to the ground rail. The third wire, usually yellow or oragne, is the signal wire and is connected directly to a digital pin on the Arduino. The Arduino can normally directly supply power to a servo motor, but when using serveral servo motors, you ned to separate the Arduino power supply to the servo power supply to avoid brown outs. Servo motors, even if they do not always act like typical motors, still have a small motor inside and can draw large amounts of current, far more than what the ATmega can deliver.



Before using servo motors, you must import the Servo library. You can do this either by importing the library. You can do this either by importing the library through the Arduino IDE menu (Sketch Import Library servo) or by manually typing:

#include (servo.h)

In your software, you must first creat a new servo object before issuing instructions. You must create one object per servo motor (or group of servo motors) to contorl.

Servo frontWheels;
Servo rearWheels;

To tell the Arduino which pins the servo motors are connected to, call attach(), specifying the pin, and optionally, specifying the minimum and maximum pulse size.

servo attach (pin)
servo attach (pin, min, max)

By default, Arduino uses 544 microseconds as the minimum pulse length (equivalent to 0 degrees) and 2,400 microseconds as the maximum pulse width (equivalent to 180 degrees.) If your servo motor has different settings for a maximum and minimum pulse, you can change the values in attch () by specifying the duration in microseconds. For example, a servo motor that uses a 1 millisecond minimum and 2 millisecond maximum can be configured like this:

servo.attach (pin, 1000, 2000);

From then on, the Arduino automatically calculates the length of the pulse according to the wanted angle but will not issue commands until a function specifically orders the servo motor to move.

没有评论:

发表评论