# LAB: Basic Digital Input/Output

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

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

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

ทดลองเขียนโปรแกรมส่งสัญญาณแบบดิจิตอลผ่านขา Output ของ Arduino board  และใช้ MyDAQ การวัดค่าแรงดันที่ค่า output ของบอร์ด

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

<table><thead><tr><th width="397">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></tbody></table>

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FvZb8cQ02LjXtj1WA2kt6%2Fdds.png?alt=media\&token=85d9353b-2399-4add-9f20-fd3ef56345e4)

### โปรแกรม

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

### **Code**

```arduino
// lab1: basic digital output 
  const int Output_digital = 7; //set pin 7 to active

void setup() {
  // put your setup code here, to run once:
  pinMode (Output_digital,OUTPUT); // assingn Output_digital to work as Output 
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(Output_digital,HIGH); // Make this pin to be High
}

```

### **บันทึกผลการทดลอง**&#x20;

1. ทำการวัดและอ่านค่าแรงดันที่แสดงผลบน Digital multimeter Application&#x20;
2. บันทึกผลการทดลอง template ที่ได้รับมอบหมาย&#x20;

## Lab 2: การเลือกค่าความต้านทาน(Resistor)ที่เหมาะสมกับวงจร LED

* อ่าน [Datasheet](https://components101.com/sites/default/files/component_datasheet/5mm-LED-Datasheet.pdf) ของ LED ที่ให้มาแล้วคำนวณหาค่า ความต้านทาน ที่รองรับแรงดัน 5V เพื่อไม่ให้แรงดันที่ตกคร่อม LED เกินกว่าค่าที่ระบุไว้ใน [Datasheet](https://components101.com/sites/default/files/component_datasheet/5mm-LED-Datasheet.pdf)&#x20;
* ทำการต่อวงจรและวัดค่าแรงดันที่ตกคร่อม LED พร้อมทั้งจดบันทึกผล

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

| Component        | Quantity |
| ---------------- | -------- |
| Arduino mega2560 | 1        |
| myDAQ            | 1        |
| Red LED          | 1        |
| Resistor         | 1        |

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FLuPJf6jj2a0UC3vRxEu6%2Fqq.png?alt=media\&token=c3dbe5f7-471b-407f-a33c-23034adcabb8)

### โปรแกรม

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%2FZKBEjRXp4pAFhyFGLTCf%2FUntitled.png?alt=media\&token=4d75c568-76dd-4101-850b-2d2f0a112d5a)

### **Circuit Diagram**

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FEpT8Ld0ohca1ckoB5mWN%2FUntitled.png?alt=media\&token=8f233b25-79e5-44de-a03a-c2d36fd983a3)

### **บันทึกผลการทดลอง**&#x20;

1. ทำการวัดแรงดันและอ่านค่าแรงดันตกคร่อม led และ ตัวต้านทาน จาก Digital multimeter Application&#x20;
2. บันทึกผลการทดลอง template ที่ได้รับมอบหมาย&#x20;

## **Lab3:** **Basic Digital Input/Output**

ทดลองเขียนโปรแกรมรับข้อมูลแบบดิจิตอลจาก button ผ่านขา Input และส่งข้อมูลแบบดิจิตอลเพื่อควบคุม LED ผ่านขา Output ของ Aduino Uno R3&#x20;

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

| Component      | Quantity |
| -------------- | -------- |
| Arduino Uno R3 | 1        |
| Pushbutton     | 1        |
| Resistor       | 1        |
| Red LED        | 1        |

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FJixdnq2kWYg17gR68GuW%2FUntitled.png?alt=media\&token=e816be93-0d52-47df-b6e9-a1d0b44fe4d9)

### โปรแกรม

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%2FhRTaHQnIpA6DipqHFdnn%2FUntitled.png?alt=media\&token=d207a4d1-7cf2-4a84-9802-bd6b8a31d71e)

### **Circuit Diagram**

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FHWwRem4CkiR5pdchZiJp%2FUntitled.png?alt=media\&token=e5606af1-6373-4528-897f-e44e7d5dd7f0)

### **Code**

```arduino
// Lab3: Basic Digital Input/Output
const int Output_digital = 7; //set pin 7 to active
const int Input_digital = 8;  //set pin 8 to active

void setup() {
  // put your setup code here, to run once:
  pinMode (Output_digital,OUTPUT); //set pin 7 as output
  pinMode (Input_digital,INPUT_PULLUP);   //set pin 8 as input
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  int Input = 0;
  Input = digitalRead(Input_digital);
  if (Input == 1)
    digitalWrite(Output_digital,HIGH); //when output is High LED is lit
  else
    digitalWrite(Output_digital,LOW);  //when output is Low LED is off

}
```

### **บันทึกผลการทดลอง**&#x20;

1. บันทึกผลการทดลอง template ที่ได้รับมอบหมาย&#x20;
