TShopping

 找回密碼
 註冊
搜索
查看: 5622|回復: 0

[教學] Arduino ESP32 如何 鍵盤控制 伺服馬達

[複製鏈接]
發表於 2020-5-26 23:20:35 | 顯示全部樓層 |閱讀模式
 
Push to Facebook
影片



伺服馬達接線圖

Arduino ESP32 鍵盤 伺服馬達

Arduino ESP32 鍵盤  伺服馬達


黃線 GPIO27紅線5V 黑線 GND
ESP32 電力只能推動一個馬達,如果要推動兩個馬達就要外接電源了

程式碼
  1. #include <Servo.h>
  2. Servo myservo;  // 建立伺服馬達控制

  3. // 伺服馬達的連接 GPIO
  4. static const int servoPin = 27;
  5. int pos = 0;
  6. void setup() {
  7.   // put your setup code here, to run once:
  8.   myservo.attach(servoPin);  // 將伺服馬達連接的GPIO pin連接伺服物件
  9.   Serial.begin(115200);//序列阜連線速率
  10. }

  11. void loop() {
  12.   // put your main code here, to run repeatedly:
  13.   if(Serial.available()){                 //
  14.     int num = Serial.parseInt();     // case 前置 num(數字鍵)
  15.    
  16.     switch(num) {                            //   
  17.    
  18.       case 1 :   //1~9
  19.         for(pos = 0; pos < 180; pos += 1) // 一度一度由 0 度旋轉到 180 度
  20.         myservo.write(pos);
  21.         delay(200);
  22.         break;
  23.       
  24.       case 2 : // 1~9
  25.         for(pos = 180; pos>=1; pos-=1) // 一度一度由 180 度旋轉到 0 度
  26.         myservo.write(pos);
  27.         delay(200);
  28.         break;
  29.     }
  30.   }
  31. }
複製代碼


注意!
寫入時如果出現了下方狀況

Arduino ESP32 鍵盤 伺服馬達

Arduino ESP32 鍵盤  伺服馬達

解決方法
先到打開伺服esp32網址(上方網址)

Arduino ESP32 鍵盤 伺服馬達

Arduino ESP32 鍵盤  伺服馬達

解壓檔案

Arduino ESP32 鍵盤 伺服馬達

Arduino ESP32 鍵盤  伺服馬達

找到arduino 資料夾 libraries 貼上 ServoESP32-master 資料夾 即可

Arduino ESP32 鍵盤 伺服馬達

Arduino ESP32 鍵盤  伺服馬達

開啟監控視窗
輸入 1 或 2 ,步進馬達就會轉180度

Arduino ESP32 鍵盤 伺服馬達

Arduino ESP32 鍵盤  伺服馬達

文章出處:網頁設計,網站架設 ,網路行銷,網頁優化,SEO - NetYea 網頁設計


 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-3-28 19:13 , Processed in 0.073548 second(s), 26 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表