Delivery throughout Europe

Ordered before 16:00 = Shipped today

Fast delivery with DHL

XNUMX days return *


Country

Raspberry Pi Pico – Lesson 5: Raspberry Pi Control Pico Servo

Continue your programming adventure during Lesson 5, where we control an SG90 servo with the Raspberry Pi Pico. This educational lesson is ideal for beginners who want to start with the Thonny programming tool and MicroPython. Learn how to use the Raspberry Pi Pico connects to your computer, uploads programs and controls an SG90 servo. Discover the possibilities of servo control with the Pico and take your first steps in using it Raspberry Pi Pico for motor control. An exciting project to further develop your skills!

This manual covers: 

  • Level - Beginner 25% 25%
  • Duration 20 min 25% 25%
  • Costs €10,95 11% 11%

This is the fifth lesson from the introductory projects for the Raspberry Pi Pico. Before you start this lesson, we recommend that you first complete the previous lessons. You will find lesson 4 HERE, lesson 1 you will find HERE.

Step 2 – Install software on the Raspberry Pi Pico for controlling the Servo

You can skip this step if you already did it in Lesson 1.
Around the Raspberry PiTo be able to program with MicroPython, we must first flash the firmware of the Pico.
This means that we provide the internal software that starts the pico with a special Python version.

Download firmware from the website below (uf2 file)
https://micropython.org/download/RPI_PICO/
https://micropython.org/download/RPI_PICO_W/
(Make sure you select the correct version of the firmware, the WiFi version and the non-WiFi version are different)
Press the white boot button on the pico and keep it pressed.
Plug the pico into a USB port on your computer. (Then you can release the button)
There will now be a drive letter, as accessible with a memory stick.
Copy the firmware file RPI_PICO_xxxxxx.uf2 to this drive
(pico will restart and the firmware will be installed)
Download and install Thonny on your PC.
(https://thonny.org/)

Select the MicroPython Interpreter below
Tools > Options > Interpreter > MicroPython (Raspberry Pi Pico)

 

Place the components as shown below on a breadboard.

raspberry pico-servo sg90

Please note the colors of the Servo:
The orange cable can also be yellow.
The brown can also be black. 

Step 3 – Programming the Raspberry Pi Pico to control the Servo

Now you can type in the program below in the Thonny interface.

from servo import Servo import time sg90_servo = Servo(pin_id=6) while True: sg90_servo.write(0) time.sleep(1) sg90_servo.write(90) time.sleep(1)

Explanation of the program:

  1. Import the Servo module
  2. Imported the time module
  3. .
  4. Define the variable sg90_servo on pin_id GP6 (pin 9)
  5. .
  6. Endless repeat loop
  7. write the value 0 to the servo
  8. wait 1 second
  9. write value 90 to the servo
  10. wait 1 second

The above example uses the PWM function in the servo module, use the below to control the servo with PWM routines.
#4500 corresponds to 90 degrees as above.

from machine import Pin from machine import PWM from time import sleep pwm = PWM(Pin(6)) pwm.freq(50) # Servo function def setServoCycle (position): pwm.duty_u16(position) sleep(0.01) while True: for pos in range(1000,4500,50): setServoCycle(pos) for pos in range(4500,1000,-50): setServoCycle(pos)

The example below uses the PWM function in the servo module, use the following to control the servo with PWM routines.

#4500 corresponds to 90 degrees as above.

Then press save and save the program on your own computer.

Press the red stop sign button to restart the backend.
(Thonny will now reconnect to your Pico)

Then you could press the green button (Run current script).

If the pico is now removed from the computer and reconnected, the program will not be started.
We only tested the programming code.

To enable automatic starting, we must save the program again, but then select the pico.
File > Save As > Raspberry Pi Pico

Save the program with its name main.py. The pico will automatically start the program with this name when connecting the Pico to a USB port.
In this case you do need a connection with your PC and Thonny to see the measured results!

Step 4 – Result: Servo is controlled with the Raspberry Pi Pico

You have now learned how to connect and control a (servo) motor Raspberry Pi Peak.
Did you enjoy doing this project? Check out our other projects, or Les zes from this Pico series!

The rating of www.elektronicavoorjou.nl at WebwinkelKeur Reviews is 9.3/10 based on 5005 reviews.