感測器簡介
CCS-811是一種超低功耗的數位氣體感測器,集成了CCS801感測器和8位MCU(帶模數轉換器(ADC)),用來檢測室內的空氣品質,包括二氧化碳(CO2)和廣泛的揮發性有機化合物氣體(VOCs)。
空氣品質感測器CCS811的特點:
超低功耗,可用在電池工作設備
靈敏度高,加熱快
智慧演算法計算TVOC/eCO2數值
輸出I2C信號,直接與主系統通信
空氣品質感測器CJMCU-811的主要特點:
監測室內空氣品質的金屬氧化物(MOX)感測器
集成了8位元MCU,用於運行第一級演算法
集成了12位元ADC,用於感測器讀數和數位化轉換
I2C從屬介面可直接接入主控系統
復位/中斷控制
電路圖
程式列表
Python - CCS811.py
from micropython import const
from machine import I2C, Pin
class GasSensor:
READ_DELAY = 10000
CCS811_HW_ID_CODE = 0x81
CCS811_REF_RESISTOR = 100000.0
# default address
CCS811_ADDR = const(0x5A)
# Commands
CCS811_STATUS = const(0x00)
CCS811_MEAS_MODE = const(0x01)
CCS811_ALG_RESULT_DATA = const(0x02)
CCS811_RAW_DATA = const(0x03)
CCS811_ENV_DATA = const(0x05)
CCS811_NTC = const(0x06)
CCS811_THRESHOLDS = const(0x10)
CCS811_BASELINE = const(0x11)
CCS811_HW_ID = const(0x20)
CCS811_HW_VERSION = const(0x21)
CCS811_FW_BOOT_VERSION = const(0x23)
CCS811_FW_APP_VERSION = const(0x24)
CCS811_ERROR_ID = const(0xE0)
CCS811_APP_START = const(0xF4)
CCS811_SW_RESET = const(0xFF)
def __init__(self, i2c, address = CCS811_ADDR):
self.i2c = i2c
self.address = address
self.tempOffset = 0
self.temp =0
self.configure_ccs811()
def configure_ccs811(self):
# See figure 22 in datasheet: Bootloader Register Map
# Check HW_ID register (0x20) - correct value 0x81
hardware_id = bytearray(1)
cmd = self.CCS811_HW_ID
self.i2c.send(cmd,self.address)
self.i2c.recv(hardware_id,self.address)
if (hardware_id[0] != self.CCS811_HW_ID_CODE):
raise ValueError('Wrong Hardware ID.')
# Check Status Register (0x00) to see if valid application present-
status = bytearray(1)
cmd = self.CCS811_STATUS
self.i2c.send(cmd, self.address)
self.i2c.recv(status,self.address)
# See figure 12 in datasheet: Status register: Bit 4: App valid
if not (status[0] >> 4) & 0x01:
raise ValueError('Application not valid.')
self.tempOffset = self.calculateTemperature() - 25.0
cmd = self.CCS811_APP_START
# Application start. Write with no data to App_Start (0xF4)
self.i2c.send(cmd,self.address)
# Set drive mode 1 - see Figure 13 in datasheet: Measure Mode Register (0x01)
cmd = bytearray([0x01, 0x18])
self.i2c.send(cmd,self.address)
def print_error(self):
"""Error code. """
error = bytearray(1)
cmd = self.CCS811_ERROR_ID
self.i2c.send(cmd,self.address)
self.i2c.recv(error,self.address)
message = 'Error: '
if (error[0] >> 5) & 1:
message += 'HeaterSupply '
elif (error[0] >> 4) & 1:
message += 'HeaterFault '
elif (error[0] >> 3) & 1:
message += 'MaxResistance '
elif (error[0] >> 2) & 1:
message += 'MeasModeInvalid '
elif (error[0] >> 1) & 1:
message += 'ReadRegInvalid '
elif (error[0] >> 0) & 1:
message += 'MsgInvalid '
else :
return False
print(message)
return True
def available(self):
status = bytearray(1)
cmd = self.CCS811_STATUS
self.i2c.send(cmd,self.address)
self.i2c.recv(status,self.address)
return status[0]
def ReadData(self):
""" Equivalent Carbone Dioxide in parts per millions. Clipped to 400 to 8192ppm."""
""" Total Volatile Organic Compound in parts per billion. """
buf = bytearray(8)
cmd = self.CCS811_ALG_RESULT_DATA
self.i2c.send(cmd,self.address)
self.i2c.recv(buf,self.address)
self.eCO2 = (buf[0] << 8) | (buf[1])
self.TVOC = (buf[2] << 8) | (buf[3])
# print("Data: ")
# print("".join("\\x%02x" % i for i in buf))
def calculateTemperature(self):
buf = bytearray(4)
cmd = self.CCS811_NTC
self.i2c.send(cmd,self.address)
self.i2c.recv(buf,self.address)
vref = (buf[0] << 8) | buf[1]
vntc = (buf[2] << 8) | buf[3]
# from ams ccs811 app note
ntc_temp = 0
if vref>0 :
rntc = vntc * self.CCS811_REF_RESISTOR / vref
ntc_temp = (rntc / self.CCS811_REF_RESISTOR) # 1
ntc_temp /= 3380 # 2
ntc_temp += 1.0 / (25 + 273.15) # 3
ntc_temp = 1.0 / ntc_temp # 4
ntc_temp -= 273.15 # 5
ntc_temp -= self.tempOffset
return ntc_temp
def reset(self):
""" Initiate a software reset. """
seq = bytearray([self.CCS811_SW_RESET, 0x11, 0xE5, 0x72, 0x8A])
self.i2c.send(seq,self.address)
Python - ePy-Lite_CCS811.py
"""
ePy-Lite_CCS811.py
ePy-Lite Sensor
-----------------
3V3 VCC
GND GND
P17_SCL0 SCL
P18_SDA0 SDA
GND WAK
INT
RST
ADD
"""
from machine import I2C, Pin
from machine import Switch #Get button KEY library
import utime
from CCS811 import GasSensor
# Start Function
if __name__=="__main__":
KeyA = Switch('keya') #Create button A
sensor_i2c = I2C(0,I2C.MASTER,baudrate=100000) #Create I2C0 Master Mode, Baudrate=100kHz
gas_sensor = GasSensor(sensor_i2c, address=GasSensor.CCS811_ADDR)
print('Starting CCS811 Read')
utime.sleep(5)
while True:
if not (gas_sensor.available() >> 4) & 0x01:
print('STATUS: 0x%2X' %(status[0]))
print('Application not valid.')
gas_sensor.print_error()
else :
temp = gas_sensor.calculateTemperature()
gas_sensor.ReadData()
print('{} - eCO2: {} ppm, TVOC: {} ppb, Temp: {} C'.format(gas_sensor_id, gas_sensor.eCO2, gas_sensor.TVOC, temp))
if KeyA.value() == True: #Press A Key
break
utime.sleep_ms(1000)
sensor_i2c.deinit()
# KeyA.deinit()
print("Exit gas measurement.")
執行結果