Raspberry Pi Picoによる気温/ガスセンサー
Raspberry Pi Picoを使って気温/ガスメーターを作成しました。
機能
・気温表示
・ガスメーター表示
・一定値を超えるとブザーが鳴る
センサー
・Sunfounderのセンサーキットを使用。
ラズパイピコのADC(アナログ→デジタル変換)で、センサー出力A0を拾っています。
・ブザーはSIGが入力信号になります。
Sunfounderのセンサーでなくとも同じ構成のセンサーならば
代用は利くと思われます。
・センサー側の判定出力D0は未使用。
写真
接続
コードはMicroPythonで記述しました。
コード(main.py)
from machine import Pin,PWM
import time
import math
from i2c_lcd1602 import I2cLcd
VREF = 3.3
SENSOR_ADC0 = 1
tempsensor = machine.ADC( SENSOR_ADC0 )
SW_PIN_TEMP=28
sw_temp = Pin(SW_PIN_TEMP,Pin.IN)
SENSOR_ADC1 = 0
gassensor = machine.ADC( SENSOR_ADC1 )
SW_PIN_GAS=19
sw_gas = Pin(SW_PIN_GAS,Pin.IN)
I2C_ADDR = 0x27
sda = machine.Pin(14)
scl = machine.Pin(15)
i2c = machine.I2C(1,sda=sda, scl=scl, freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
BUZZ_PIN = 21
FREQ = 400
DUTY = 50
buzz = PWM( Pin( BUZZ_PIN ) )
buzz.freq( FREQ )
while True:
temp = tempsensor.read_u16()
gas = gassensor.read_u16()
Vr = VREF * float(temp) / 65535
Rt = 10000 * Vr / (VREF - Vr)
temp = 1/(((math.log(Rt / 10000)) / 3950) + (1 / (273.15+25)))
temp = temp - 273.15-7
print ('temperature = ', temp, 'C')
print ('gass = ', gas, 'C')
if (sw_temp.value() == 0):
print("ON.")
if (sw_gas.value() == 0):
print("ON.")
lcd.putstr("{:.2f}C".format(temp)+' '+"{:.0f}".format(gas))
if (temp > 30 or gas > 45000):
DUTY = 50
pwm_out = int( DUTY / 100 * 65535 )
buzz.duty_u16( pwm_out )
time.sleep(1)
time.sleep(1)
lcd.clear()
DUTY = 0
pwm_out = int( DUTY / 100 * 65535 )
buzz.duty_u16( pwm_out )
time.sleep(0.01)
注意と補足
LCD表示に必要な「i2c_lcd1602.py」は、Sunfounderのセンサーキットにサンプルで付いていたものです。ブログにはアップしていません。
LCDは、PCF8574T(NXP)というI2Cを8本の制御信号に拡張するI/Oエキスパンダーを使った物でした。
i2c_lcd1602.pyは、Pico内のmain.pyと同じ階層、またはlibフォルダを新規作成してその下に置くと参照されるようです。