风筝
发表于: 2018-8-9 16:43:21 | 显示全部楼层

我们可以将小型步进电机直接连接到Arduino开发板,以便非常精确地控制轴位置。

由于步进电机使用反馈来确定轴的位置,我们可以非常精确地控制该位置。因此,步进电机经常用于控制物体的位置,旋转物体,移动机器人的腿、手臂,精确移动传感器等。步进电机体积小,并且由于它们内置电路控制其自身的运动,因此可以直接连接到Arduino开发板。


大多数步进电机有以下三根连接线:

●    黑色/棕色的地线。

●    红色电源线(5V)。

●    黄色或白色PWM线。


在本篇文章中,我们将电源和地引脚直接连接到Arduino开发板的5V和GND引脚。 PWM输入连接到Arduino的一个数字输出引脚。


实验1

在本实验中,Ardunio开发板通过PWM引脚按照给定角度旋转步进电机。


需要的硬件

●    TowerPro SG90步进电机

●    Arduino Mega2560开发板

●    连接导线


接线图

步进电机的最佳之处在于它可以直接连接到Arduino。下图显示了步进电机与Arduino的连接方法:

●    步进电机的红线 - Arduino的5V引脚

●    步进电机的棕线 - Arduino的接地引脚

●    步进电机的黄线 - Arduino的PWM(9)引脚


注意:请勿尝试用手旋转步进电机,否则可能会损坏电机。

servo-arduino_.jpg


代码

程序开始运行时,步进电机将从0度缓慢旋转180度,每次一度。当电机旋转180度时,它将开始向另一个方向旋转,直到它返回原位。

  1. #include              //Servo library

  2. Servo servo_test;                    //initialize a servo object for the connected servo  
  3.                
  4. int angle = 0;   

  5. void setup()
  6. {
  7.   servo_test.attach(9);                  // attach the signal pin of servo to pin9 of arduino
  8. }
  9.   
  10. void loop()
  11. {
  12.   for(angle = 0; angle < 180; angle += 1)          // command to move from 0 degrees to 180 degrees
  13.   {                                 
  14.     servo_test.write(angle);                       //command to rotate the servo to the specified angle
  15.     delay(15);                       
  16.   }

  17.   delay(1000);
  18.   
  19.   for(angle = 180; angle>=1; angle-=5)     // command to move from 180 degrees to 0 degrees
  20.   {                                
  21.     servo_test.write(angle);              //command to rotate the servo to the specified angle
  22.     delay(5);                       
  23.   }

  24.     delay(1000);
  25. }
复制代码

实验2

该实验与实验1基本相同,只是我们增加了一个位置控制电位器。 Arduino将读取电位器中间引脚上的电压,然后调整步进电机轴的位置。


需要的硬件

●    TowerPro SG90步进电机

●    Arduino Mega2560开发板

●    20kΩ电位器

●    面包板

●    连接导线


接线图

连接电路如下图所示:

●    步进电机的红线 - Arduino的5V

●    步进电机的棕线 - Arduino的接地引脚

●    步进电机的黄线 - Arduino的PWM(9)引脚

●    电位器的1脚 - Arduino的5V

●    电位器的3脚 - Arduino的接地引脚

●    电位器的2脚 - Arduino的模拟输入(A0)引脚

servo-arduino-potentiometer_1.jpg


代码

程序启动后,旋转电位器应会使得步进电机的轴旋转。

  1. #include         //Servo library

  2. Servo servo_test;            //initialize a servo object for the connected servo  
  3.                
  4. int angle = 0;   
  5. int potentio = A0;      // initialize the A0analog pin for potentiometer


  6. void setup()
  7. {
  8.   servo_test.attach(9);         // attach the signal pin of servo to pin9 of arduino
  9. }

  10. void loop()
  11. {
  12.   angle = analogRead(potentio);            // reading the potentiometer value between 0 and 1023
  13.   angle = map(angle, 0, 1023, 0, 179);     // scaling the potentiometer value to angle value for servo between 0 and 180)
  14.   servo_test.write(angle);                   //command to rotate the servo to the specified angle
  15.   delay(5);            
  16. }  
复制代码

跳转到指定楼层
cmmjava
发表于: 2022-5-12 16:51:43 | 显示全部楼层

TowerPro SG90步进电机, 是不对的。TowerPro SG90是伺服电机,不是步进电机。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1480



手机版|

GMT+8, 2024-4-19 22:33 , Processed in 0.134507 second(s), 6 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表