For the complete documentation index, see llms.txt. This page is also available as Markdown.

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 ├────────────────────┼─────────────→ 0

Resolution 12-bit: 2^12 = 4096 ระดับ

สูตรแปลงเป็น mV:

mV=ADC_count×VREF_mV4096mV = \frac{ADC\_count \times VREF\_mV}{4096}

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

  1. 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

Hint

ตัวอย่าง

  1. Threshold Alert : แจ้งเตือนเมื่อ POT เกิน threshold ที่กำหนด

Hint

ตัวอย่าง


Last updated

Was this helpful?