Skip to content

Commit 6f213e9

Browse files
authored
Add driver for TMP102 low-power digital temperature sensor (#141)
* tmp102: add driver and example
1 parent ebceed6 commit 6f213e9

File tree

4 files changed

+106
-1
lines changed

4 files changed

+106
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func main() {
5252

5353
## Currently supported devices
5454

55-
The following 45 devices are supported.
55+
The following 46 devices are supported.
5656

5757
| Device Name | Interface Type |
5858
|----------|-------------|
@@ -101,6 +101,7 @@ The following 45 devices are supported.
101101
| [Waveshare 2.13" e-paper display](https://www.waveshare.com/w/upload/e/e6/2.13inch_e-Paper_Datasheet.pdf) | SPI |
102102
| [Waveshare 2.13" (B & C) e-paper display](https://www.waveshare.com/w/upload/d/d3/2.13inch-e-paper-b-Specification.pdf) | SPI |
103103
| [WS2812 RGB LED](https://cdn-shop.adafruit.com/datasheets/WS2812.pdf) | GPIO |
104+
| [TMP102 I2C Temperature Sensor](https://download.mikroe.com/documents/datasheets/tmp102-data-sheet.pdf) | I2C |
104105

105106
## Contributing
106107

examples/tmp102/main.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"machine"
6+
"time"
7+
8+
"tinygo.org/x/drivers/tmp102"
9+
)
10+
11+
func main() {
12+
machine.I2C0.Configure(machine.I2CConfig{
13+
Frequency: machine.TWI_FREQ_400KHZ,
14+
})
15+
16+
thermo := tmp102.New(machine.I2C0)
17+
thermo.Configure(tmp102.Config{})
18+
19+
for {
20+
21+
temp, _ := thermo.ReadTemperature()
22+
23+
print(fmt.Sprintf("%.2f°C\r\n", float32(temp)/1000.0))
24+
25+
time.Sleep(time.Millisecond * 1000)
26+
}
27+
28+
}

tmp102/registers.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package tmp102
2+
3+
const (
4+
// Default I2C address
5+
Address = 0x48
6+
7+
// Temperature register address
8+
RegTemperature = 0x00
9+
10+
// Configuration register address
11+
RegConfiguration = 0x01
12+
13+
// Low limit register address
14+
RegLimitLow = 0x02
15+
16+
// High limit register address
17+
RegLimitHigh = 0x03
18+
)

tmp102/tmp102.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Package tmp102 implements a driver for the TMP102 digital temperature sensor.
2+
//
3+
// Datasheet: https://download.mikroe.com/documents/datasheets/tmp102-data-sheet.pdf
4+
5+
package tmp102 // import "tinygo.org/x/drivers/tmp102"
6+
7+
import (
8+
"machine"
9+
)
10+
11+
// Device holds the already configured I2C bus and the address of the sensor.
12+
type Device struct {
13+
bus machine.I2C
14+
address uint8
15+
}
16+
17+
// Config is the configuration for the TMP102.
18+
type Config struct {
19+
Address uint8
20+
}
21+
22+
// New creates a new TMP102 connection. The I2C bus must already be configured.
23+
func New(bus machine.I2C) Device {
24+
return Device{
25+
bus: bus,
26+
}
27+
}
28+
29+
// Configure initializes the sensor with the given parameters.
30+
func (d *Device) Configure(cfg Config) {
31+
if cfg.Address == 0 {
32+
cfg.Address = Address
33+
}
34+
35+
d.address = cfg.Address
36+
}
37+
38+
// Reads the temperature from the sensor and returns it in celsius milli degrees (°C/1000).
39+
func (d *Device) ReadTemperature() (temperature int32, err error) {
40+
41+
tmpData := make([]byte, 2)
42+
43+
err = d.bus.ReadRegister(d.address, RegTemperature, tmpData)
44+
45+
if err != nil {
46+
return
47+
}
48+
49+
temperatureSum := int32((int16(tmpData[0])<<8 | int16(tmpData[1])) >> 4)
50+
51+
if (temperatureSum & int32(1<<11)) == int32(1<<11) {
52+
temperatureSum |= int32(0xf800)
53+
}
54+
55+
temperature = temperatureSum * 625
56+
57+
return temperature / 10, nil
58+
}

0 commit comments

Comments
 (0)