Arduino中文教程----认识SG90舵机模块并制作自动喂食器

in #utopian-io7 years ago (edited)

What Will I Learn?

  • 认识Arduino 的SG90舵机模块

  • 编写程序驱动SG90舵机模块转动

  • 制作自动喂食器

    GIF.gif

Requirements

  • Arduino UNO

    图片.png

  • Arduino IDE开发环境

    图片.png

  • Arduino SG90舵机模块

    图片.png

Difficulty

  • 基础

Tutorial Contents

一、认识Arduino 的SG90舵机模块及应用

舵机,也叫做伺服马达,内部控制系统是具有闭环控制系统的机电结构。

舵机由以下部件组成:

  • 外壳
  • 电路板
  • 无核心马达
  • 齿轮
  • 位置检测器

SG90舵机模块的的原理主要是由核心闭环控制系统发出PWM(脉冲宽度调制)信号给舵机,然后信号在电路板上得到IC处理之后计算出转动的角度, 根据设定的角度驱动无核心马达转动,通过减速齿轮给摆臂以动力,在此同时电位器返回当前的位置信号,判断是否已经到达设定位置。

SG90舵机模块的特性如下:

  • 只能旋转180度
  • 尺寸:22.3 X 11.8 X 26.3 mm
  • 操作速度:0.12秒/60度(4.8V);0.1秒/60度(6.0V)(无负载条件下)
  • 扭矩大小:1.3kg·cm(4.8V);1.5kg·cm(6.0V)
  • 操作温度:-30~+60°C
  • 正常工作电压:4.8V~6V

二、编写程序驱动SG90舵机模块转动

连接Arudino UNO与SG90舵机模块

图片.png

SG90舵机模块出厂时配有三条不同颜色的接线已连接在舵机控制电路上,分别是:

  • 棕色 : 接地线

  • 红色 : 接电源正极线

  • 橙色 : 模块信号输出引脚

Arduino UNOSG90舵机模块
GND棕色接线
5V红色接线
D10橙色接线

图片.png

图片.png

编程驱动SG90舵机模块转动

  • 引入SG90舵机模块库文件

    #include<Servo.h>
    
  • 实例化一个舵机对象

    Servo myservo;
    
  • 定义变量

    #define PIN_SERVO 10  
    
  • setup()初始化

    void setup() {
     Serial.begin(9600);         //使用9600速率进行串口通讯
    }
    
  • loop()主体函数

    void loop()  
    {  
      myservo.write(0);  
      delay(1000);  
      myservo.write(80);  
      delay(1000);  
      myservo.write(160);  
      delay(1000);  
      myservo.write(80);  
      delay(1000);  
      myservo.write(0);  
      delay(1000);  
    }  
    

连接Arduino UNO至电脑

图片.png

编译并上传程序至Arduino UNO

图片.png

测试效果:

GIF.gif


三、制作自动喂食器

  • DIY装饲料的容器

    找一个曲奇罐子或者其他罐子,最好是呈圆柱状的

    然后在罐子的一边开一个小口子,面积不用太大,如下图大小即可

    将罐子的底部与舵机的舵片粘在一起固定好

  • 根据罐子开口的位置,修改程序

    #include <Servo.h>  
    
    #define PIN_SERVO 10  
    const int  buttonPin = 2;
    int buttonState = 0;
    int lastButtonState = 0;
    Servo myservo;  
    
    void setup()  
    {  
      pinMode(buttonPin, INPUT);
      myservo.attach(PIN_SERVO);  
    }  
    
    void loop()  
    {  
      buttonState = digitalRead(buttonPin);
      if (buttonState != lastButtonState) {
          if(buttonState == HIGH){
          myservo.write(0);  
          delay(1000);  
          myservo.write(160);  
          delay(1000);  
          myservo.write(0);  
          delay(1000);  
        }
      }
      lastButtonState = buttonState;
    }  
    
  • 并上传程序测试效果

    GIF.gif

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @cha0s0000 I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x