CircuitPython/MicroPythonに温度センサーTMP102(i2c)を接続する
2020/2/24
CircuitPython TMP102(i2c)
CircuitPython TMP102(i2c)
概要
CircuitPython/MicroPythonに以下の温度センサーTMP102(i2c)を接続する。
(ホストPCとしてはubuntuを想定している)
接続
以下のように接続する。
Wiring:
TMP102 | Teensy4.0(CircuitPython) |
---|---|
SCL | D19(SCL0) |
SDA | D18(SDA0) |
VCC | 3.3V |
GND | GND |
該当モジュールのインストール
Module Install:
git clone https://github.com/khoulihan/micropython-tmp102.git
cd micropython-tmp102
cp _tmp102.py tmp102_cpy.py
# Patch
次項目の内容のようにtmp102_cpy.pyにパッチする。
# copy related modules
cp tmp102_cpy.py CIRCUTPY/lib/
tmp102_cpy.pyの内容:
「#m」を含む行が修正行になる。
(コメントアウト行も含む)
# tmp102_cpy.py
# 2020/3/24 modification for CircutPython
REGISTER_TEMP = 0
REGISTER_CONFIG = 1
EXTENDED_MODE_BIT = 0x10
def _set_bit(b, mask):
return b | mask
def _clear_bit(b, mask):
return b & ~mask
def _set_bit_for_boolean(b, mask, val):
if val:
return _set_bit(b, mask)
else:
return _clear_bit(b, mask)
class Tmp102(object):
def __init__(self, bus, address, temperature_convertor=None, **kwargs):
# There doesn't seem to be a way to check this at present. The first
# send or recv should throw an error instead if the mode is incorrect.
#if not bus.in_master_mode():
# raise ValueError('bus must be in master mode')
self.bus = bus
self.address = address
self.temperature_convertor = temperature_convertor
# The register defaults to the temperature.
self._last_write_register = REGISTER_TEMP
self._extended_mode = False
if len(kwargs) > 0:
# Apply the passed in settings
config = bytearray(self._get_config())
for key, value in kwargs.items():
applyfunc = '_apply_{}'.format(key)
config = getattr(self, applyfunc)(config, value)
self._set_config(config)
if 'thermostat_high_temperature' in kwargs:
self.thermostat_high_temperature = kwargs['thermostat_high_temperature']
if 'thermostat_low_temperature' in kwargs:
self.thermostat_low_temperature = kwargs['thermostat_low_temperature']
def _read_register(self, register):
val = bytearray(2) #m
if register != self._last_write_register:
# Reads come from the last register written.
self._write_register(register)
try:
#m val = self.bus.readfrom(self.address, 2)
self.bus.readfrom_into(self.address, val)
except AttributeError:
#m val = self.bus.recv(2, addr=self.address)
self.bus.readfrom_into(self.address, val)
return val
def _write_register(self, register, value=None):
bvals = bytearray()
bvals.append(register)
if value is not None:
for val in value:
bvals.append(val)
try:
self.bus.writeto(self.address, bvals)
except AttributeError:
self.bus.send(bvals, addr=self.address)
self._last_write_register = register
def _get_config(self):
return self._read_register(REGISTER_CONFIG)
def _set_config(self, config):
self._write_register(REGISTER_CONFIG, config)
self._extended_mode = config[1] & EXTENDED_MODE_BIT
def _read_temperature_register(self, register):
rt = self._read_register(register)
lo = rt[1]
hi = rt[0]
negative = (hi >> 7) == 1
shift = 4
if self._extended_mode:
shift = 3
if not negative:
t = (((hi * 256) + lo) >> shift) * 0.0625
else:
remove_bit = 0b011111111111
if self._extended_mode:
remove_bit = 0b0111111111111
ti = (((hi * 256) + lo) >> shift)
# Complement, but remove the first bit.
ti = ~ti & remove_bit
t = -(ti * 0.0625)
if self.temperature_convertor is not None:
t = self.temperature_convertor.convert_to(t)
return rt, t
@property
def temperature(self):
"""
The most recently recorded temperature.
"""
rt, t = self._read_temperature_register(REGISTER_TEMP)
return t
CircuitPythonとMicroPythonに共通してあるi2c.writeto,i2c.readfrom_intoを使用しているので、両方に対応できている。
Demo Script(動作確認)
import time
import busio
import board
i2c = busio.I2C(board.SCL, board.SDA)
i2c.try_lock()
from tmp102_cpy import Tmp102
sensor = Tmp102(i2c, 0x48)
while True:
print('Temperature: {0:.1f}'.format(sensor.temperature))
time.sleep(2)
MicroPython(Nucleo)の場合
該当モジュールtmp102_cpy.pyが、CircuitPython/MicroPython両方に対応しているので そのまま/flashに書き込むだけでパッチなどは必要ない。
Demo Script:
import time
from machine import Pin, I2C
i2c = I2C(1)
from tmp102_cpy import Tmp102
sensor = Tmp102(i2c, 0x48)
while True:
print('Temperature: {0:.1f}'.format(sensor.temperature))
time.sleep(2)
デバイスなどの初期化以外はCircuitPythonのものと全く同じ。
参照情報
ESP8266のMicroPythonに温度センサーTMP102(i2c)を接続する
Turtorial Examples for CircuitPython(Teensy4.0)
Teensy4.0にCircuitPythonをインストールする
Turtorial Examples for Nucleo-MicroPytho
NUCLEO-F446REにMicropythonをインストールする(v2)
以上
| 固定リンク
« CircuitPython/MicroPythonにi2c-LCD(AE-AQM0802)を接続する | トップページ | RaspberryPiのpython3でCircuitPythonのAPIを使用する »
「linux」カテゴリの記事
- platfomioを使ってnaitive(linux/windows)のプログラムをビルドする方法(2021.03.10)
- micro:bit Yotta開発ツール(linux版)(2021.01.09)
- PlatformIOをRaspberryPi4で動かしてみる(実験)(2020.10.20)
- headless RaspberryPiインストール方法(v2)(2020.10.20)
- wio-terminalのファームウェア・アップデートについて(linux版)(2020.10.15)
「MicroPython」カテゴリの記事
- microbit-v2にMicropythonをインストールする(2021.05.06)
- PicoボードのMicroPythonをVS_CodeのextensionのPico-Goでプログラミングする(2021.02.10)
- MicroPython/CircuitPython Performance Test(2021.02.07)
- PicoボードにMicropython/CircuitPythonをインストールする(2021.02.03)
- MicroPython(F767ZI) Network Samples(2021.01.03)
「Nucleo」カテゴリの記事
- MicroPython/CircuitPython Performance Test(2021.02.07)
- MicroPython(F767ZI) Network Samples(2021.01.03)
- MicroPython(F767ZI)でStartwars(AsciiArt)を動かす(2020.12.29)
- NUCLEO-F767ZIにMicropythonをインストールする(v2)(2020.05.22)
- Nucleo-F303K8でADCの値をLCD(AQM0802)(i2c)に表示する(Arduino版)(2020.05.16)
「CircuitPython」カテゴリの記事
- MicroPython/CircuitPython Performance Test(2021.02.07)
- PicoボードにMicropython/CircuitPythonをインストールする(2021.02.03)
- XIAOでMPL3115A2(気圧・高度・気温センサ)を使用する(CircuitPython版)(2020.07.02)
- XIAOでMCP9808(温度センサ)を使用する(CircuitPython版)(2020.07.02)
- XIAOにCircuitPythonをインストールする(2020.06.09)
「Teensy4.0」カテゴリの記事
- Grove-OLED-i2cにFramebufferモジュールを使ってイメージを表示する(2020.04.25)
- FT232Hボード用ならびにCircuitPython用にJupyter-Notebookをインストールする(2020.04.05)
- FT232Hボードに気圧センサーMPL115A2(i2c)を接続する(2020.04.02)
- CircuitPythonに温度センサーLM61CIZ(analog)を接続する(2020.03.31)
- CircuitPython/MicroPythonに温度センサーTMP102(i2c)を接続する(2020.03.24)
コメント