Arduino Project Report on Servo Motor
This report covers all that has been done from the beginning.
An Arduino is an open source electronics prototyping platform based on flexible easy to use hardware and software can sense environment by receiving input from a variety of sensors and can affect its surroundings by controlling lights, motors and other actuators.
We began with the blinking led light study
three connecting wires, and arduino Uno board, bread board, resistor and a led bulb.
How to connect
First, connect one end of a wire into the '5V' Power output on the Arduino., and the other end into the positive section of the breadboard.
Then you need to plug one end of a wire into the ground ('GND') Power output, and the other end into the Negative section of the Breadboard.
Connect one end of the resistor from the Negative section of the Breadboard, to any horizontal line on the main section.
A resistor of about 330 ohms was used in order not to blow the LED.
Now to attach the Led into the circuit. The bulb will not do anything until you have uploaded the code to the board, which is done at a later stage.
You have to put the cathode pin next to the wire connected to the port, and the anode pin next to the resistor. Finally plug the arduino pin into the computer and upload the code for it to run.
Code
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Servo Movement
Servo motors are regular DC motors used together with a potentiometer together with an arduino board. the arduino reads the voltage on the middle pin of the potentiometer and adjust the position of the servo shaft. Using an arduino you can tell a servo to go to a specified position.
It has a built in motor a feedback circuit and a motor drive.
The two types of servo motor is the DC and AC servo motor.
It is easy to control the speed and direction of a DC motor. Due to high ratio toque to inertia DC motor responses quickly to control signal.
We preceded to learn on how the servo works when connected to the arduino board.
We learnt about how to move the servo from 90 degrees to 180 degrees and also insteps of 1 degree to 180 degrees.
How to connect
The servo motor has three wires, power, signal and the ground wire.
The ground wire (black)is connected to the ground pin on the Arduino board.
The power wire is connected to the 5V pin on the Arduino board.
Finally, the signal wire is connected to the digital pin on the Arduino board of which any port can be chosen but must be indicated in the program.
A specification needs to be done with the servo pin used so that it is well programmed in the code to aid simulation. On the various port on the board u can use any port.
The Servo Library
We also studied about the servo library and how it helps in the programming.
To generate servo control signals the library is needed in the program which is #include<Servo.h>
You can also find it in the arduino IDE at the help menu and go to reference. Examples include attach (), writeMicoseconds (), detach (). etc. The Servo.h has all the control signals hence when included makes it easy. The attach() is used to indicate the servo library the pin it is connected to .The write function is used to give high to the servo with the appropriate angle.
The writeMicoseconds works like the delay, read function reads the current angle of the servo and also the attach function checks if the servo variable is attached to the pin. The detach is also used to disconnect a servo incase they are more than one connected.
The programs below cause the servo to move from 0 to 180 degrees and
#include <Servo.h> //Servo libraryServo myservo;
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 angledelay(50); }
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 angledelay(50); } delay(1000);
<a name="_GoBack"></a>
The program below also causes the program to run from zero to 180.
#include <Servo.h>Servo servo_test; // create servo object to control a servo// a maximum of eight servo objects can be createdint pos = 0; int angle =0; // variable to store the servo position
void setup(){myservo. attach (9); // attaches the servo on pin 9 to the servo object}void loop(){for (pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees{ // in steps of 1 degree
}
Calculating the angle and the distance of the servo measured by the ultrasonic sensor
We preceded to learn about how to calculate the distance of the servo with the ultrasonic sensor.
We fixed the servo motor on a board and also fixed the sensor on it to. With all other connections in place we had the sensor turn together with the servo motor .The code below calculated the distance and the angle of the movement.
#include <NewPing.h>#include <Servo.h> #define TRIGGER_PIN 13#define ECHO_PIN 12#define MAX_DISTANCE 200
Servo servo_test; //initialize a servo object for the connected servo int angle = 0;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {pinMode(TRIGGER_PIN, OUTPUT);pinMode(ECHO_PIN, INPUT);Serial.begin(9600);servo_test.attach(9);}
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 angleSerial.print("Obstacle is ");Serial.print(sonar.ping_cm());Serial.print(" cm away at an angle of ");Serial.print(angle);Serial.println(" degrees");delay(50); }
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 angleSerial.print("Obstacle is ");Serial.print(sonar.ping_cm());Serial.print(" cm away at an angle of ");Serial.print(angle);Serial.println(" degrees");delay(50); } delay(1000);
}
Here are 2 photos of our setup
|
|
Ultrasonic distance sensor mounted on the servo motor |
Arduino with connection to servo motor |
--
Uli Raich - 2017-05-01
Comments
Topic revision: r1 - 2017-05-01
- uli