--- title: สอนทำ เครื่องวัดค่าความสว่าง ด้วยบอร์ด Arduino และ LDR Photoresistor tags: - Blog favourite: false created: '2021-04-15T22:09:36+07:00' published: '2021-04-15' modified: '2021-04-15T22:15:49+07:00' template: '[[General]]' feature: '[[DSC01007-copy-720x400.jpg]]' --- # สอนทำ เครื่องวัดค่าความสว่าง ด้วยบอร์ด Arduino และ LDR Photoresistor ![[DSC01007-copy-720x400.jpg]] > บทความนี้ เป็นบทความจาก Clip YouTube “เปรียบเทียบหลอดไฟ 7 ยี่ห้อ ด้วยเครื่องวัดค่าความสว่าง DIY | SmartHomeOK | PorTV” https://www.youtube.com/watch?v=313ZhIyCKI8 ## อุปกรณ์ที่ต้องใช้ 1. Arduino UNO 2. LDR Photoresistor 3. ตัวต้านทาน 10K Ohm 4. 7 Segment LCD (TM1637) แบบ 4 ตัวอักษร 4 PIN (ไม่จำเป็น) > Code ทั้งหมด อยู่ใน Github [https://github.com/chaintng/arduino-ldr-tm1637-led](https://github.com/chaintng/arduino-ldr-tm1637-led) ## วิธีการทำ ขอแบ่งวิธีการทำออกเป็น 3 ส่วนนะครับ โดยส่วนแรก คือการทำเครื่องวัดแสง ส่วนที่ 2 คือการแสดงผลบนจอ LCD และส่วนที่ 3 คือการนำไปแสดงบนจอ PC ### ส่วนที่ 1: การทำเครื่องวัดแสง ![[working_with_ldr_iZfldcQDGN-1024x768.jpeg]] 1. ต่อสาย LDR และ ตัวต้านทาน ตามรูปประกอบ 2. เข้า Arduino IDE และ รัน Code ด้านล่าง ``` const int LDR = A0; int input_val = 0; void setup() { Serial.begin(9600); } void loop() { input_val = analogRead(LDR); Serial.print("LDR Value is: "); Serial.println(input_val); delay(1000); } ``` ค่าความสว่างจะแสดงขึ้นที่ Serial Monitor ดังรูปประกอบ ![[image-1-1024x540.png]] ค่าความสว่าง จะแสดงขึ้นมาแล้วที่ Serial Monitor ### ส่วนที่ 2: การแสดงผลบนจอ LCD ![[tm1637_sketch_bb_Sb0Q69e5MA-1024x509.jpeg]] 1. ให้ต่อสาย 7 Segment LCD ตามรูป (ผ่าน Breadbroad เพื่อความสะดวก) 2. รัน Code ด้านล่าง ``` #include #define CLK 2 #define DIO 3 TM1637 Display1(CLK, DIO); #define CHAR_BLANK 127 const int ldrPin = A0; void setup() { Serial.begin(9600); Display1.init(); Display1.set(BRIGHTEST); // BRIGHT_TYPICAL = 2 , BRIGHT_DARKEST = 0 , BRIGHTEST = 7 void loop() { int ldrStatus = 100 - ((double) analogRead(ldrPin)) / 1024 * 100; // Normalize data to 100 Serial.println(ldrStatus); int8_t number[] = {0,0, ldrStatus / 10 , ldrStatus % 10}; // Send to LCD Display1.display(number); delay(1000); } ``` ตัวเลขค่า LDR จะแสดงบนจอ 7Segment LCD ![[DSC01005-copy-1024x683.jpg]] ### ส่วนที่ 3: การแสดงผล บนจอ PC 1. ในขณะที่ Arduino รันโปรแกรมอย่างถูกต้องให้ปิด Arduino IDE 2. install pySerial โดยใช้คำสั่ง pip install pySerial 3. ใช้ VSCode รัน Python script ด้านล่าง (อย่าลืมเปลี่ยน serial_port ตาม port ของคุณ) ``` ############## ## Script listens to serial port and writes contents into a file ############## ## requires pySerial to be installed import serial serial_port = '/dev/cu.usbmodem1101'; ## Change to your correct serial port, check in Arduino IDE baud_rate = 9600; #In arduino, Serial.begin(baud_rate) write_to_file_path = "output.txt"; ser = serial.Serial(serial_port, baud_rate) while True: line = ser.readline(); line = line.decode("utf-8") #ser.readline returns a binary, convert to string output_file = open(write_to_file_path, "w"); print(line); output_file.write(line); output_file.close(); ``` 4. สังเกตดูว่า ไฟล์ output.txt แสดงตัวเลขที่ถูกต้องหรือยัง 5. สร้างไฟล์ index.html ใน Directory เดียวกัน ```
``` 5. สำหรับ MacOS, Linux ให้ จำลอง Python SimpleHTTPServer ขึ้นมาใน Directory ปัจจุบัน ``` python -m SimpleHTTPServer 1234 ``` 6. เข้า localhost:1234 จะมีตัวเลขขึ้น ![[DSC01007-copy-1024x682.jpg]] สำหรับใครที่สงสัย สามารถ ทิ้งคำถามไว้ใน Comment ได้เลยครับ ## References - https://create.arduino.cc/projecthub/krivanja/working-with-light-dependent-resistor-ldr-1ded4f - https://create.arduino.cc/projecthub/ryanchan/tm1637-digit-display-arduino-quick-tutorial-ca8a93 - https://github.com/chaintng/arduino-ldr-tm1637-led