กลับไปหน้ารวมไฟล์
easiest-way-to-control-your-arduino-with-a-mobile-device-9251c8.md

ชื่อโปรเจกต์: วิธีที่ง่ายที่สุดในการควบคุม Arduino ของคุณด้วยอุปกรณ์มือถือ!

การเชื่อมต่อ Serial โดยตรง: สมาร์ทโฟน Bluetooth สู่ Arduino

โปรเจกต์ Wi-Fi IoT มักจะตั้งค่ายุ่งยากอย่างไม่น่าเชื่อ โดยต้องกำหนดค่า IP Addresses, router firewalls และ MQTT Cloud dashboards โปรเจกต์ การควบคุมอุปกรณ์มือถือที่ง่ายที่สุด นี้อาศัยความเรียบง่ายดิบ ๆ ของ Bluetooth Classic / BLE (Bluetooth Low Energy) เป็นหลัก! ด้วยการใช้โมดูล HC-05 ราคาถูกอย่างเหลือเชื่อ หรือโมดูล HM-10 ขั้นสูง, Arduino จะทำงานเหมือนกับ Wireless Serial Monitor ทุกประการ เปิด Bluetooth Terminal App ฟรีบนอุปกรณ์ Android ใดก็ได้ กดเชื่อมต่อ แล้วพิมพ์ "1" ลงในโทรศัพท์อย่างรวดเร็ว จะส่งอักขระ ASCII ตรง ๆ ข้ามย่านความถี่ RF 2.4GHz ตรงไปยัง pin ของ Arduino ทันที!

ac_light_relay_ir_remote_arduino_1772681534011.png

โปรโตคอลการบายพาสทางกายภาพด้วย SoftwareSerial

คุณต้องไม่ต่อ TX และ RX pin ของโมดูล Bluetooth เข้ากับ Pin 0 และ 1 ของ Arduino โดยตรง! หากคุณทำเช่นนั้น คุณจะไม่สามารถอัปโหลดโค้ดใหม่จาก PC ได้เลย เพราะชิป Bluetooth จะไปบล็อกชิป USB อย่างรุนแรง!

  1. คุณใช้สถาปัตยกรรมจำลองของ <SoftwareSerial.h>!
  2. คุณประกาศ Pins 10 และ 11 ให้เป็นพอร์ต Serial ฮาร์ดแวร์เสมือน
  3. โมดูล Bluetooth ที่ทรงพลังอย่างเหลือเชื่อจะสื่อสารกับ Arduino ผ่านช่องทางด้านข้างทั้งหมด ปล่อยให้ Main Serial.println() bus ว่างอย่างเต็มที่สำหรับการดีบักบน PC!
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX (Connects to BT TX), TX (Connects to BT RX - via divider!)

int LED_PIN = 13;

void setup() {
  Serial.begin(9600);   // The PC Debugger Pipeline!
  BTSerial.begin(9600); // The Bluetooth 2.4GHz RF Pipeline!
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  if (BTSerial.available()) { // The exact second a character is received from Android!
    char incomingByte = BTSerial.read();
    
    if (incomingByte == '1') {
      digitalWrite(LED_PIN, HIGH);
      BTSerial.println("Arduino: LED IGNITED NATIVELY!"); // Send a text message BACK to the Phone!
    } 
    else if (incomingByte == '0') {
      digitalWrite(LED_PIN, LOW);
      BTSerial.println("Arduino: LED EXTINGUISHED!");
    }
  }
}

อันตรายจากการทำลาย Logic 5V (Voltage Divider)

โมดูล Bluetooth ส่วนใหญ่ (HC-05, HM-10, JDY-31) ทำงานภายในด้วย Logic 3.3 โวลต์!

  • แม้ว่าโดยปกติจะมีวงจร regulator 5V ในตัวที่จ่ายไฟ VCC ให้ แต่ RX (Receive) Pin จริง ๆ นั้นไม่มีการป้องกันใด ๆ เลย!
  • หากขา Transmit (TX Pin 11) ของ Arduino SoftwareSerial ส่งสัญญาณ 5V HIGH แบบคลื่นสี่เหลี่ยมที่รุนแรงตรงไปยัง RX pin ของ Bluetooth จะทำให้ชิป Bluetooth พังอย่างสมบูรณ์!
  • คุณต้องต่อวงจร Voltage Divider โดยเฉพาะ! (ตัวต้านทาน 1K-Ohm จาก Arduino ไปยัง BT RX, และตัวต้านทาน 2K-Ohm จาก BT RX ไปยัง GND) เพื่อลดแรงดันไฟฟ้าให้ปลอดภัยที่ 3.3V!

ฮาร์ดแวร์สำหรับเชื่อมต่อสมาร์ทโฟนแบบไร้สาย

  • Arduino Uno/Nano (จัดการการแปลงอักขระแบบ dual-serial ได้อย่างราบรื่นและเป็นธรรมชาติ!)
  • HC-05 (Classic OS) หรือ HM-10 (BLE iOS Compatible) (Bluetooth transceivers)
  • Voltage Divider Array (ตัวต้านทาน 1K และ 2K Ohm จำเป็นอย่างยิ่งสำหรับการใช้งานฮาร์ดแวร์!)
  • Android Smartphone หรือ iPhone (ใช้แอปพลิเคชัน "Bluetooth Serial Terminal" ทั่วไปที่เรียบง่ายอย่างเหลือเชื่อ หลีกเลี่ยงความจำเป็นในการเขียนโค้ด UI ของ Java/Swift โดยเฉพาะ!)

ข้อมูล Frontmatter ดั้งเดิม

title: "Easiest Way To Control Your Arduino With A Mobile Device!"
description: "Smartphone BLE Integration! Entirely sidestep cumbersome TCP/IP networking matrices and deploy raw point-to-point SPP (Serial Port Profile) architectures using HM-10 Bluetooth Low Energy, violently piping Android Strings into physical GPIO pins natively."
category: "Wireless & IoT"
difficulty: "Beginner"