05. การติดต่อกับสัญญาณอนาลอก

รูปที่ 1

ข้อควรจำ เนื่องจากขา Digital 0 (PD0) เป็นขา RxD และ Digital 1 (PD1) เป็นขา ธxD ของพอร์ทอนุกรม UART ซึ่ง Arduino ใช้เป็นพอร์ทดาวน์โหลดโปรแกรมลงชิพ AVR ดังนั้นขณะดาวน์โหลดโปรแกรมนี้ ห้ามต่อขา D0 และ D1 เข้ากับวงจรใดๆ

โปรแกรมที่ 1

// include the library code:

#include <LiquidCrystal.h>

int sensorPin = A0; // select the input pin for the potentiometer

int sensorValue = 0; // variable to store the value coming from the sensor

// initialize the library with the numbers of the interface pins

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {

// set up the LCD's number of columns and rows:

lcd.begin(16, 2);

}

void loop() {

sensorValue = analogRead(sensorPin); // read the value from the sensor:

lcd.setCursor(0, 0); // set the cursor to column 1, line 1

lcd.print(sensorValue); // พิมพ์ค่าออกที่ LCD

lcd.print(" "); // พิมพ์ ที่ว่าง

}

โปรแกรมที่ 2

โปรแกรม อ่านค่าแรงดันด้วย A/D

// include the library code:

#include <LiquidCrystal.h>

int sensorPin = A0; // select the input pin for the potentiometer

int sensorValue = 0; // variable to store the value coming from the sensor

float volt = 0.0; // Convert from sensor

// initialize the library with the numbers of the interface pins

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {

// set up the LCD's number of columns and rows:

lcd.begin(16, 2);

// Print a message to the LCD.

lcd.print("Power is");

sensorValue = 0;

}

void loop() {

sensorValue = analogRead(sensorPin); // read the value from the sensor:

lcd.setCursor(0, 0); // set the cursor to column 1, line 1

lcd.print("Digital is ");

lcd.print(sensorValue);

lcd.print(" ");

lcd.setCursor(0, 1); // set the cursor to column 1, line 2

lcd.print("Analog is ");

volt = (float)((sensorValue*5.0)/1024.0);

lcd.print(volt);

//lcd.print((sensorValue*5)/1024);

lcd.print(" ");

}

โปรแกรม อ่าน Infared Sensor แล้วเปรียบเทียบ

คู่มือของ TCRT5000 อยู่ด้านล่านหน้าเว็บนี้

รูปที่ 2 ลักษณะเซ็นเอร์และการต่อวงจร โดย ขา+V ต่อกับ 5 Volt ขา Data ต่อกับ A0 ของ Arduino

โปรแกรมที่ 3

โปรแกรม โปรแกรมตรวจสอบเซ็นเซอร์

// include the library code:

#include <LiquidCrystal.h>

int sensorPin = A0; // select the input pin for the potentiometer

int ledPin = 13; // select the pin for the LED

int sensorValue = 0; // variable to store the value coming from the sensor

int setting_value = 500;

#define led_on digitalWrite(ledPin, HIGH)

#define led_off digitalWrite(ledPin, LOW)

// initialize the library with the numbers of the interface pins

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {

// set up the LCD's number of columns and rows:

lcd.begin(16, 2);

// Print a message to the LCD.

lcd.print("Power is");

sensorValue = 0;

// declare the ledPin as an OUTPUT:

pinMode(ledPin, OUTPUT);

}

void loop() {

sensorValue = analogRead(sensorPin); // read the value from the sensor:

lcd.setCursor(0, 0); // set the cursor to column 1, line 1

lcd.print("Sensor is ");

lcd.print(sensorValue);

lcd.print(" ");

lcd.setCursor(0, 1); // set the cursor to column 1, line 2

if(sensorValue > setting_value)

{

lcd.print("Sensor ON ");

led_on;

}

else

{

lcd.print("Sensor OFF ");

led_off;

}

}