Lesson 7: Servo Motor Control
Lesson 7: Servo Motor Control
Lesson: Servo Motor Control
Objective: At the end of this lesson, you will be able to control a servo motor using an Arduino board.
Materials Required:
- Arduino board
- Servo motor
- Jumper wires
- USB cable
Circuit Connections:
- Connect the signal wire (usually yellow or white) of the servo motor to a digital pin on the Arduino board (e.g., pin 9).
- Connect the power wire (usually red) of the servo motor to the 5V pin on the Arduino board.
- Connect the ground wire (usually black or brown) of the servo motor to the ground (GND) pin on the Arduino board.
Arduino Code:
#include <Servo.h>
Servo myServo;
void setup() {
myServo.attach(9); // Attach the servo to digital pin 9
}
void loop() {
myServo.write(0); // Set servo angle to 0 degrees
delay(1000); // Delay for 1 second
myServo.write(90); // Set servo angle to 90 degrees
delay(1000); // Delay for 1 second
myServo.write(180); // Set servo angle to 180 degrees
delay(1000); // Delay for 1 second
}
Experiment Steps:
- Set up the circuit connections as described above.
- Connect the Arduino board to your computer using a USB cable.
- Open the Arduino IDE software and create a new sketch.
- Copy and paste the provided code into the Arduino IDE.
- Click the "Upload" button to upload the code to the Arduino board.
- Observe the servo motor rotating to different angles: 0 degrees, 90 degrees, and 180 degrees, with a 1-second delay between movements.
- Experiment with modifying the angles and delays in the code to achieve different servo motor movements.
Explanation: In this experiment, we are using the Servo library to control a servo motor connected to the Arduino board. The servo motor is connected to a digital pin (pin 9 in this example), and the Servo library is used to control the servo motor's angle.
The setup()
function initializes the servo motor by attaching it to the specified digital pin using myServo.attach()
. The servo motor is ready for control after this step.
The loop()
function contains the main control code for the servo motor. In this example, we set the servo angle to 0 degrees using myServo.write(0)
and introduce a delay of 1 second using delay(1000)
. Then, we set the servo angle to 90 degrees and introduce another delay, followed by setting the angle to 180 degrees with a delay. This pattern repeats in a loop, resulting in the servo motor moving back and forth between the specified angles.
You can modify the angles and delays in the code to achieve different servo motor movements according to your requirements.
Remember to follow safety precautions while working with electronic components and circuits.
Comments
Post a Comment