Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
π Ready to explore and create with us? Click [here] and join today!
Lesson 33: Servo Motor (SG90)ο
In this lesson, you will learn how to use Arduino to control a servo motor and make it rotate from 0 to 180 degrees and back. We will cover the usage of the Servo library, defining and using variables for servo control, as well as implementing a for loop for gradual movement. This project is ideal for beginners as it provides hands-on experience with motor control and basic programming principles in Arduino.
Required Componentsο
In this project, we need the following components.
Itβs definitely convenient to buy a whole kit, hereβs the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
Arduino UNO R3 or R4 |
|
Wiringο
Codeο
Code Analysisο
Here, the
Servolibrary is included which allows for easy control of the servo motor. The pin connected to the servo and the initial angle of the servo are also defined.#include <Servo.h> const int servoPin = 9; // Define the servo pin int angle = 0; // Initialize the angle variable to 0 degrees Servo servo; // Create a servo object
The
setup()function runs once when the Arduino starts. The servo is attached to the defined pin using theattach()function.void setup() { servo.attach(servoPin); }
The main loop has two
forloops. The first loop increases the angle from 0 to 180 degrees, and the second loop decreases the angle from 180 to 0 degrees. Theservo.write(angle)command sets the servo to the specified angle. Thedelay(15)causes the servo to wait for 15 milliseconds before moving to the next angle, controlling the speed of the scanning movement.void loop() { // scan from 0 to 180 degrees for (angle = 0; angle < 180; angle++) { servo.write(angle); delay(15); } // now scan back from 180 to 0 degrees for (angle = 180; angle > 0; angle--) { servo.write(angle); delay(15); } }