Arduino Servo Motors (https://learn.adafruit.com/adafruit-arduino-lesson-14-servo-motors/arduino-code-for-sweep)

In this lesson, you will learn how to control a servo motor using an Arduino.
Firstly, you will get the servo to sweep back and forth automatically and then you will add a pot to control the position of the servo.
learn_arduino_knob.jpg
To build the projects described in this lesson, you will need the following parts.

Part

Qty

learn_arduino_motor_servo.jpg
Servo Motor 1
learn_arduino_pot.jpg
10 kΩ variable resistor (pot)
1
learn_arduino_breadboard_half_web.jpg
Half-size Breadboard
1
learn_arduino_uno_r3_web.jpg
Arduino Uno R3
1
learn_arduino_jumpers_web.jpg
Jumper wire pack
1
learn_arduino_cap_100uF.jpg
100 µF capacitor
Optional

The Breadboard Layout for 'Sweep'

For this experiment, the only thing connected to the Arduino is the servo motor.
learn_arduino_fritzing_sweep.jpg
The servo motor has three leads. The color of the leads varies between servo motors, but the red lead is always 5V and GND will either be black or brown. The other lead is the control lead and this is usually orange or yellow. This control lead is connected to digital pin 9.
The servo is conveniently terminated in a socket into which we can push jumper wires, to link it to the breadboard and then to the Arduino.
learn_arduino_sweep.jpg

If the Servo Misbehaves

Your servo may behave erratically, and you may find that this only happens when the Arduino is plugged into certain USB ports. This is because the servo draws quite a lot of power, especially as the motor is starting up, and this sudden high demand can be enough to drop the voltage on the Arduino board, so that it resets itself.
If this happens, then you can usually cure it by adding a high value capacitor (470uF or greater) between GND and 5V on the breadboard.
learn_arduino_fritzing_with_cap.jpg
The capacitor acts as a reservoir of electricity for the motor to use, so that when it starts, it takes charge from the capacitor as well as the Arduino supply.
The longer lead of the capacitor is the positive lead and this should be connected to 5V. The negative lead is also often marked with a '-' symbol.

Arduino Code for 'Sweep'

Load up the following sketch onto your Arduino. You should find that the servo immediately begins to turn first in one direction and then back in the other.
The sketch is based on the standard 'sweep' sketch that you can find in the Arduino Examples under the folder 'servo'. You can if you prefer just run that sketch.
  1. /*
  2. Adafruit Arduino - Lesson 14. Sweep
  3. */
  4.  
  5. #include <Servo.h>
  6.  
  7. int servoPin = 9;
  8. Servo servo;
  9. int angle = 0; // servo position in degrees
  10. void setup()
  11. {
  12. servo.attach(servoPin);
  13. }
  14. void loop()
  15. {
  16. // scan from 0 to 180 degrees
  17. for(angle = 0; angle < 180; angle++)
  18. {
  19. servo.write(angle);
  20. delay(15);
  21. }
  22. // now scan back from 180 to 0 degrees
  23. for(angle = 180; angle > 0; angle--)
  24. {
  25. servo.write(angle);
  26. delay(15);
  27. }
  28. }
Servo motors are controlled by a series of pulses and to make it easy to use them, an Arduino library has been created so that you can just instruct the servo to turn to a particular angle.
The commands for using a servo are like built-in Arduino commands, but because you are not always going to be using a servo in your projects, they are kept in something called a library. If you are going to use commands in the servo library, you need to tell the Arduino IDE that you are using the library with this command:
  1. #include <Servo.h>
As usual, we then use a variable 'servoPin' to define the pin that is to control the servo.
This line:
  1. Servo servo;
defines a new variable 'servo' of type 'Servo'. The library has provided us with a new type, like 'int' or 'float' that represents a servo. You can actually define up to eight servos in this way, so if we had two servos, then we could write something like this:
  1. Servo servo1;
  2. Servo servo2;
 In the 'setup' function we need to link the 'servo' variable to the pin that will control the servo using this command:
  1. servo.attach(servoPin);
The variable 'angle' is used to contain the current angle of the servo in degrees. In the 'loop' function, we use two 'for' loops to first increase the angle in one direction and then back in the other when it gets to 180 degrees.
The command:
  1. servo.write(angle);
tells the servo to update its position to the angle supplied as a parameter.

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

IC 13

DIY Arduino Traffic Light Pedestrian Light Push Button Control (https://thecustomizewindows.com/2016/06/diy-arduino-traffic-light-pae-push-button/)

สรุป Augmented Reality (AR) and Virtual Reality (VR)