0Pricing
Arduino & IoT Academy · บทเรียน

ควบคุม LED จากแอปโทรศัพท์

ส่งคำสั่งผ่าน BLE ไปยังบอร์ดของคุณ

ควบคุม LED จากแอปโทรศัพท์ เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Arduino & IoT Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Your First BLE Control

Time to put it together: tap a button on your phone and watch an LED turn on. This is BLE control in action. 💡

The Plan

The phone writes a value to a characteristic, your board reads it, and your sketch switches the LED to match. Simple and powerful.

Prepare the LED Pin

In setup, declare the LED pin as an output so your board can drive it. This is the same pinMode you already know.

pinMode(2, OUTPUT);

Make It Writable

The control characteristic needs PROPERTY_WRITE so the phone is allowed to send commands to it. Read-only would block control.

svc->createCharacteristic(uuid,
  BLECharacteristic::PROPERTY_WRITE);

Listen for Writes

Attach a callback so your code runs the instant the phone writes. The onWrite method becomes your command handler.

Read the Command

Inside the callback, fetch what the phone sent with getValue. Maybe it is the text 1 for on or 0 for off.

std::string v = c->getValue();

Act on the Value

Compare the command, then drive the pin. A 1 turns the LED on, anything else turns it off. That is the whole logic.

digitalWrite(2, v == "1" ? HIGH : LOW);

Confirm Back

It helps to send the new state back so the app stays in sync. A short Serial print also confirms the command really arrived.

Serial.println("LED set");

Use a Generic App

No need to build an app yet. A tool like nRF Connect lets you connect and write values to test your control instantly.

Connect and Send

In the app, connect to your device, open the writable characteristic, and send 1. The onboard LED should light up right away.

From Here to a Real App

The same write idea powers a full custom app. Once this works, you can control motors, relays, or anything from your phone.

Quick Check

You want the phone to send an on or off command to your board.

Recap

A writable characteristic plus a callback turns phone taps into real-world action. You just built remote LED control over BLE. 🎉

คำถามที่พบบ่อย

บทเรียน “ควบคุม LED จากแอปโทรศัพท์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ควบคุม LED จากแอปโทรศัพท์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ควบคุม LED จากแอปโทรศัพท์”

ส่งคำสั่งผ่าน BLE ไปยังบอร์ดของคุณ คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “ควบคุม LED จากแอปโทรศัพท์” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม

ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. Bluetooth แบบคลาสสิกเทียบกับ BLE
  2. ประกาศบริการ BLE
  3. อ่านและเขียนคุณลักษณะ
  4. ควบคุม LED จากแอปโทรศัพท์
← กลับไปที่ Arduino & IoT Academy