RoboCircuits
Published © CC BY-ND

The Raspberry Pi Pico Tutorial 2

PWM and Servo

BeginnerProtip1 hour2,051
The Raspberry Pi Pico Tutorial 2

Things used in this project

Hardware components

Raspberry Pi Pico
Raspberry Pi Pico
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
LED (generic)
LED (generic)
×1

Software apps and online services

VS Code
Microsoft VS Code

Story

Read more

Code

PWM

MicroPython
import time
from machine import Pin, PWM

MIN_DUTY = 1000 # 5 percent of 65025 = 3251.25
MAX_DUTY = 9000 # 10 percent of 65025 = 6502.5

pwm = PWM(Pin(15))
pwm.freq(50)

duty = MIN_DUTY
direction = 1

while True:
	for _ in range(1024):
		duty += direction
		if duty > MAX_DUTY:
			duty = MAX_DUTY
			direction = -direction
		elif duty < MIN_DUTY:
			duty= MIN_DUTY
			direction = -direction
		pwm.duty_u16(duty)
        

Credits

RoboCircuits
40 projects • 226 followers
YouTuber, Explorer, Creator, Programmer, Arduino Lover and Engineer

Comments