xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

使用 Pico 控制 SG90 舵机 All In One

使用 Pico 控制 SG90 舵机 All In One

Raspberry Pi Pico

SG90 舵机

Servo / 舵机

n. 伺服,伺服系统; 继动

image

https://www.bilibili.com/video/BV1j7AHeQEha/

SG90 舵机, 资料

180度舵机与360度舵机可以使用同一个程序, 但是使用的效果不一样
180度舵机控制的是角度
360度舵机控制的是转速以及正反转

PWM信号与180度舵机的关系:
0.5ms----------------0度;
1ms -----------------45度;
1.5ms----------------90度;
2ms -----------------135度;
2.5ms ---------------180度;

PWM信号与360舵机转速的关系:
0.5ms----------------正向最大转速;
1.5ms----------------速度为0;
2.5ms----------------反向最大转速;

image

image

image

https://pan.baidu.com/s/1B01Pd5Lp_R3Q2puzzC9D_g?pwd=8889

https://www.zave.xin/index.php?c=show&id=3598

demos

Pico H / Pico

#!/usr/bin/env python3
# coding: utf8

from machine import Pin, PWM
from time import sleep

# PWM 控制舵机
# 21 号针脚,PWM0 / GP16
# ⚠️ Pin(16) 中 16 指的的是 GP16,对应的物理引脚是 21 号
pwm0 = PWM(Pin(16))
# 舵机标准 50Hz,之前写 500Hz 舵机不认
pwm0.freq(50)
print('pwm0.freq =', pwm0.freq());

# SG90:0° ≈ 1638,90° ≈ 4915,180° ≈ 8192
angle_0 = 1638
angle_90 = 4915
angle_180 = 8192

print("舵机测试开始 🚀")
while True:
    pwm0.duty_u16(angle_0)
    sleep(1)
    pwm0.duty_u16(angle_90)
    sleep(1)
    pwm0.duty_u16(angle_180)
    sleep(1)

print("舵机测试完成 ✅")

⚠️ 重要提醒(舵机常见坑)

标准 SG90 这类舵机的 PWM 频率应该是 50Hz,而代码里写的是 500,这个频率舵机无法正常工作,会抖动、乱转。

duty_u16:范围 0~65535,对应占空比;
舵机一般使用占空比 2.5% ~ 12.5%(对应大约 1638 ~ 8191),
图中 20000 / 40000 对于 50Hz 舵机也超出常规区间。

duty_u16 范围理解

duty_u16() 范围:0 ~ 65535,代表一个周期里高电平占比。
50Hz,周期 = 20ms:

  • 舵机标准:脉冲 0.5ms(0°)~ 2.5ms(180°)
  • 换算:
    • 0.5ms → 65535 * 0.5 / 20 ≈ 1638
    • 1.5ms(中间 90°)→ 65535 * 1.5 / 20 ≈ 4915
    • 2.5ms(180°)→ 65535 * 2.5 / 20 ≈ 8192

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

The C/C++ SDK

// Blink UF2 file

https://github.com/raspberrypi/pico-examples/blob/master/blink/blink.c

Blink UF2 file

https://github.com/raspberrypi/pico-sdk-prebuilts/releases/latest/download/blink_universal.uf2

# minicom
$ sudo apt install minicom
$ minicom -b 115200 -o -D /dev/ttyACM0

Install CMake (at least version 3.13), and GCC cross compiler

# CMake
$ sudo apt install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib


# 
$ git clone https://github.com/raspberrypi/pico-sdk.git


$ cp external/pico_sdk_import.cmake pico_c_cpp_sdk_import.cmake

# PICO_SDK_PATH



hello_world.c

#include <stdio.h>
#include "pico/stdlib.h"

int main() {
    setup_default_uart();
    printf("Hello, world!\n");
    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.13)

# initialise the SDK based on PICO_SDK_PATH
# note: this must happen before project()
include(pico_sdk_import.cmake)

project(my_project)

# initialise the Raspberry Pi Pico SDK
pico_sdk_init()

# rest of your project

# ✅
add_executable(hello_world
    hello_world.c
)

# Add pico_stdlib library which aggregates commonly used features
target_link_libraries(hello_world pico_stdlib)

# create map/bin/hex/uf2 file in addition to ELF.
pico_add_extra_outputs(hello_world)

mkdir build
cd build
cmake ..

$ make hello_world

# debugger
hello_world.elf

# drag and drop
hello_world.uf2

https://www.raspberrypi.com/documentation/microcontrollers/c_sdk.html#your-first-binaries

https://github.com/raspberrypi/pico-sdk/blob/master/src/boards/include/boards

https://github.com/raspberrypi/pico-examples

https://github.com/raspberrypi/pico-examples/blob/master/hello_world/usb/hello_usb.c

Raspberry Pi Pico SDK / Pico C SDK

https://www.raspberrypi.com/documentation/pico-sdk/index_doxygen.html#raspberry-pi-pico-sdk

MicroPython

https://micropython.org/download/rp2-pico/rp2-pico-latest.uf2

https://www.raspberrypi.com/documentation/microcontrollers/micropython.html#drag-and-drop-micropython

Fritzing 可视化电子设计工具 / PCB 电路板设计

Fritzing parts

https://fritzing.github.io/fritzing-parts/

Fritzing 自定义部件

image

Pico / Pico H & Pico W / Pico WH

https://www.raspberrypi.com/documentation/microcontrollers/pico-series.html#pico1

image

https://www.raspberrypi.com/documentation/microcontrollers/pico-series.html#resources-for-non-wireless-raspberry-pi-pico-boards

image

https://www.raspberrypi.com/documentation/microcontrollers/pico-series.html#resources-for-wireless-raspberry-pi-pico-boards

Pico 2 / Pico 2H & Pico 2 W / Pico 2 WH

https://www.raspberrypi.com/documentation/microcontrollers/pico-series.html#pico2

image

https://www.raspberrypi.com/documentation/microcontrollers/pico-series.html#non-wireless_pico2

https://www.raspberrypi.com/documentation/microcontrollers/pico-series.html#resources-for-wireless-raspberry-pi-pico-2-boards

如何给 Fritzing 添加 Parts 中没有 Pico 部件

image

image

  1. 直接使用 Fritzing 打开 ✅

image

image

  1. 在 My Parts 下拉菜单中 Import 对应的

image

image

Fix: Pico 与 Pico H,模型文件 id 冲突问题 ❌

image

  1. 先删除 Parts

image

image

  1. 再重新导入 Parts

✅:不需要处理冲突,因为 Pico 与 Pico H 两个的模型是一模一样的!💩

Pico pinout

https://pico.pinout.xyz/#i=p

refs

https://www.cnblogs.com/xgqfrms/p/23604403#5429722



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2026-11-12 01:56  xgqfrms  阅读(7)  评论(4)    收藏  举报