0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RaspberryPiでADT7410で計測した温度をLCDに表示

Posted at

概要

RaspberryPiで温度計測センサADT7410とst7032i搭載LCDを制御し、温度をLCDに表示させるコードです。温度が上がると▲アイコンを表示、下がると▼を表示します。

動作動画

ADT7410チップをさわっている間は温度が上がるため▲アイコン表示、指を離すと温度が下がり▼アイコンが表示される仕様です。

RaspberryPi ADT7410

ソース

たまにLCDの初期化でIOErrorが発生すると思いますが、例外処理していません。そんな時は再度実行すると動作します。

ADT7410.py
import smbus
from time import sleep

global old_val

def read_adt7410():
    data = bus.read_word_data(adt7410,reg_data)
    data2 = (data&0x00ff)<<8 | (data&0xff00)>>8
    data2 >>= 3

    if data2&0x1000:
        temp = (data2-8192) * 0.0625
    else:
        temp = data2 * 0.0625
    return temp


def setup_lcd():
    sleep(0.04)
    bus.write_i2c_block_data(lcd,command_inst,[0x39,0x39,0x1c,0x78,0x5d,0x6c])
    sleep(0.2)
    bus.write_i2c_block_data(lcd,command_inst,[0x0c,0x01])
    sleep(0.001)

def write_string(s):
    bus.write_byte_data(lcd,command_inst,0x01)
    sleep(0.001)
    for c in list(s):
        bus.write_byte_data(lcd,command_data,ord(c))

def icon(i):
    if i==0:
        bus.write_byte_data(lcd,command_inst,0x47)
        bus.write_byte_data(lcd,command_data,0x08)
    elif i==1:
        bus.write_byte_data(lcd,command_inst,0x47)
        bus.write_byte_data(lcd,command_data,0x10)




bus = smbus.SMBus(1)
adt7410 = 0x48
reg_data = 0x00

lcd = 0x3e
command_inst = 0x00
command_data = 0x40
contrust = 40
old_val = read_adt7410()

setup_lcd()

try:
    while True:
        val = read_adt7410()

        if val > old_val:
            icon(1)
        else:
            icon(0)

        old_val = val
        write_string(str(val))

        sleep(0.5)

except KeyboardInterrupt:
    pass

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?