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!
3.7 Swinging Servoď
In this lesson, weâll learn how to control a servo motor using the Raspberry Pi Pico 2. A servo motor is a device that can rotate to a specific angle between 0° and 180°. Itâs widely used in remote control toys, robots, and other applications that require precise position control.
Letâs get started and make the servo swing back and forth!
What Youâll Need
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 |
---|---|---|
Newton Lab Kit |
450+ |
You can also buy them separately from the links below.
SN |
COMPONENT |
QUANTITY |
LINK |
---|---|---|---|
1 |
1 |
||
2 |
Micro USB Cable |
1 |
|
3 |
1 |
||
4 |
Several |
||
5 |
1 |
Circuit Diagram
Wiring Diagram
Orange wire is signal and connected to GP15.
Red wire is VCC and connected to VBUS(5V).
Brown wire is GND and connected to GND.
Servos can draw significant current, especially under load. Since weâre using a small servo and not putting it under heavy load, powering it from the Picoâs VBUS pin is acceptable for this simple experiment. For larger servos or multiple servos, use an external power supply.
Setting Up the Servo Arm
Attach the servo arm (also called a horn) to the servoâs output shaft.
Secure it with the small screw provided with the servo if necessary.
Writing the Code
Note
You can open the file
3.7_swinging_servo.ino
fromnewton-lab-kit/arduino/3.7_swinging_servo
.Or copy this code into Arduino IDE.
Select the Raspberry Pi Pico 2 board and the correct port, then click âUploadâ.
#include <Servo.h>
Servo myServo; // Create a servo object
void setup() {
myServo.attach(15); // Attach the servo to GPIO pin 15
}
void loop() {
// Move the servo from 0 to 180 degrees
for (int angle = 0; angle <= 180; angle += 1) {
myServo.write(angle);
delay(15); // Wait 15 milliseconds for the servo to reach the position
}
// Move the servo from 180 to 0 degrees
for (int angle = 180; angle >= 0; angle -= 1) {
myServo.write(angle);
delay(15);
}
}
After uploading the code, the servo arm should start swinging smoothly from 0° to 180° and back. If the servo doesnât move or behaves erratically:
Check your wiring connections.
Ensure the servo is properly powered.
Make sure the servo is not mechanically blocked.
Understanding the Code
Including the
Servo
Library:Includes the
Servo
library, which provides functions to control the servo motor.#include <Servo.h>
Creating a
Servo
Object:Creates a
Servo
object namedmyServo
to control the servo.Servo myServo;
Attaching the Servo to a Pin:
Attaches the servo to GPIO pin 15 on the Pico.
myServo.attach(15);
Moving the Servo:
Moves the servo from 0° to 180° in 1-degree increments. The delay(15) provides a small delay to allow the servo to reach each position smoothly.
for (int angle = 0; angle <= 180; angle += 1) { myServo.write(angle); delay(15); }
Reversing the Movement: Moves the servo back from 180° to 0°, creating a back-and-forth swinging motion.
for (int angle = 180; angle >= 0; angle -= 1) { myServo.write(angle); delay(15); }
Further Exploration
Adjusting Speed:
Change the
delay()
value in the loops to make the servo move faster or slower.Controlling Position Directly:
Use
myServo.write(angle);
with a specific angle to set the servo to a fixed position.Interactive Control:
Connect a potentiometer to control the servo angle interactively.
Conclusion
In this lesson, youâve learned how to control a servo motor using the Raspberry Pi Pico and the Servo library. By adjusting the code, you can set the servo to any angle between 0° and 180°, allowing for precise control in your projects.