# LAB: Pulse Width Modulation (PWM)

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

1. &#x20;เข้าใจหลักการทำงานของ PWM
2. สามารถสร้างกราฟสัญญาณ PWM โดยใช้ Arduino board ได้

## LAB: การสร้างสัญญาณ PWM

การทดลองปรับค่า potentiometer จนได้ duty cycle 50% บันทึกผลกราฟ และอ่านค่าความถี่(Hz), แรงดัน(V) จากกราฟที่ได้

### อุปกรณ์ทดลอง

<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>Potentiometer</td><td>1</td></tr></tbody></table>

![](/files/iCoUYAB1Fv2nMim6AhJ5)

### โปรแกรม

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

### Schematic

![](/files/a2kGnCVwAAsJwGMxnkUH)

### Circuit Diagram

![](/files/Z99kjTP7z3chZQ2J3VmF)

### Code

```arduino
//Arduino Mage 2560 read potentiometer and write LED
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. บันทึกภาพวงจร ผลกราฟ, serial monitor และอธิบายการทดลองลง template ที่ได้รับมอบหมาย


---

# 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/waveform/lab-pulse-width-modulation-pwm.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.
