问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

Arduino控制MG996R高扭矩伺服电机教程

创作时间:
作者:
@小白创作中心

Arduino控制MG996R高扭矩伺服电机教程

引用
CSDN
1.
https://blog.csdn.net/acktomas/article/details/139993620

本教程将介绍如何使用Arduino控制MG996R高扭矩伺服电机。通过本教程,你将学习到所需硬件、电机引脚介绍、接线图、Arduino代码示例以及代码说明等。

所需硬件

  • 1 × Arduino UNO 或 Genuino UNO
  • 1 × USB 2.0 电缆 A/B 型
  • 1 × MG996R 伺服电机
  • 1 × 跳线
  • 1 × (可选) 9V 电源适配器
  • 1 × (推荐) 螺钉接线端子屏蔽层
  • 1 × (可选) Arduino Uno 透明亚克力外壳

关于伺服电机

MG996R 伺服电机是一款高扭矩伺服电机,能够举起高达15kg的重量。电机可以将其手柄旋转 0° 至 180°,从而提供对角度位置的精确控制。有关伺服电机的基本信息,请参阅Arduino - 伺服电机教程。

引脚排列

本例中使用的MG996R伺服电机包括三个引脚:

  • VCC 引脚:(通常为红色)需要连接到 VCC (4.8V – 7.2V)
  • GND 引脚:(通常为黑色或棕色)需要连接到 GND (0V)
  • 信号引脚:(通常为黄色或橙色)接收来自Arduino引脚的PWM控制信号。

接线图

由于MG996R是一款高扭矩伺服电机,因此它消耗了大量的动力。我们不应该通过Arduino的5v引脚为该电机供电。相反,我们需要使用MG996R伺服电机的外部电源。

Arduino代码

/*
 * Created by ArduinoGetStarted.com
   *
 * This example code is in the public domain
   *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-mg996r
   */
#include <Servo.h>
Servo servo;  // create servo object to control a servo
void setup() {
  servo.attach(9);  // attaches the servo on pin 9 to the servo objectư
  servo.write(0);   // rotate slowly servo to 0 degrees immediately
}
void loop() {
  for (int angle = 0; angle <= 180; angle += 1) {  // rotate slowly from 0 degrees to 180 degrees, one by one degree
    // in steps of 1 degree
    servo.write(angle);  // control servo to go to position in variable 'angle'
    delay(10);         // waits 10ms for the servo to reach the position
  }
  for (int angle = 180; angle >= 0; angle -= 1) {  // rotate from 180 degrees to 0 degrees, one by one degree
    servo.write(angle);                        // control servo to go to position in variable 'angle'
    delay(10);                               // waits 10ms for the servo to reach the position
  }
}

快速步骤

  • 通过USB线将Arduino连接到PC
  • 打开Arduino IDE,选择正确的板卡和端口
  • 复制上面的代码并使用Arduino IDE打开
  • 单击Arduino IDE上的“上传”按钮,将代码上传到Arduino
  • 查看结果:伺服电机从 0 到 180° 缓慢旋转,然后从 180° 缓慢旋转到 0°

代码说明

阅读代码注释行中的逐行说明!

如何控制伺服电机的速度

通过使用 map() 和 millis() 函数,我们可以在不阻塞其他代码的情况下平稳地控制伺服电机的速度

/*
 * Created by ArduinoGetStarted.com
   *
 * This example code is in the public domain
   *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-mg996r
   */
#include <Servo.h>
Servo myServo;
unsigned long MOVING_TIME = 3000; // moving time is 3 seconds
unsigned long moveStartTime;
int startAngle = 30; // 30°
int stopAngle  = 90; // 90°
void setup() {
  myServo.attach(9);
  moveStartTime = millis(); // start moving
  // TODO: other code
}
void loop() {
  unsigned long progress = millis() - moveStartTime;
  if (progress <= MOVING_TIME) {
    long angle = map(progress, 0, MOVING_TIME, startAngle, stopAngle);
    myServo.write(angle); 
  }
  // TODO: other code
}

函数参考

  • map()
  • millis()
  • Servo.attach()
  • Servo.write()
  • Servo.writeMicroseconds()
  • Servo.read()
  • Servo.attached()
  • Servo.detach()
© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号