2.7 Swing Servo

In this project, we use a servo and a potentiometer to simulate a steering wheel. Rotating the potentiometer will drive the servo to turn together.

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

Kepler Kit

450+

Kepler Kit

You can also buy them separately from the links below.

SN

COMPONENT

QUANTITY

LINK

1

Raspberry Pi Pico W

1

BUY

2

Micro USB Cable

1

3

Breadboard

1

BUY

4

Jumper Wires

Several

BUY

5

Servo

1

BUY

6

Potentiometer

1

BUY

Wiring

controllable_servo0

  • The orange wire (signal) of the servo is connected to GP15, the red wire (power) is connected to VBUS, and the brown wire (ground) is connected to GND.

  • Potentiometer is a resistive element with 3 terminals, the 2 side pins are connected to 5V and GND, and the middle pin is connected to GP26(A0).

Code

Note

  • You can refer to the image below to write code by dragging and dropping.

  • Import 2.7_swing_servo.png from the path of kepler-kit-main\piper. For detailed tutorials, please refer to Import the Code.

controllable_servo

  • After connecting Pico W, click the Start button and the code starts to run.

  • Turn the potentiometer and the servo will follow. To see it clearly, you can insert a rocker arm in the servo shaft.

How it Works?

controllable_servo1

Set the rotation speed of pin15 (servo) to 15%.

  • [servo pin() set speed to ()%]: Used to set the rotation speed of the servo pin, the range is 0%~100%.

controllable_servo2

Create a variable [angle], then read the voltage of A0. Use the [map value () from () to ()] block, map the voltage of A0 from 0 to 3.3V voltage range to 0 to 180°, and then use the mapped angle as the rotation angle of the servo.

  • [map value () from () to ()]: map a value from one range to another.

Note

The voltage of A0~A2 takes the range of 0~3.3V, even if your power supply is connected to VBUS (5V).