🎛️🤖使用电位器控制 SG90 伺服器


🔹 所需组件

✅ Arduino(Uno/Nano/等)
✅ SG90伺服电机
✅ 10kΩ电位器
✅ 跳线和面包板


🔹 电路连接

元件Pin连接至
SG90伺服红色(VCC)5V
棕色的 (GND)GND
橙色(信号)Pin 9 (脉宽调制)
电位器左 PinGND
中间 PinA0 (模拟输入)
右 Pin5V

🔹 Arduino 代码(带电位器的伺服控制)

#include <Servo.h>

Servo myServo;  // Create servo object
int potPin = A0;  // Potentiometer connected to A0
int potValue;     // Variable to store potentiometer value

void setup() {
    myServo.attach(9);  // Servo signal wire connected to Pin 9
}

void loop() {
    potValue = analogRead(potPin);  // Read potentiometer value (0-1023)
    int angle = map(potValue, 0, 1023, 0, 180);  // Convert to 0-180°
    
    myServo.write(angle);  // Move servo to mapped angle
    delay(15);  // Small delay for smooth movement
}

🔹 运行代码的步骤

1️⃣ 按照电路图连接所有东西。
2️⃣ 将代码上传到您的 Arduino。
3️⃣ 转动电位器并观察伺服器平稳移动! 🎛️🤖


🔹 下一步是什么?

📡广播信号——放大连接。

发表评论