Skip to content

Instantly share code, notes, and snippets.

@tokida
Created May 20, 2016 19:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tokida/ba7323c56e5f47c149d83b1bd14866ae to your computer and use it in GitHub Desktop.
Save tokida/ba7323c56e5f47c149d83b1bd14866ae to your computer and use it in GitHub Desktop.
i2c接続のBME280センサーで取得した値を LCD(AQM0802A)に描画する
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 温度計の温度をLCDに表示する
import smbus
import time
from datetime import datetime
class BME280:
def __init__(self):
self.bus_number = 1
self.i2c_address = 0x76
self.bus = smbus.SMBus(self.bus_number)
self.digT = []
self.t_fine = 0.0
def writeReg(self, reg_address, data):
self.bus.write_byte_data(self.i2c_address, reg_address, data)
def get_calib_param(self):
calib = []
for i in range(0x88, 0x88+24):
calib.append(self.bus.read_byte_data(self.i2c_address, i))
calib.append(self.bus.read_byte_data(self.i2c_address, 0xA1))
for i in range(0xE1, 0xE1+7):
calib.append(self.bus.read_byte_data(self.i2c_address, i))
self.digT.append((calib[1] << 8) | calib[0])
self.digT.append((calib[3] << 8) | calib[2])
self.digT.append((calib[5] << 8) | calib[4])
for i in range(1, 2):
if self.digT[i] & 0x8000:
self.digT[i] = (-self.digT[i] ^ 0xFFFF) + 1
def readData(self):
data = []
for i in range(0xF7, 0xF7+8):
data.append(self.bus.read_byte_data(self.i2c_address, i))
temp_raw = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4)
return self.compensate_T(temp_raw)
def compensate_T(self, adc_T):
# global t_fine
self.t_fine
v1 = (adc_T / 16384.0 - self.digT[0] / 1024.0) * self.digT[1]
v2 = (adc_T / 131072.0 - self.digT[0] / 8192.0) * (adc_T / 131072.0 - self.digT[0] / 8192.0) * self.digT[2]
t_fine = v1 + v2
temperature = t_fine / 5120.0
# print "temp : %-6.2f ℃" % (temperature)
# print "%.1f" % (temperature)
return round(temperature,1)
def setup(self):
osrs_t = 1 # Temperature oversampling x 1
osrs_p = 1 # Pressure oversampling x 1
osrs_h = 1 # Humidity oversampling x 1
mode = 3 # Normal mode
t_sb = 5 # Tstandby 1000ms
filter = 0 # Filter off
spi3w_en = 0 # 3-wire SPI Disable
ctrl_meas_reg = (osrs_t << 5) | (osrs_p << 2) | mode
config_reg = (t_sb << 5) | (filter << 2) | spi3w_en
ctrl_hum_reg = osrs_h
self.writeReg(0xF2, ctrl_hum_reg)
self.writeReg(0xF4, ctrl_meas_reg)
self.writeReg(0xF5, config_reg)
class i2clcd:
i2c = smbus.SMBus(1)
addr = 0x3e
contrast = 42 # 0~63
def __init__(self):
self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0)
self.i2c.write_byte_data(self.addr, 0, 0x39) # function set(IS=1)
self.i2c.write_byte_data(self.addr, 0, 0x14) # internal osc
self.i2c.write_byte_data(self.addr, 0,
(0x70 | (self.contrast & 0x0f))) # contrast
self.i2c.write_byte_data(self.addr, 0,
(0x54 | ((self.contrast >> 4) & 0x03))) # contrast/icon/power
self.i2c.write_byte_data(self.addr, 0, 0x6B) # follower control
# self.i2c.write_byte_data(self.addr, 0, 0x6c) # follower control
time.sleep(0.2)
def clear(self):
self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0)
self.i2c.write_byte_data(self.addr, 0, 0x70) # Constract
self.i2c.write_byte_data(self.addr, 0, 0x0C) # Display On
self.i2c.write_byte_data(self.addr, 0, 0x01) # Clear Display
self.i2c.write_byte_data(self.addr, 0, 0x06) # Entry Mode Set
time.sleep(0.2)
def puts(self, msg):
self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0)
[self.i2c.write_byte_data(self.addr, 0x40, ord(c)) for c in msg]
def setaddress(self, line, col):
self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0)
self.i2c.write_byte_data(self.addr, 0, 0x80 | (0x40 if line > 0 else 0) | col)
def setcg(self, no, cg):
self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0)
self.i2c.write_byte_data(self.addr, 0, 0x40 | (no << 3))
[self.i2c.write_byte_data(self.addr, 0x40, c) for c in cg]
def putcg(self, line, col, no):
self.setaddress(line, col)
self.i2c.write_byte_data(self.addr, 0x40, no)
if __name__ == '__main__':
try:
# クラスの宣言
bme280 = BME280()
bme280.setup()
bme280.get_calib_param()
temperature = bme280.readData()
lcd = i2clcd()
# 度記号
cgchar0 = (0b00000,
0b00001,
0b01100,
0b10010,
0b10000,
0b10010,
0b01100)
lcd.setcg(0, cgchar0)
lcd.clear()
while True:
lcd.setaddress(0, 3)
lcd.puts(str(temperature))
lcd.putcg(0, 7, 0)
# 現在時間
lcd.setaddress(1, 0)
lcd.puts(datetime.now().strftime("%H:%M:%S"))
time.sleep(1)
except KeyboardInterrupt:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment