« CircuitPython/MicroPythonにi2c-LCD(AE-AQM0802)を接続する | トップページ | RaspberryPiのpython3でCircuitPythonのAPIを使用する »

2020年3月24日 (火)

CircuitPython/MicroPythonに温度センサーTMP102(i2c)を接続する

2020/2/24

CircuitPython TMP102(i2c)

CircuitPython TMP102(i2c)

概要

CircuitPython/MicroPythonに以下の温度センサーTMP102(i2c)を接続する。
(ホストPCとしてはubuntuを想定している)

TMP102搭載 デジタル温度センサモジュール

接続

以下のように接続する。

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のものと全く同じ。

参照情報

TMP102搭載 デジタル温度センサモジュール

ESP8266のMicroPythonに温度センサーTMP102(i2c)を接続する

Turtorial Examples for CircuitPython(Teensy4.0)
Teensy4.0にCircuitPythonをインストールする

Turtorial Examples for Nucleo-MicroPytho
NUCLEO-F446REにMicropythonをインストールする(v2)

CircuitPython 5.0.x - I2C

以上

|

« CircuitPython/MicroPythonにi2c-LCD(AE-AQM0802)を接続する | トップページ | RaspberryPiのpython3でCircuitPythonのAPIを使用する »

linux」カテゴリの記事

MicroPython」カテゴリの記事

Nucleo」カテゴリの記事

CircuitPython」カテゴリの記事

Teensy4.0」カテゴリの記事

コメント

コメントを書く



(ウェブ上には掲載しません)




« CircuitPython/MicroPythonにi2c-LCD(AE-AQM0802)を接続する | トップページ | RaspberryPiのpython3でCircuitPythonのAPIを使用する »