Potentiometer (ADC)
Lab 3: Potentiometer (ADC) — อ่านค่า Analog
วัตถุประสงค์
Analog Input: อ่านค่าแบบ Analog (ไม่ใช่แค่ 0/1)
ADC: เข้าใจหลักการแปลง Analog → Digital
Scaling: แปลงค่า ADC เป็นหน่วยที่เข้าใจได้
จะได้เรียนรู้อะไร
ADC Concept: 12-bit resolution (0-4095)
Unit Conversion: ADC counts → millivolts → percentage
Periodic Reading: อ่านค่าเป็นช่วงเวลา
หลักการ ADC
Analog Input ┌─────────┐ Digital Output
(0 - 1.8V) ────► │ ADC │ ────► (0 - 4095)
└─────────┘ 12-bit
Analog Input (0V - 1.8V) ADC Digital Output (0 - 4095)
│ │ │
1.8V ├────────────────────┼─────────────→ 4095
0.9V ├────────────────────┼─────────────→ 2048
0.0V ├────────────────────┼─────────────→ 0Resolution 12-bit: 2^12 = 4096 ระดับ
สูตรแปลงเป็น mV:
Hardware Info
Component
Pin
หมายเหตุ
Potentiometer (POT)
P15_1
0-1800mV range
ADC Reference
Internal
1.8V
Lab 3.1 : แก้ไข Macros เดิมจาก
เป็น Macros ใหม่ , เพิ่ม Variables และ ชื่อฟังก์ชันเพิ่มเติม ดังนี้
สร้างฟังก์ชัน init_adc()
สร้างฟังก์ชัน read_potentiometer_mv()
สร้างฟังก์ชัน get_pot_percentage()
ปรับปรุง main() ใหม่
ตัวอย่าง Output บน Serial Terminal
ADC Potentiometer Flowcharts
Overall Program Flow and init_adc() Flowchart

read_potentiometer_mv() Flowchart
get_pot_percentage() Flowchart
ADC Conversion Diagram
Program Code ทั้งหมด
✍️ Exercise
LED Brightness Indicator : ใช้ POT ควบคุมความเร็วการกระพริบ (Blink Rate) ของ LED
0-25%: Blink Rate --> 1000 ms
25-50%: Blink Rate --> 500 ms
50-75%: Blink Rate --> 250 ms
75-100%: Blink Rate --> 100 ms
Threshold Alert : แจ้งเตือนเมื่อ POT เกิน threshold ที่กำหนด
Last updated