Skip to content

Commit 49865e7

Browse files
authored
Merge pull request #29 from tinygo-keeb/sht40
Sht40
2 parents 6ef8f51 + 364561c commit 49865e7

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

24_sht40/main.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"image/color"
6+
"machine"
7+
"time"
8+
9+
"tinygo.org/x/drivers"
10+
"tinygo.org/x/drivers/sht4x"
11+
"tinygo.org/x/drivers/ssd1306"
12+
"tinygo.org/x/tinyfont"
13+
"tinygo.org/x/tinyfont/shnm"
14+
)
15+
16+
var (
17+
white = color.RGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF}
18+
black = color.RGBA{R: 0x00, G: 0x00, B: 0x00, A: 0xFF}
19+
)
20+
21+
func main() {
22+
machine.I2C0.Configure(machine.I2CConfig{
23+
Frequency: 2.8 * machine.MHz,
24+
SDA: machine.GPIO12,
25+
SCL: machine.GPIO13,
26+
})
27+
28+
display := ssd1306.NewI2C(machine.I2C0)
29+
display.Configure(ssd1306.Config{
30+
Address: 0x3C,
31+
Width: 128,
32+
Height: 64,
33+
})
34+
display.SetRotation(drivers.Rotation180)
35+
display.ClearDisplay()
36+
time.Sleep(50 * time.Millisecond)
37+
38+
sensor := sht4x.New(machine.I2C0)
39+
40+
cnt := 0
41+
for {
42+
for x := int16(0); x < 128; x += 2 {
43+
temp, humidity, _ := sensor.ReadTemperatureHumidity()
44+
t := fmt.Sprintf("温度 %.2f ℃", float32(temp)/1000)
45+
h := fmt.Sprintf("湿度 %.2f %", float32(humidity)/1000)
46+
47+
display.ClearBuffer()
48+
tinyfont.WriteLine(&display, &shnm.Shnmk12, 5, 10, t, white)
49+
tinyfont.WriteLine(&display, &shnm.Shnmk12, 5, 30, h, white)
50+
display.Display()
51+
time.Sleep(1 * time.Second)
52+
}
53+
cnt++
54+
}
55+
}

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ smoketest:
2626
tinygo build -o ./out/21_midi2.uf2 --target waveshare-rp2040-zero --size short ./21_midi2
2727
tinygo build -o ./out/22_buzzer.uf2 --target waveshare-rp2040-zero --size short ./22_buzzer
2828
tinygo build -o ./out/23_akatonbo.uf2 --target waveshare-rp2040-zero --size short ./23_akatonbo
29+
tinygo build -o ./out/24_sht40.uf2 --target waveshare-rp2040-zero --size short ./24_sht40
2930
tinygo build -o ./out/80_checker.uf2 --target waveshare-rp2040-zero --size short ./80_checker

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,38 @@ $ tinygo flash --target waveshare-rp2040-zero --size short ./22_buzzer/
877877
* [./23_akatonbo](./23_akatonbo/)
878878
* https://github.com/triring/7Keyx3Oct
879879

880+
## I2C 接続の温湿度センサーを使う
881+
882+
※ この例を試すには I2C 接続の SHT4x (SHT40 / SHT41 など) が必要です
883+
※ GROVE 端子に接続してください
884+
885+
![](./images/24_sht40.jpg)
886+
887+
ここでは GROVE 端子に接続した I2C のセンサーを使用してみます。
888+
GROVE 端子は oled と同じピンにつながっているので I2C0 を使う必要があります。
889+
890+
Frequency は 2.8MHz としていますが、スペック上は 400KHz とかに下げる必要があります。
891+
892+
温度と湿度については `ReadTemperatureHumidity()` で取得可能です。
893+
894+
```go
895+
// import "tinygo.org/x/drivers/sht4x" が必要
896+
machine.I2C0.Configure(machine.I2CConfig{
897+
Frequency: 2.8 * machine.MHz,
898+
SDA: machine.GPIO12,
899+
SCL: machine.GPIO13,
900+
})
901+
sensor := sht4x.New(machine.I2C0)
902+
temp, humidity, _ := sensor.ReadTemperatureHumidity()
903+
t := fmt.Sprintf("温度 %.2f", float32(temp)/1000)
904+
h := fmt.Sprintf("湿度 %.2f", float32(humidity)/1000)
905+
```
906+
907+
* SHT4x 入手元
908+
* https://www.switch-science.com/products/9270
909+
* https://www.switch-science.com/products/8737
910+
* https://akizukidenshi.com/catalog/g/g130207/
911+
880912
# sago35/tinygo-keyboard を使う
881913

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

0 commit comments

Comments
 (0)