# LAB: Basic Analog Input/Output

## **จุดประสงค์**&#x20;

1. &#x20;สามารถอ่านจาก Datasheet ของ sensor เพื่อวิเคราะห์หลักการทำงานสำหรับนำมาใช้ในการเขียนโปรแกรมให้เหมาะสมกับ sensor&#x20;
2. สามารถวัดและอ่านค่าแรงดันของขาอุปกรณ์ด้วย Digital Multimeter ได้&#x20;
3. สามารถเขียนโปรแกรมลง Embedded board เพื่อควบคุมอุปกรณ์ผ่านขา I/O ด้วยสัญญาณ analogได้&#x20;

## Lab 1: การวัดค่าแรงดันขา Output ของ Sensor ที่ส่งสัญญาณแบบ Analog&#x20;

* อ่าน [Datasheet](https://datasheetspdf.com/pdf-file/731248/NTEElectronics/KA2531S28/1) ของ [potentiometer](https://datasheetspdf.com/pdf-file/731248/NTEElectronics/KA2531S28/1) ว่าหน้าที่ของแต่ละใช้งานอย่างไรและต่อ sensor กับ Embedded board&#x20;
* ต่อวงจรและเขียนโปรแกรมให้สามารถรับค่าจาก sensor แบบ Analog
* สังเกตการทำงานของโปรแกรมบน serial monitor เมื่อปรับค่า sensor&#x20;

### **อุปกรณ์ทดลอง**&#x20;

<table><thead><tr><th width="231.69613718078722">Component </th><th>Quantity</th></tr></thead><tbody><tr><td>Arduino mega2560</td><td>1</td></tr><tr><td>myDAQ</td><td>1</td></tr><tr><td>LED</td><td>1</td></tr><tr><td>Potentiometer</td><td>1</td></tr></tbody></table>

![](/files/xQ6hxUaVvNII0lS3zMCt)

### โปรแกรม

1. Arduino IDE
2. โปรแกรม NI ELVISSmx

### **Schematic**&#x20;

![](/files/xEzLhm0lpqQys818zCQD)

### **Circuit Diagram**

![](/files/Ocp6oWFGspoLFPoyI6H4)

### Code

```arduino
// leb 1
int sensorPin = A8;    // connect this pin to potentiometer
int ledPin = 2;      // connect this pin to external LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT); // set this pin to output
  Serial.begin(9600); // start serial monitor
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  sensorValue = map(sensorValue, 0,1023,0,255);
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);
  Serial.println(sensorValue);
  // stop the program for <sensorValue> milliseconds:
  delay(sensorValue);
  // turn the ledPin off:
  digitalWrite(ledPin, LOW);
  // stop the program for for <sensorValue> milliseconds:
  delay(sensorValue);
}
```

* เราสามารถเปิดดูค่า analog ที่อ่านได้ด้วยการเปิด serial monitor ด้วยการกดปุ่มตามรูป

![หน้าต่าง Serial monitor](/files/mYJ4GGxSXk74v1rF7osM)

### บันทึกผลการทดลอง

1. ทดลองปรับค่า potentiometer สังเกตุค่าการเปลี่ยนแปลงบน Serial monitor
2. วัดค่าแรงดันที่ออกจาก Output pin ของ potentiometer บันทึกการเปลี่ยนแปลงและค่าสูงสุด-ต่ำสุด ที่แสดงบน  Serial monitor และค่าแรงดันที่วัดได้&#x20;
3. บันทึกผลการทดลอง template ที่ได้รับมอบหมาย&#x20;

## Lab 2:การทดลองใช้งาน [potentiometer](https://datasheetspdf.com/pdf-file/731248/NTEElectronics/KA2531S28/1) เป็นตัวควบคุม Analog output&#x20;

&#x20;      การทดลองใช้งาน potentiometer เป็นตัวควบคุม Analog output โดยต่อชุดทดลองในแบบเดียวกับการทดลองที่ 1 และใช้ชุดคำสั่งดังต่อไปนี้

### Code

```
// lab 2
int sensorPin = A8;    // connect this pin to potentiometer
int ledPin = 2;      // connect this pin to external LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT); // set this pin to output
  Serial.begin(9600); // start serial monitor
}


void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  sensorValue = map(sensorValue, 0,1023,0,255);
  // turn the ledPin on
  analogWrite(ledPin, sensorValue);
  Serial.println(sensorValue); // bright LED with read value
  
  
}
```

### บันทึกผลการทดลอง

1. ทดลองปรับค่า potentiometer สังเกตุค่าการเปลี่ยนแปลงบน Serial monitor
2. วัดค่าแรงดันที่ออกจาก Output pin ของ potentiometer บันทึกการเปลี่ยนแปลงและค่าสูงสุด-ต่ำสุด ที่แสดงบน  Serial monitor และค่าแรงดันที่วัดได้&#x20;
3. บันทึกผลการทดลอง template ที่ได้รับมอบหมาย&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aic-eec.com/embedded-systems/resources/basic-hardware-and-firmware/lab1-oscilloscope/basic-digital-and-analog-i-o/lab-basic-analog-input-output.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
