Authorized Online Retailers:

Buy from USA Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES ここでご購入を!

In the last lesson, we learned how to read analog data from the ADC pin. In this lesson, we will also read an analog signal from the ADC2 pin. However, this time, the input device is a potentiometer (adjustable resistor) instead of a photoresistor. We will also connect a servo motor to GP16, which can generate a PWM signal. The servo will rotate its arm when you rotate the potentiometer. Through this project, we will learn how to use servo blocks to rotate the servo arm.

  1. Raspberry Pi Pico board and microUSB cable
  2. A computer to run Thonny Python IDE
  3. Breadboard x 1
  4. Potentiometer x 1
  5. Servo motor x 1
  6. some jumper wires

In above circuit graph, you can see that:
Servo red wire connected to Pico Vbus pin(5V)
Servo brown wire connected to Pico GND
Servo yellow wire   connected to Pico PWM pin(GP16)
Potentiometer GND connected to Pico GND
Potentiometer VCC connected to Pico 3.3V
Potentiometer  middle pin connected to Pico ADC pin(GP28)

Making Blocks
Step 1: Create a New Project and Connect the Pico to the Serial Port

1)Click the New Project icon. You will see a My Project icon with a fancy name pop up (see Picture 2.1),
2)Click the new icon in My Projects.

Now you will enter a new graphic programming page. Please click the CONNECT button at the bottom of the page and connect your Pico board to your project.

3)Then your browser will pop up a Serial Port option menu. Please select Pico from the menu and click the Connect button.

If you cannot see Pico in the Serial Port menu, it means your Pico is not set up properly. You need to go back to Lesson 1, Step 1, and set up the Pico again.

After you connect the Pico to the Serial Port, your GUI Connect button will switch to the Disconnect button as follows:

Step 2: Build Graphic Coding Chips

1) Click on Chip -> Start, then move the Start chip to the coding area as follows:

2)Click on Loops -> Repeat while true and move this block just below the Start block as follows

3) Click on Variables -> Create Variable and name the variable “servo_val” as follows

4)Now you will see some new variable blocks. Move the “set servo_val to” block inside the repeat loop block as follows

5)From the Chip category, add a “Read Voltage” block to the “set servo_val to” block. Change the port from A0 to A2 as follows

Explanation: This block will read the voltage value from the ADC2 pin (GP28) and assign the value to the “servo_val” variable.

6)From the Chip category, add a “Print” block below the “set servo_val to” block.

7)Right-click and delete the “” golden block inside the Print block.

8)From the Variable category, add a “servo_val” block to the Print block as follows

9)From the Logic category, add an “if_do_else” block under the Print block as follows:
10)Add an “= ” block in the “if” block as follows:
11)From the Variables category, add a “servo_val” block in the “if” block, then change the “=” sign to “<" as follows

12)From the Values category, add a “0” block to the “if” block, then change the block value from 0 to 1 as follows

13)From the Action category, add a “servo pin set angle to” block to the “do” area, change the pin number from 0 to 15, and change the angle from 90 to 0 as follows:
14)Right-click the light purple area, duplicate a new “if_do_else” block, then insert the new block into the “else” area. Change the new if statement area from <1 to <2, and change the "set angle to" area from 0 to 90 as follows:

15)From the Action category, add a “servo pin set angle to” block to the “else” area, change the pin number from 0 to 15, and change the angle from 90 to 180 as follows

16)From the Chip category, add a “Wait 1 second” block below the outer “if_do_else” block, change the wait time from 1 to 0.5 as follows:
Now we have finished building the program. We can click the “START” button to run the program.

Step 3: Test the program

When you rotate the Potentiometer, the Servo arm will rotate accordingly. You can click the “Console” tag at the bottom, and you will see the console printing value changing as long as you rotate the Potentiometer.

Learn More about parts SKU:2021005900

No. Picture Product Name Link
1 Raspberry Pi Pico Board
2 Servo motor
3 Infrared Sensor Module
4 RFID Module and card
5 Push Buttons and Hats
6 LED(6 x White, 6 x Red, 6 x Yellow, 6x Green)
7 Servo motor
8 Piezo Buzzer Module
9 I2C LCD Display(16×2)
10 Philips Screwdriver
11 Solderless Prototype Breadboard
12 Potentionmeter (10K adjustable resistor)
13 40Pin M to M Jumper Wires
14 20Pin M to F Jumper wires 15cm