LAB: Basic Analog Input/Output

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

จุดประสงค์

  1. สามารถอ่านจาก Datasheet ของ sensor เพื่อวิเคราะห์หลักการทำงานสำหรับนำมาใช้ในการเขียนโปรแกรมให้เหมาะสมกับ sensor

  2. สามารถวัดและอ่านค่าแรงดันของขาอุปกรณ์ด้วย Digital Multimeter ได้

  3. สามารถเขียนโปรแกรมลง Embedded board เพื่อควบคุมอุปกรณ์ผ่านขา I/O ด้วยสัญญาณ analogได้

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

  • อ่าน Datasheet ของ potentiometer ว่าหน้าที่ของแต่ละใช้งานอย่างไรและต่อ sensor กับ Embedded board

  • ต่อวงจรและเขียนโปรแกรมให้สามารถรับค่าจาก sensor แบบ Analog

  • สังเกตการทำงานของโปรแกรมบน serial monitor เมื่อปรับค่า sensor

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

Component Quantity

Arduino mega2560

1

myDAQ

1

LED

1

Potentiometer

1

โปรแกรม

  1. Arduino IDE

  2. โปรแกรม NI ELVISSmx

Schematic

Circuit Diagram

Code

// 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 ด้วยการกดปุ่มตามรูป

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

  1. ทดลองปรับค่า potentiometer สังเกตุค่าการเปลี่ยนแปลงบน Serial monitor

  2. วัดค่าแรงดันที่ออกจาก Output pin ของ potentiometer บันทึกการเปลี่ยนแปลงและค่าสูงสุด-ต่ำสุด ที่แสดงบน Serial monitor และค่าแรงดันที่วัดได้

  3. บันทึกผลการทดลอง template ที่ได้รับมอบหมาย

Lab 2:การทดลองใช้งาน potentiometer เป็นตัวควบคุม Analog output

การทดลองใช้งาน 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 และค่าแรงดันที่วัดได้

  3. บันทึกผลการทดลอง template ที่ได้รับมอบหมาย

Last updated

Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University