หน้าแรกHome › Arduino › บทที่ 02Lesson 02
Blink LEDBlink LED
ควบคุมการกระพริบของ LEDMake an LED blink
บทนี้เราจะได้...In this lesson you will...
- รู้ว่า LED มีขายาว (+) กับขาสั้น (−) และต้องต่อให้ถูกด้านLearn that an LED has a long leg (+) and a short leg (−), and they must go the right way
- ต่อวงจรให้ไฟวิ่งครบรอบ: ขา 13 → LED → ตัวต้านทาน → GNDBuild a complete loop: pin 13 → LED → resistor → GND
- เขียนโค้ดสั่งให้ไฟเปิด–ปิดสลับกันทุกครึ่งวินาทีWrite code that turns the light on and off every half second
อุปกรณ์ที่ใช้What you need
ขา 13 จ่ายไฟ 5V ส่วน LED สีแดงใช้แรงดันประมาณ 2V จึงเหลือ 3V ตกคร่อมตัวต้านทาน ตามกฎของโอห์ม I = V ÷ R = 3 ÷ 220 ≈ 0.014 A หรือ 14 mA ซึ่งพอดีให้ LED สว่างโดยไม่พัง (LED ทั่วไปรับได้ราว 20 mA)Pin 13 gives 5V and a red LED drops about 2V, leaving 3V across the resistor. Ohm's law: I = V ÷ R = 3 ÷ 220 ≈ 0.014 A, or 14 mA. That's bright but safe (a typical LED handles about 20 mA).
โต๊ะทดลอง: ต่อวงจร แล้วรันโค้ด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
ในแต่ละคอลัมน์ ช่อง a–e เชื่อมกันเป็นแถบเดียว ช่อง f–j ก็อีกแถบ แต่สองฝั่งไม่ต่อข้ามร่องกลาง ส่วนรางยาวด้านบนและล่าง (+ / −) เชื่อมกันตลอดแนวนอน ใช้เป็นทางไฟเลี้ยงและ GND ร่วมกันIn each column, holes a–e are one strip and f–j are another; they don't connect across the middle gap. The long rails at the top and bottom (+ / −) run the whole length and are used to share power and GND.
อ่านโค้ดทีละบรรทัดReading the code, line by line
โค้ดอยู่ในช่องโค้ดของโต๊ะทดลองด้านบน ตอนกดรัน บรรทัดที่บอร์ดกำลังทำจะเป็นสีเหลืองThe code lives in the code box on the lab bench above. While it runs, the line the board is doing turns yellow.
setup() { ... }ทำครั้งเดียวตอนเปิดบอร์ด เหมือนการเตรียมของก่อนเริ่มเล่นRuns once when the board starts, like getting ready before you play.loop() { ... }ทำซ้ำไปเรื่อย ๆ ไม่มีวันหยุด ไฟเลยกะพริบตลอดRepeats forever, which is why the light keeps blinking.digitalWrite(13, HIGH)HIGH = ส่งไฟ 5V ออกขา 13 (เปิด) · LOW = 0V (ปิด)HIGH = send 5V out of pin 13 (on) · LOW = 0V (off)delay(500)หยุดรอ 500 มิลลิวินาที = ครึ่งวินาที (1000 = 1 วินาที)Wait 500 milliseconds = half a second (1000 = 1 second)ลองแก้เองดูสิTry changing it
แก้โค้ดในช่องโค้ดของโต๊ะทดลองด้านบนได้เลย แล้วกด ▶ รันโค้ด ดูผล ถ้าแก้จนงง กด "คืนค่าโค้ดเดิม" ได้Edit the code in the lab bench's code box above, then press ▶ Run code. Got lost? Press "Reset code".
- เปลี่ยนเป็น
100ไฟจะกะพริบเร็วขึ้นหรือช้าลง?Change both to100. Faster or slower? - ทำให้ไฟติดนาน 1 วินาที แต่ดับแค่ 0.2 วินาทีMake it stay on for 1 second but off for only 0.2 seconds.
- ย้ายสายไปขา 12 แล้วแก้โค้ดให้ไฟยังกะพริบได้Move the wire to pin 12, then fix the code so it still blinks.
- ท้าทาย: ทำไฟกะพริบเป็นรหัสมอร์ส S-O-S (สั้น 3 ยาว 3 สั้น 3) ลองใช้
forช่วยวนซ้ำChallenge: blink S-O-S in Morse code (3 short, 3 long, 3 short). Try aforloop.
มีบอร์ด Arduino จริง? กดคัดลอกโค้ด ไปวางในโปรแกรม Arduino IDE (ฟรี) แล้วกดอัปโหลดลงบอร์ดได้เลยHave a real Arduino? Copy the code into the free Arduino IDE and upload it to your board.
ควิซท้ายบทQuick quiz
ขาไหนของ LED คือขาบวก (+)?Which LED leg is positive (+)?
ถ้าเปลี่ยน delay(500) เป็น delay(2000) จะเกิดอะไรขึ้น?If you change delay(500) to delay(2000), what happens?
ตัวต้านทานในวงจรนี้มีไว้ทำอะไร?What is the resistor for in this circuit?
โค้ดในส่วน loop() ทำงานกี่ครั้ง?How many times does the code in loop() run?
ใบงาน: Worksheet: Blink LEDBlink LED
วาดวงจรของฉัน (ขา 13 → LED → ตัวต้านทาน → GND)Draw my circuit (pin 13 → LED → resistor → GND)
ทำครบทุกภารกิจแล้ว?All missions done?
กดปุ่มเพื่อให้จุดบนแผนที่บทเรียนสว่างขึ้นPress the button to light up this lesson on the map.