ห้องทดลองเทคโนโลยีTech Lab ครูใบเฟิร์นKru Baifern

หน้าแรกHome › Arduino › บทที่ 07Lesson 07

07

BuzzerBuzzer

สร้างเสียงเตือนMake a warning beep

ระดับเนื้อหาLevel
1

บทนี้เราจะได้...In this lesson you will...

  • ต่อ buzzer ให้บอร์ดส่งเสียงได้Wire a buzzer so the board can make sound
  • ใช้ tone() กับ noTone() เปิด–ปิดเสียงUse tone() and noTone() to start and stop sound
  • รู้ว่าความถี่ (Hz) มาก = เสียงสูงKnow that a bigger frequency (Hz) = a higher sound
2

อุปกรณ์ที่ใช้What you need

Arduino UnoArduino Uno× 1
BuzzerBuzzer× 1
สายจัมเปอร์Jumper wires× 2
มัธยม · เสียงเกิดได้ยังไงSecondary · How the sound is made

tone(8, 1000) ทำให้ขา 8 สลับ HIGH/LOW 1000 ครั้งต่อวินาที แผ่นเพียโซใน buzzer สั่นตาม อากาศสั่นตาม หูเราจึงได้ยินเป็นเสียง หูคนได้ยินราว 20–20,000 Hztone(8, 1000) flips pin 8 HIGH/LOW 1000 times a second. The piezo disc in the buzzer shakes, the air shakes, and we hear it. People hear about 20–20,000 Hz.

3

โต๊ะทดลอง: ต่อวงจร แล้วรันโค้ดLab bench: wire it, then run the code

สีสายWire

ลากจากขาหรือช่องหนึ่งไปอีกช่องเพื่อต่อสาย · ชี้ที่ช่องเพื่อดูว่าช่องไหนเชื่อมกัน · แตะที่สายเพื่อลบ · แตะที่ 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

โค้ด (แก้ได้เลย!)Code (edit it!)

ภารกิจMissions

    4

    อ่านโค้ดทีละบรรทัด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:

    tone(buzzer, 1000)ส่งเสียงที่ความถี่ 1000 Hz ออกขา 8 (เสียงดังค้างจนกว่าจะสั่งหยุด)Play a 1000 Hz sound on pin 8 (it keeps going until you stop it)
    noTone(buzzer)หยุดเสียงStop the sound
    tone(buzzer, 1500)ความถี่มากขึ้น = เสียงสูงขึ้นA bigger frequency = a higher sound
    delay(200)ปล่อยเสียงหรือความเงียบไว้ 0.2 วินาทีKeep the sound (or the quiet) for 0.2 seconds
    5

    ลองแก้เองดูสิ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 Hz กับ 900 HzMake a siren: switch between 600 Hz and 900 Hz
    • เล่นโน้ต โด เร มี: 262, 294, 330 HzPlay do re mi: 262, 294, 330 Hz
    • ท้าทาย: เก็บโน้ตเพลงไว้ใน array แล้วเล่นด้วย for เช่นเพลงช้างช้างช้างChallenge: keep a tune's notes in an array and play them with for

    มีบอร์ด Arduino จริง? กดคัดลอกโค้ด ไปวางในโปรแกรม Arduino IDE (ฟรี) แล้วกดอัปโหลดลงบอร์ดได้เลยHave a real Arduino? Copy the code into the free Arduino IDE and upload it to your board.

    6

    ควิซท้ายบทQuick quiz

    คำสั่งไหนทำให้ buzzer เงียบ?Which command makes the buzzer quiet?

    tone(8, 2000) เทียบกับ tone(8, 500) เสียงเป็นยังไง?How does tone(8, 2000) sound compared with tone(8, 500)?

    Hz (เฮิรตซ์) คืออะไร?What is Hz (hertz)?

    buzzer ใช้ทำอะไรได้บ้าง?What can a buzzer be used for?

    ทำครบทุกภารกิจแล้ว?All missions done?

    กดปุ่มเพื่อให้จุดบนแผนที่บทเรียนสว่างขึ้นPress the button to light up this lesson on the map.

    กลับแผนที่Back to map
    ← บทที่ 06Lesson 06 บทที่ 08Lesson 08 →