บทที่ 2
ESP32 Microcontroller
ESP32 คือชิปไมโครคอนโทรลเลอร์แบบ 32-bit สถาปัตยกรรม RISC ที่ทรงพลังและได้รับความนิยมสูงที่สุดในปัจจุบันสำหรับงานด้าน IoT (Internet of Things) และโปรเจกต์อิเล็กทรอนิกส์อัจฉริยะ เนื่องจากมีโมดูลการเชื่อมต่อไร้สายประสิทธิภาพสูงติดตั้งมาให้สำเร็จรูปในตัวชิป
1. ประเภทของชิปซีรีส์ ESP32 (Chip Series)
Espressif Systems ผู้ผลิตชิปได้พัฒนา ESP32 ออกมาหลายรุ่นเพื่อตอบโจทย์งานที่ต่างกัน
- ESP32 (Original): รุ่นคลาสสิก ดึงประสิทธิภาพสูงสุดด้วย Dual-Core (มี 2 แกนประมวลผล) รองรับทั้ง Wi-Fi และ Bluetooth 4.2 / BLE
-ESP32-S Series (เช่น S2, S3): รุ่นอัปเกรดประสิทธิภาพสูง โดยเฉพาะ ESP32-S3 ที่มาพร้อมหน่วยประมวลผลสำหรับงาน AI (Vector Instructions) เหมาะกับงานตรวจจับใบหน้าหรือจดจำเสียง
- ESP32-C Series (เช่น C3, C6): รุ่นประหยัดพลังงานและราคาถูก ใช้สถาปัตยกรรมเปิด RISC-V (Single-Core) โดยในรุ่น C6 จะอัปเกรดให้รองรับ Wi-Fi 6, Zigbee และ Thread เหมาะกับอุปกรณ์ Smart Home ยุคใหม่
- ESP32-H Series: รุ่นที่เน้นการเชื่อมต่อระยะไกลพลังงานต่ำเป็นหลัก โดยรองรับ Bluetooth 5.4 และตระกูล IEEE 802.15.4 (Thread/Zigbee) โดยไม่มี Wi-Fi
โครงสร้างของ ESP32
ESP32 เป็นไมโครคอนโทรลเลอร์แบบ System on Chip (SoC) ที่รวมหน่วยประมวลผล หน่วยความจำ โมดูลสื่อสารไร้สาย และอุปกรณ์ต่อพ่วงต่าง ๆ ไว้ภายในชิปเดียว ทำให้สามารถพัฒนาระบบสมองกลฝังตัว (Embedded System) และระบบอินเทอร์เน็ตของสรรพสิ่ง (IoT) ได้โดยไม่ต้องใช้อุปกรณ์ภายนอกจำนวนมาก
โครงสร้างภายในของ ESP32 ประกอบด้วยส่วนสำคัญ ได้แก่ CPU, RAM, Flash Memory, GPIO และ Peripheral ดังแสดงในภาพรวม
CPU (Central Processing Unit)
CPU หรือหน่วยประมวลผลกลาง เปรียบเสมือน "สมอง" ของ ESP32 ทำหน้าที่ประมวลผลคำสั่งทั้งหมดของโปรแกรม เช่น การคำนวณ การควบคุมอุปกรณ์ และการจัดการการสื่อสาร
ESP32 ใช้สถาปัตยกรรม Xtensa LX6 แบบ 32 บิต และมี 2 แกนประมวลผล (Dual Core) ทำให้สามารถทำงานหลายงานพร้อมกัน (Multitasking) ได้อย่างมีประสิทธิภาพ
คุณสมบัติ
- สถาปัตยกรรม 32 บิต
- Dual Core
- ความเร็วสูงสุด 240 MHz
- รองรับ Floating Point
- รองรับ FreeRTOS
RAM (Random Access Memory)
RAM คือหน่วยความจำชั่วคราวที่ใช้เก็บข้อมูลระหว่างการทำงาน เมื่อปิดไฟเลี้ยงข้อมูลทั้งหมดจะหายไป โดยใช้ RAM สำหรับเก็บข้อมูลของตัวแปรต่าง ๆ
-
Flash Memory
Flash Memory เป็นหน่วยความจำถาวร ใช้เก็บโปรแกรมและข้อมูลต่าง ๆ แม้ว่าจะปิดเครื่อง ข้อมูลก็ยังคงอยู่ Flash ใช้เก็บ โปรแกรมที่เขียน โดยเมือทำการอัพโหลดโปรแกรมจากคอมพิวเตอร์ไปยัง ESP32 โปรแกรมที่เขียนสำหรับการทำงานของ ESP32 จะถูกเก็บไว้ในหน่วยความจำ Flash
GPIO (General Purpose Input Output)
GPIO คือขาสัญญาณที่สามารถกำหนดให้เป็นอินพุตหรือเอาต์พุตได้ตามต้องการ เพื่อการใช้งานเป็น Input สำหรับตรวจจับปุ่มกด หรือจากเซนเซอร์ต่าง ๆ และ Output สำหรับการขับ LED รีเลย์ เป็นต้น โดยESP32 มี GPIO มากกว่า 30 ขา ทำให้สามารถเชื่อมต่ออุปกรณ์ได้หลายชนิดพร้อมกัน
Peripheral
Peripheral คืออุปกรณ์ต่อพ่วงที่อยู่ภายในชิป ช่วยลดความซับซ้อนในการออกแบบวงจรและการเขียนโปรแกรม ESP32 มี Peripheral จำนวนมาก เช่น
The ESP32 peripherals include:
-18 Analog-to-Digital Converter (ADC) channels
-3 SPI interfaces
-3 UART interfaces
-2 I2C interfaces
-16 PWM output channels
-2 Digital-to-Analog Converters (DAC)
-2 I2S interfaces
-10 Capacitive sensing GPIOs
The ADC (analog to digital converter) and DAC (digital to analog converter) features are assigned to specific static pins. However, you can decide which pins are UART, I2C, SPI, PWM, etc – you just need to assign them in the code. This is possible due to the ESP32 chip’s multiplexing feature.
Although you can define the pins properties on the software, there are pins assigned by default as shown in the following figure (this is an example for the ESP32 DEVKIT V1 DOIT board with 36 pins
https://randomnerdtutorials.com/getting-started-with-esp32/#esp32-intro
Additionally, there are pins with specific features that make them suitable or not for a particular project. The following table shows what pins are best to use as inputs, outputs and which ones you need to be cautious.
The pins highlighted in green are OK to use. The ones highlighted in yellow are OK to use, but you need to pay attention because they may have an unexpected behavior mainly at boot. The pins highlighted in red are not recommended to use as inputs or outputs.
https://randomnerdtutorials.com/esp32-pinout-reference-gpios/