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+

Newton Lab Kit

You can also buy them separately from the links below.

SN

COMPONENT

QUANTITY

LINK

1

Raspberry Pi Pico 2

1

BUY

2

Micro USB Cable

1

3

Breadboard

1

BUY

4

Jumper Wires

Several

BUY

5

Servo

1

BUY

Circuit Diagram

sch_servo

Wiring Diagram

wiring_servo

  • 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 from newton-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

  1. Including the Servo Library:

    Includes the Servo library, which provides functions to control the servo motor.

    #include <Servo.h>
    
  2. Creating a Servo Object:

    Creates a Servo object named myServo to control the servo.

    Servo myServo;
    
  3. Attaching the Servo to a Pin:

    Attaches the servo to GPIO pin 15 on the Pico.

    myServo.attach(15);
    
  4. 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.