嘗試--樹莓派i2c接口SHT30的使用

給樹莓派安裝SHT30

由于買來的SHT30已經接好電路,直接用杜邦線連接樹莓派即可。

樹莓派SHT30連線

完美

樹莓派開啟i2c

參考?https://blog.csdn.net/panwen1111/article/details/81044428 這里不多贅述。

檢查是否開啟成功 在命令行輸入 lsmod 出現如下說明加載成功。

安裝i2c-tools

sudo apt-get install i2c-tools

簡單介紹一下:

i2c-tools僅有四條命令

1.i2c-tool查詢i2c設備:

sudo i2cdetect -y 1? ?(樹莓派1用 -y 0? 樹莓派2用 -y 1? ?參考https://blog.csdn.net/xukai871105/article/details/15029843)

地址為0x44的就是我們的SHT30。

2.所有寄存器內容導出

i2cdump -y 1 0x44

? ?-y? ? ? ? ?代表取消用戶交互過程,直接執行指令;

????1? ? ?????代表I2C總線編號;

? ? 0x44? ? 代表I2C設備從機地址,

3.寄存器內容寫入

i2cset -y 1 0x44 0x00 0x13

?-y? ??????代表曲線用戶交互過程,直接執行指令

1? ? ?????代表I2C總線編號

0x44? ? 代表I2C設備地址

0x00? ? 代表存儲器地址

0x13? ? 代表存入的數據

4.寄存器內容讀取

i2cget -y 1 0x44 0x00

?-y? ??????代表曲線用戶交互過程,直接執行指令

1? ? ?????代表I2C總線編號

0x44? ? 代表I2C設備地址

0x00? ? 代表存儲器地址

輸出溫濕度

根據SHT30手冊,我們在寄存器中讀取的數值并不是真實的溫濕度數值。數據需要經行轉換。

SHT30手冊截圖

python代碼實現

實現一(根據SHT31修改 十分完美 基于python3):

import smbus

import time

i2c = smbus.SMBus(1)

addr=0x44

i2c.write_byte_data(addr,0x23,0x34)

time.sleep(0.5)

while 1:

? ? i2c.write_byte_data(addr,0xe0,0x0)

? ? data = i2c.read_i2c_block_data(addr,0x0,6)

? ? rawT = ((data[0]) << 8) | (data[1])

? ? rawR = ((data[3]) << 8) | (data[4])

? ? temp = -45 + rawT * 175 / 65535

? ? print (str(temp) +"C")

? ? RH = 100 * rawR / 65535

? ? print (str(RH) +"%")

? ? time.sleep(1)

? ? print ("*************")

實現二(根據SHT20修改 半成品 基于python2):

import commands

import time

while 1:

? ? status_temp,temp_reg=commands.getstatusoutput('i2cget -f -y 1 0x44 0x00')

? ? temp_int = int(temp_reg,16)

? ? temp = (temp_int<<8)|temp_int

? ? T=-45+temp*175/65535

? ? print "Current Temperature=",T,"℃"

? ? time.sleep(0.5)

? ? status_hump,hump_reg=commands.getstatusoutput('i2cget -f -y 1 0x44 0x30')

? ? hump_int=int(hump_reg,16)

? ? hump=(hump_int<<8)|hump_int

? ? H= 100 * hump / 65535

? ? print"Current Hump=",H,'%'

? ? print "--------------------------------------------------------"

? ? time.sleep(0.5)


ps:這份代碼存在以下問題:

1.最主要的問題:無法確定濕度數據的寄存器地址,寄存器中經常沒有值。查詢到的0x44i2c設備地址應該無誤(如果地址錯誤會全是xx),但是寄存器中的數據全為0x00。溫度的地址為0x00,但是濕度的地址一直在變化甚至沒有濕度的數據。

2.使用commands.getstatusoutput庫時,如果把溫度和濕度command命令放在一起,則會出現以下問題

? 其解決辦法就是使用time庫延時0.5s 即可。

? 剛入門實屬小白一枚 歡迎大家指點幫助!!!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容