Controlling a servo motor with an Arduino Uno
![](https://aitwobo.com/wp-content/uploads/2024/06/Controlling-a-servo-motor-with-an-Arduino-Uno-1024x437.png)
Introduction
Controlling a servo motor with an Arduino Uno is a common and straightforward project for beginners. Servo motors are widely used in robotics, automation, and control systems for their precise control over angular position. In this guide, we’ll walk you through the steps to control a servo motor using an Arduino Uno.
Materials Needed
- Arduino Uno board
- Servo motor (e.g., SG90)
- Breadboard
- Jumper wires
- Power source (e.g., USB cable for the Arduino or external power supply)
Circuit Diagram
Here is a simple circuit diagram to follow:
Steps
-
Connect the Components on the Breadboard:
- Connect the brown wire (GND) of the servo motor to the GND pin on the Arduino.
- Connect the red wire (VCC) of the servo motor to the 5V pin on the Arduino.
- Connect the orange/yellow wire (Signal) of the servo motor to digital pin 9 on the Arduino.
-
Set Up the Arduino IDE:
- Download and install the Arduino IDE if you haven’t already.
- Connect your Arduino Uno to your computer using the USB cable.
- Open the Arduino IDE and select your board and port from the “Tools” menu:
- Board: “Arduino Uno”
- Port: Select the appropriate port that your Arduino is connected to.
-
Write the Code:
- Open a new sketch in the Arduino IDE and write the following code to control the servo motor:
#include <Servo.h> // Create a servo object to control a servo Servo myServo; // Define the servo pin const int servoPin = 9; void setup() { // Attach the servo on pin 9 to the servo object myServo.attach(servoPin); } void loop() { // Move the servo to 0 degrees myServo.write(0); delay(1000); // Wait for 1 second // Move the servo to 90 degrees myServo.write(90); delay(1000); // Wait for 1 second // Move the servo to 180 degrees myServo.write(180); delay(1000); // Wait for 1 second // Move the servo back to 90 degrees myServo.write(90); delay(1000); // Wait for 1 second }
- Upload the Code:
- Click the “Upload” button (right arrow icon) in the Arduino IDE to compile and upload the code to your Arduino Uno.
- After the upload is complete, the servo motor should start moving to different positions as specified in the code.
How It Works
- The
Servo.h
library is included to provide functions to control the servo motor. - A
Servo
object namedmyServo
is created to control the servo. - In the
setup()
function, the servo is attached to pin 9 usingmyServo.attach(servoPin)
. - In the
loop()
function, the servo motor is moved to 0 degrees, 90 degrees, 180 degrees, and back to 90 degrees with 1-second delays between each position usingmyServo.write(angle)
.
Troubleshooting
If your servo motor isn’t moving:
- Double-check all the connections.
- Make sure the correct board and port are selected in the Arduino IDE.
- Verify the power supply to the servo motor (ensure it is within the operating voltage range).
Conclusion
Congratulations! You’ve successfully controlled a servo motor using an Arduino Uno. This simple project is a great introduction to working with servos and can be expanded into more complex projects involving multiple servos, sensors, and other components. Keep experimenting and exploring the exciting world of electronics with Arduino!
Feel free to share your experiences or ask questions in the comments below. Happy tinkering!
Responses