Skip to content

Commit 86205dc

Browse files
authored
Add 22_buzzer (#21)
1 parent 31ba0f8 commit 86205dc

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

22_buzzer/main.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package main
2+
3+
// Please connect a piezo buzzer to the 3V3 and EX01 pins on the back terminal.
4+
//
5+
// | EX01 | EX03 | 3V3 | SDA0 | 3V3 | 3V3 | | GROVE |
6+
// | EX02 | EX04 | GND | SCL0 | GND | GND | - - | GND | 3V3 | SDA0 | SCL0 |
7+
8+
import (
9+
"machine"
10+
"time"
11+
12+
"tinygo.org/x/drivers/tone"
13+
)
14+
15+
var pinToPWM = map[machine.Pin]tone.PWM{
16+
machine.GPIO14: machine.PWM7, // for EX01
17+
machine.GPIO15: machine.PWM7, // for EX02
18+
machine.GPIO26: machine.PWM5, // for EX01
19+
machine.GPIO27: machine.PWM5, // for EX01
20+
}
21+
22+
func main() {
23+
bzrPin := machine.GPIO14
24+
pwm := pinToPWM[bzrPin]
25+
speaker, err := tone.New(pwm, bzrPin)
26+
if err != nil {
27+
println("failed to configure PWM")
28+
return
29+
}
30+
31+
song := []tone.Note{
32+
tone.C5,
33+
tone.D5,
34+
tone.E5,
35+
tone.F5,
36+
tone.G5,
37+
tone.A5,
38+
tone.B5,
39+
tone.C6,
40+
tone.C6,
41+
tone.B5,
42+
tone.A5,
43+
tone.G5,
44+
tone.F5,
45+
tone.E5,
46+
tone.D5,
47+
tone.C5,
48+
}
49+
50+
for {
51+
for _, val := range song {
52+
speaker.SetNote(val)
53+
time.Sleep(time.Second / 2)
54+
}
55+
}
56+
}

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ smoketest:
2424
tinygo build -o ./out/19_redkey.uf2 --target waveshare-rp2040-zero --size short ./19_redkey
2525
tinygo build -o ./out/20_rotary_gopher.uf2 --target waveshare-rp2040-zero --size short ./20_rotary_gopher
2626
tinygo build -o ./out/21_midi2.uf2 --target waveshare-rp2040-zero --size short ./21_midi2
27+
tinygo build -o ./out/22_buzzer.uf2 --target waveshare-rp2040-zero --size short ./22_buzzer
2728
tinygo build -o ./out/80_checker.uf2 --target waveshare-rp2040-zero --size short ./80_checker

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,76 @@ Windows 環境では MIDI-OX を使うとよいでしょう。
791791

792792
* [./21_midi2](./21_midi2//)
793793

794+
## buzzer を使う
795+
796+
※この例を試すには他励ブザーが必要です
797+
※ EX01 と 3V3 間にブザーをつないでください。
798+
799+
![](./images/22_buzzer.jpg)
800+
801+
ブザーを鳴らす方法はいろいろありますが、ここでは PWM を使用します。
802+
TinyGo で PWM を使用する場合、マイコン毎の設定が若干残っていることに注意が必要です。
803+
804+
RP2040 で zero-kb02 の背面端子 (EX01 - EX04) を使う場合は以下のような設定を使用することができます。
805+
必要なのはどのピンを使うか、どの PWMGroup を使うか、です。
806+
以下ではピンに対応する PWMGroup のマップを作製しました。
807+
808+
```go
809+
var pinToPWM = map[machine.Pin]tone.PWM{
810+
machine.GPIO14: machine.PWM7, // for EX01
811+
machine.GPIO15: machine.PWM7, // for EX02
812+
machine.GPIO26: machine.PWM5, // for EX01
813+
machine.GPIO27: machine.PWM5, // for EX01
814+
}
815+
```
816+
817+
このあとは tinygo.org/x/drivers/tone を使うと音を鳴らすことができます。
818+
819+
```go
820+
func main() {
821+
bzrPin := machine.GPIO14
822+
pwm := pinToPWM[bzrPin]
823+
speaker, err := tone.New(pwm, bzrPin)
824+
if err != nil {
825+
println("failed to configure PWM")
826+
return
827+
}
828+
829+
song := []tone.Note{
830+
tone.C5,
831+
tone.D5,
832+
tone.E5,
833+
tone.F5,
834+
tone.G5,
835+
tone.A5,
836+
tone.B5,
837+
tone.C6,
838+
tone.C6,
839+
tone.B5,
840+
tone.A5,
841+
tone.G5,
842+
tone.F5,
843+
tone.E5,
844+
tone.D5,
845+
tone.C5,
846+
}
847+
848+
for {
849+
for _, val := range song {
850+
speaker.SetNote(val)
851+
time.Sleep(time.Second / 2)
852+
}
853+
}
854+
}
855+
```
856+
857+
* 他励ブザーの例
858+
* https://akizukidenshi.com/catalog/g/g104118/
859+
* 参考ソース
860+
* https://github.com/tinygo-org/drivers/blob/release/examples/tone/tone.go
861+
* https://github.com/sago35/tinygo-examples/blob/main/wioterminal/buzzer/main.go
862+
863+
794864
# sago35/tinygo-keyboard を使う
795865

796866
自作キーボードに必要な要素、というのは人によって違うと思います。

images/22_buzzer.jpg

39.3 KB
Loading

0 commit comments

Comments
 (0)