Arduino Servo Motor Control / Usage
You can interface little servo engines specifically to an Arduino to control the pole position exactly.
Since servo engines utilize input to decide the situation of the pole, you can control that position decisively. Therefore, servo engines are utilized to control the situation of items, pivot objects, move legs, arms or hands of robots, move sensors and so forth with high exactness. Servo engines are little in estimate, and in light of the fact that they have worked in hardware to control their development, they can be associated specifically to an Arduino.Most servo engines have the accompanying three associations:
- Dark/Brown ground wire.
- Red power wire (around 5V).
- Yellow or White PWM wire.
In this test, we will associate the power and ground sticks straightforwardly to the Arduino 5V and GND pins. The PWM information will be associated with one of the Arduino's advanced yield pins.
Test #1
Requirements
- 1 x TowerPro SG90 servo motor - Click to Buy
- 1 x Arduino Mega2560 (You can use Uno model too) - Click to Buy
- 3 x jumper wires - Click to Buy
Wiring Diagram
The best thing about a servo engine is that it can be associated straightforwardly to an Arduino. Associate with the engine to the Arduino as appeared in the table beneath:
- Servo red wire – 5V pin Arduino
- Servo brown wire – Ground pin Arduino
- Servo yellow wire – PWM(9) pin Arduino
Alert: Do not attempt to pivot the servo engine by hand, as you may harm the engine.
Code
At the point when the program begins running, the servo engine will pivot gradually from 0 degrees to 180 degrees, one degree at any given moment. At the point when the engine has pivoted 180 degrees, it will start to turn the other way until the point when it comes back to the home position.
#include <Servo.h> // Servo Library Servo servo_test; //initialize a servo object for the connected servo int angle = 0; void setup() { servo_test.attach(9); // attach the signal pin of servo to pin9 of arduino } void loop() { for(angle = 0; angle < 180; angle += 1) // command to move from 0 degrees to 180 degrees { servo_test.write(angle); //command to rotate the servo to the specified angle delay(15); } delay(1000); for(angle = 180; angle>=1; angle-=5) // command to move from 180 degrees to 0 degrees { servo_test.write(angle); //command to rotate the servo to the specified angle delay(5); } delay(1000); }
Test #2
This trial is basically the same as Experiment 1, aside from that we have included a potentiometer for position control. The Arduino will read the voltage on the center stick of the potentiometer and alter the situation of the servo engine shaft.
Requirments
- 1 x TowerPro SG90 servo engine
- 1 x Arduino Mega2560
- 1 x 20kω potentiometer
- 1 x breadboard
- 6 x jumper wires
Wiring Diagram
Associate the circuit as show in the figure beneath:
- Servo red wire – 5V stick Arduino
- Servo dark colored wire – Ground stick Arduino
- Servo yellow wire – PWM(9) stick Arduino
- Potentiometer stick 1 - 5V stick Arduino
- Potentiometer stick 3 - Ground stick Arduino
- Potentiometer stick 2 – Analog In (A0) stick Arduino
Code
Once the program is begun, turning the potentiometer should make the pole of the servo engine pivot.
#include <Servo.h>//Servo library Servo servo_test; //initialize a servo object for the connected servo int angle = 0; int potentio = A0; // initialize the A0analog pin for potentiometer void setup() { servo_test.attach(9); // attach the signal pin of servo to pin9 of arduino }
void loop() { angle = analogRead(potentio); // reading the potentiometer value between 0 and 1023 angle = map(angle, 0, 1023, 0, 179); // scaling the potentiometer value to angle value for servo between 0 and 180) servo_test.write(angle); //command to rotate the servo to the specified angle delay(5);
}
Comments
Post a Comment