หน้าแรกHome › Arduino › บทที่ 08Lesson 08
เซนเซอร์วัดแสง (LDR)Light Sensor (LDR)
ตรวจวัดความสว่างSense how bright it is
บทนี้เราจะได้...In this lesson you will...
- อ่านค่าแสงด้วย analogRead() ได้ตัวเลข 0–1023Read light with analogRead() as a number from 0–1023
- ใช้ค่าที่อ่านได้ตัดสินใจ: มืดแล้วเปิดไฟUse the reading to decide: dark means light on
- รู้ความต่างของขา AO (ตัวเลข) กับ DO (มืด/สว่าง)Know the difference between AO (a number) and DO (dark/bright)
อุปกรณ์ที่ใช้What you need
LDR (Light Dependent Resistor) เป็นตัวต้านทานที่ค่าความต้านทานเปลี่ยนตามแสง สว่างมาก = ความต้านทานต่ำ โมดูลต่อ LDR เป็นวงจรแบ่งแรงดัน แล้วขา AO ส่งแรงดันนั้นให้บอร์ดอ่านเป็นตัวเลขAn LDR (Light Dependent Resistor) is a resistor whose resistance changes with light: bright = low resistance. The module puts it in a voltage divider, and AO sends that voltage to the board to read as a number.
โต๊ะทดลอง: ต่อวงจร แล้วรันโค้ดLab bench: wire it, then run the code
ลากจากขาหรือช่องหนึ่งไปอีกช่องเพื่อต่อสาย · ชี้ที่ช่องเพื่อดูว่าช่องไหนเชื่อมกัน · แตะที่สายเพื่อลบ · แตะที่ LED เพื่อกลับขาDrag from one pin or hole to another to add a wire · point at a hole to see what's connected · tap a wire to remove it · tap an LED to flip it
ภารกิจMissions
อ่านโค้ดทีละบรรทัดReading the code, line by line
ช่องโค้ดเริ่มจากโครงเปล่า ปิ๊งจะบอกทีละบรรทัดว่าต้องพิมพ์อะไร ตอนกดรัน บรรทัดที่บอร์ดกำลังทำจะเป็นสีเหลือง คำสั่งสำคัญของบทนี้มีดังนี้The code box starts as an empty sketch, and Ping tells you what to type line by line. While it runs, the line the board is doing turns yellow. The key commands in this lesson:
analogRead(A0)อ่านแรงดันที่ขา A0 เป็นตัวเลข 0 ถึง 1023 (ในโมดูลนี้ ค่ามาก = มืด)Read the voltage on A0 as a number 0 to 1023 (on this module, bigger = darker)Serial.println(light)ส่งค่าที่อ่านได้ไปดูที่ Serial MonitorPrint the reading in the Serial Monitorif (light > 600)ถ้าค่ามากกว่า 600 แปลว่ามืดพอแล้วIf it's more than 600, it's dark enoughdigitalWrite(13, HIGH)เปิดไฟ L เหมือนไฟถนนที่ติดเองตอนกลางคืนTurn on the L light, like a street light at nightลองแก้เองดูสิTry changing it
แก้โค้ดในช่องโค้ดของโต๊ะทดลองด้านบนได้เลย แล้วกด ▶ รันโค้ด ดูผล อยากเริ่มใหม่ทั้งหมด กด "ล้างโค้ด"Edit the code in the lab bench's code box above, then press ▶ Run code. Want to start over? Press "Clear code".
- เปลี่ยนเลข 600 ให้ไฟติดตอนมืดมาก ๆ เท่านั้นChange 600 so the light comes on only when it's very dark
- เพิ่ม LED อีกดวงที่ติดตอนสว่างAdd another LED that lights when it's bright
- ท้าทาย: ใช้
map(light, 0, 1023, 0, 100)แปลงเป็นเปอร์เซ็นต์ความมืดChallenge: usemap(light, 0, 1023, 0, 100)to show darkness as a percent
มีบอร์ด Arduino จริง? กดคัดลอกโค้ด ไปวางในโปรแกรม Arduino IDE (ฟรี) แล้วกดอัปโหลดลงบอร์ดได้เลยHave a real Arduino? Copy the code into the free Arduino IDE and upload it to your board.
ควิซท้ายบทQuick quiz
analogRead() ให้ค่าในช่วงไหน?What range does analogRead() give?
ขาไหนของโมดูลให้ค่าแสงเป็นตัวเลข?Which module pin gives light as a number?
ในโค้ดนี้ ถ้าค่า light = 900 ไฟ L จะเป็นยังไง?In this code, if light = 900, what does the L light do?
ของใช้อะไรในชีวิตจริงใช้เซนเซอร์แสง?What real thing uses a light sensor?
ใบงาน: Worksheet: เซนเซอร์วัดแสง (LDR)Light Sensor (LDR)
ออกแบบสิ่งประดิษฐ์ที่ใช้เซนเซอร์แสง เช่น โคมไฟอัตโนมัติDesign an invention with a light sensor, like an automatic lamp
ทำครบทุกภารกิจแล้ว?All missions done?
กดปุ่มเพื่อให้จุดบนแผนที่บทเรียนสว่างขึ้นPress the button to light up this lesson on the map.