ដំឡើង Arduino-ESP32 Core 3.x និង Upload កូដដំបូង
បន្ថែម Board Package របស់ Espressif ក្នុង Arduino IDE 2.x, ជ្រើស ESP32 Dev Module ហើយ Upload កូដបង្ហាញព័ត៌មានឈីប និង Blink ដោយ millis()។
- ពន្យល់ភាពខុសគ្នារវាង ESP32 និង Arduino Uno (CPU, RAM, Wi-Fi, 3.3V Logic)
- ដំឡើង esp32 by Espressif Systems 3.x, ជ្រើស ESP32 Dev Module និង Upload ជោគជ័យ
- ដោះស្រាយ Driver CP2102/CH340, ខ្សែ USB, Port និង BOOT Mode ពេល Upload បរាជ័យ
ក្នុង Arduino IDE 2.x ចូល File → Preferences ហើយបិទភ្ជាប់ URL https://espressif.github.io/arduino-esp32/package_esp32_index.json ក្នុងប្រអប់ Additional boards manager URLs។ បន្ទាប់មកបើក Boards Manager ស្វែងរក esp32 ហើយដំឡើង esp32 by Espressif Systems កំណែ 3.x។ កុំច្រឡំជាមួយ “Arduino ESP32 Boards” របស់ Arduino ដែលសម្រាប់តែបន្ទះ Arduino Nano ESP32 ប៉ុណ្ណោះ។
ជ្រើស Tools → Board → esp32 → ESP32 Dev Module (ប្រើបានជាមួយ DevKit V1 / WROOM-32 ស្ទើរតែទាំងអស់) រួចជ្រើស Port។ ការកំណត់លំនាំដើមគឺល្អរួចហើយ៖ Upload Speed 921600, CPU Frequency 240MHz (WiFi/BT), Flash Size 4MB (32Mb)។ ប្រសិនបើ Upload បរាជ័យញឹកញាប់ (ខ្សែវែង ឬ USB Hub) សូមបន្ថយ Upload Speed មក 115200។
Core 3.x ផ្អែកលើ ESP-IDF 5.x ហើយបានផ្លាស់ប្ដូរ API មួយចំនួន ជាពិសេស LEDC PWM ដែលយើងនឹងរៀនក្នុង Module 3។ កូដចាស់ៗតាមអ៊ីនធឺណិតដែលប្រើ ledcSetup() និង ledcAttachPin() នឹង Compile មិនជាប់ទៀតទេ។ កូដខាងក្រោមប្រើ #error ដើម្បីបញ្ឈប់ការ Compile ភ្លាមៗ ប្រសិនបើ Core របស់អ្នកនៅជាកំណែ 2.x — វិធីនេះសន្សំពេលដោះស្រាយ Error ច្រឡំៗ។
ESP32 ប្រើ Baud Rate 115200 ជាទូទៅ (Boot ROM ក៏បោះសារ Boot ដូចជា rst:0x1 (POWERON_RESET) នៅ 115200 ដែរ)។ បើក Serial Monitor ហើយចុចប៊ូតុង EN ម្ដង អ្នកនឹងឃើញព័ត៌មានឈីប។ LED លើបន្ទះ (ភាគច្រើន GPIO2) ភ្លឹបភ្លែតៗដោយ millis() ជំនួស delay() ដើម្បីឱ្យ loop() នៅទំនេរធ្វើការងារផ្សេងទៀត ដូចជារាយការណ៍ Uptime រៀងរាល់ 5 វិនាទី។
- Board Package៖ esp32 by Espressif Systems 3.x, Board៖ ESP32 Dev Module
- Serial Monitor ត្រូវកំណត់ 115200 baud ដើម្បីអានសារ Boot និងកូដរបស់អ្នក
- ប្រើ millis() ជំនួស delay() តាំងពីកូដដំបូង ដើម្បីត្រៀមសម្រាប់ Wi-Fi និងការងារច្រើនក្នុងពេលតែមួយ
// hello_esp32.ino - first upload on ESP32 (Arduino-ESP32 core 3.x)
// Board: Tools > Board > esp32 > "ESP32 Dev Module", Serial Monitor @ 115200 baud
#if ESP_ARDUINO_VERSION_MAJOR < 3
#error "This course targets Arduino-ESP32 core 3.x. Update 'esp32 by Espressif Systems' in Boards Manager."
#endif
constexpr uint8_t PIN_LED = 2; // on-board LED on most DevKit V1 boards
constexpr uint32_t BLINK_INTERVAL_MS = 500;
constexpr uint32_t REPORT_INTERVAL_MS = 5000;
uint32_t lastBlinkMs = 0;
uint32_t lastReportMs = 0;
bool ledOn = false;
void printChipInfo() {
Serial.println();
Serial.println("=== Hello, ESP32! ===");
Serial.printf("Chip model : %s (revision %d)\n", ESP.getChipModel(), ESP.getChipRevision());
Serial.printf("CPU : %d cores @ %lu MHz\n", ESP.getChipCores(), (unsigned long)ESP.getCpuFreqMHz());
Serial.printf("Flash size : %lu MB\n", (unsigned long)(ESP.getFlashChipSize() / (1024UL * 1024UL)));
Serial.printf("Core : Arduino-ESP32 %d.%d.%d\n",
ESP_ARDUINO_VERSION_MAJOR, ESP_ARDUINO_VERSION_MINOR, ESP_ARDUINO_VERSION_PATCH);
}
void setup() {
Serial.begin(115200);
delay(300); // let the USB-UART bridge settle after reset
pinMode(PIN_LED, OUTPUT);
printChipInfo();
}
void loop() {
const uint32_t now = millis();
// Task 1: blink without blocking
if (now - lastBlinkMs >= BLINK_INTERVAL_MS) {
lastBlinkMs = now;
ledOn = !ledOn;
digitalWrite(PIN_LED, ledOn ? HIGH : LOW);
}
// Task 2: periodic status report
if (now - lastReportMs >= REPORT_INTERVAL_MS) {
lastReportMs = now;
Serial.printf("Uptime %lu s | free heap %lu bytes\n",
(unsigned long)(now / 1000), (unsigned long)ESP.getFreeHeap());
}
}ការពន្យល់៖ កូដនេះពិនិត្យកំណែ Core ពេល Compile, បោះព័ត៌មានឈីប (Model, Cores, MHz, Flash) និងដំណើរការការងារពីរព្រមគ្នា (Blink + Report) ដោយ millis()។ បើបន្ទះរបស់អ្នកគ្មាន LED លើ GPIO2 សូមភ្ជាប់ LED + 220 Ω ទៅ GPIO2 ឬប្ដូរ PIN_LED។
សំណួរវាស់សមត្ថភាព (Knowledge Check)
ឆ្លើយគ្រប់សំណួរ រួចចុច «ពិនិត្យចម្លើយ»។ បើត្រឹមត្រូវ ≥ ៨០% មេរៀននេះនឹងត្រូវបានកត់ត្រាថាបានបញ្ចប់ស្វ័យប្រវត្តិ។
- Arduino-ESP32 — Installing docs.espressif.comការណែនាំផ្លូវការសម្រាប់បន្ថែម Board Manager URL និងដំឡើង Core ក្នុង Arduino IDE។
- Arduino IDE 2 — Getting Started docs.arduino.ccរបៀបប្រើ Boards Manager, ជ្រើស Port, Upload និង Serial Monitor។