> For the complete documentation index, see [llms.txt](https://docs.aic-eec.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](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).

# LAB: Basic Analog Input/Output

การวัดค่าแรงดันของวงจรและเขียนโปรแกรมสำหรับรับและส่งข้อมูลAnalogผ่านขา I/O ของ Embedded board

## **จุดประสงค์**&#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>

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FUyNHjpbtrb1DSrDvMUnS%2FUntitled.png?alt=media\&token=65f9c42f-b61b-438c-9026-350b77d469fa)

### โปรแกรม

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

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

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2F2OtVzr5voqHsB9hM8J3m%2FScreenshot%202021-11-29%20145844.png?alt=media\&token=b021a769-fab0-4dc9-be07-00791ab5650b)

### **Circuit Diagram**

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FGeyUX0GSePSLsDluGpwS%2Fimage.png?alt=media\&token=ce952030-b48d-4085-98c6-7c64088199ea)

### 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](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FAdZGxMYTgDFwCV7RbGcj%2FUntitled.png?alt=media\&token=f30ef469-ba7a-4034-a2c3-490e5c52dc46)

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

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;
