本文主要是介绍物联网开发121 - Micropython ESP32 C3连接TCS34725颜色识别传感器模块(I2C通信),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、目的
这一节我们来学习如何使用合宙ESP32 C3,连接TCS34725颜色识别传感器模块,识别颜色。下面我们一起来学习一下吧!
二、环境
ESP32 C3开发板(MicroPython v1.19.1 on 2022-06-18)+ TCS34725颜色识别传感器模块 + WS2812彩色灯珠模块 + 几根杜邦线 + Win10商业版
ESP32 C3和TCS34725模块接线方法:
三、示例代码
example.py
from machine import Pin,I2C
from tcs34725 import TCS34725 # 导入颜色识别模块驱动
from neopixel import NeoPixel # mircropython自带彩色灯珠驱动
import time# 创建I2C对象
i2c = I2C(0,scl = Pin(5),sda = Pin(4),freq = 400_000)
# 创建彩色LED对象
npp = NeoPixel(Pin(8,Pin.OUT),24)
# 打印扫描出的I2C对象
print("I2C Bus Scan: ", i2c.scan(), "\n") # 输出I2C Bus Scan: [41],41的十六进制29
# 创建tcs34725颜色识别模块对象
tcs = TCS34725(i2c,0x29)'''
G R B 5 6 5 2字节= 16位 = 65536G = 5 2^5 = 32
R = 6 2^6 = 64
b = 5 2^5 = 32'''
# 创建RGB颜色计算函数
def Colour():try:red,green,blue,colour = tcs.read(True) # 读取颜色值if colour != 0: # 如果colour不等于0Red = int((red / colour) * 32) # 计算红色的值Green = int((green / colour) * 64) # 计算绿色的值Blue = int((blue / colour) * 32) # 计算蓝色的值return Red,Green,Blue # 返回RGB值else: return 0,0,0except :print("数据异常!")RuntimeError# 创建显示颜色识别
def dispaly(colour):red,green,blue = colournpp.fill((red,green,blue)) # 填充颜色npp.write() # 写入颜色print("red: %.2d green:%.2d blue:%.2d" % (red,green,blue))time.sleep(0.1)# 初始化LED灯
def Init():for i in range(0,24,1):npp[i]=(i*10,255,100) # 获取颜色值npp.write() # 写入颜色time.sleep(0.2) # 等待0.2秒def main():print("%#x"%(i2c.scan()[0])) # 输出29Init() # 初始化while True:dispaly(Colour()) # 显示颜色if __name__ == "__main__":main()
演示效果:
四、tcs34725颜色识别模块驱动
tcs34725.py
from machine import I2C
import time
import ustructconst = lambda x:x_COMMAND_BIT = const(0x80)
_REGISTER_ENABLE = const(0x00)
_REGISTER_ATIME = const(0x01)
_REGISTER_AILT = const(0x04)
_REGISTER_AIHT = const(0x06)
_REGISTER_ID = const(0x12)
_REGISTER_APERS = const(0x0c)
_REGISTER_CONTROL = const(0x0f)
_REGISTER_SENSORID = const(0x12)
_REGISTER_STATUS = const(0x13)
_REGISTER_CDATA = const(0x14)
_REGISTER_RDATA = const(0x16)
_REGISTER_GDATA = const(0x18)
_REGISTER_BDATA = const(0x1a)
_ENABLE_AIEN = const(0x10)
_ENABLE_WEN = const(0x08)
_ENABLE_AEN = const(0x02)
_ENABLE_PON = const(0x01)_GAINS = (1, 4, 16, 60)
_CYCLES = (0, 1, 2, 3, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60)class TCS34725:def __init__(self, i2c, address=None):self.i2c = i2cself.address = addressself._active = Falseself.integration_time(2.4)sensor_id = self.i2c.scan()[0]if sensor_id not in (0x29,0x10):raise RuntimeError("wrong sensor id 0x{:x}".format(sensor_id))def _register8(self, register, value=None):register |= _COMMAND_BITif value is None:return self.i2c.readfrom_mem(self.address, register, 1)[0]data = ustruct.pack('<B', value)self.i2c.writeto_mem(self.address, register, data)def _register16(self, register, value=None):register |= _COMMAND_BITif value is None:data = self.i2c.readfrom_mem(self.address, register, 2)return ustruct.unpack('<H', data)[0]data = ustruct.pack('<H', value)self.i2c.writeto_mem(self.address, register, data)def active(self, value=None):if value is None:return self._activevalue = bool(value)if self._active == value:returnself._active = valueenable = self._register8(_REGISTER_ENABLE)if value:self._register8(_REGISTER_ENABLE, enable | _ENABLE_PON)time.sleep_ms(3)self._register8(_REGISTER_ENABLE,enable | _ENABLE_PON | _ENABLE_AEN)else:self._register8(_REGISTER_ENABLE,enable & ~(_ENABLE_PON | _ENABLE_AEN))def sensor_id(self):return self.i2c.scan()[0]def integration_time(self, value=None):if value is None:return self._integration_timevalue = min(614.4, max(2.4, value))cycles = int(value / 2.4)self._integration_time = cycles * 2.4return self._register8(_REGISTER_ATIME, 256 - cycles)def gain(self, value):if value is None:return _GAINS[self._register8(_REGISTER_CONTROL)]if value not in _GAINS:raise ValueError("gain must be 1, 4, 16 or 60")return self._register8(_REGISTER_CONTROL, _GAINS.index(value))def _valid(self):return bool(self._register8(_REGISTER_STATUS) & 0x01)def read(self, raw=False):was_active = self.active()self.active(True)while not self._valid():time.sleep_ms(int(self._integration_time + 0.9))data = tuple(self._register16(register) for register in (_REGISTER_RDATA,_REGISTER_GDATA,_REGISTER_BDATA,_REGISTER_CDATA,))self.active(was_active)if raw:return datareturn self._temperature_and_lux(data)def _temperature_and_lux(self, data):r, g, b, c = datax = -0.14282 * r + 1.54924 * g + -0.95641 * by = -0.32466 * r + 1.57837 * g + -0.73191 * bz = -0.68202 * r + 0.77073 * g + 0.56332 * bd = x + y + zn = (x / d - 0.3320) / (0.1858 - y / d)cct = 449.0 * n**3 + 3525.0 * n**2 + 6823.3 * n + 5520.33return cct, ydef threshold(self, cycles=None, min_value=None, max_value=None):if cycles is None and min_value is None and max_value is None:min_value = self._register16(_REGISTER_AILT)max_value = self._register16(_REGISTER_AILT)if self._register8(_REGISTER_ENABLE) & _ENABLE_AIEN:cycles = _CYCLES[self._register8(_REGISTER_APERS) & 0x0f]else:cycles = -1return cycles, min_value, max_valueif min_value is not None:self._register16(_REGISTER_AILT, min_value)if max_value is not None:self._register16(_REGISTER_AIHT, max_value)if cycles is not None:enable = self._register8(_REGISTER_ENABLE)if cycles == -1:self._register8(_REGISTER_ENABLE, enable & ~(_ENABLE_AIEN))else:self._register8(_REGISTER_ENABLE, enable | _ENABLE_AIEN)if cycles not in _CYCLES:raise ValueError("invalid persistence cycles")self._register8(_REGISTER_APERS, _CYCLES.index(cycles))def interrupt(self, value=None):if value is None:return bool(self._register8(_REGISTER_STATUS) & _ENABLE_AIEN)if value:raise ValueError("interrupt can only be cleared")self.i2c.writeto(self.address, b'\xe6')def html_rgb(data):r, g, b, c = datared = pow((int((r/c) * 256) / 255), 2.5) * 255green = pow((int((g/c) * 256) / 255), 2.5) * 255blue = pow((int((b/c) * 256) / 255), 2.5) * 255return red, green, bluedef html_hex(data):r, g, b = html_rgb(data)return "{0:02x}{1:02x}{2:02x}".format(int(r),int(g),int(b))
五、模块购买地址
https://detail.tmall.com/item.htm?_u=cp01rchd77a&id=662903315676&spm=a1z09.2.0.0.2df52e8dTDETVohttps://detail.tmall.com/item.htm?_u=cp01rchd77a&id=662903315676&spm=a1z09.2.0.0.2df52e8dTDETVo
资料链接:
https://pan.baidu.com/s/1z_5qOfe-YMbj0TYSbDD-sQ?pwd=6668 提取码:6668
WS2812彩色灯珠彩色LED模块
https://item.taobao.com/item.htm?id=624152924451&ali_refid=a3_430582_1006:1307270063:N:tpvpl9C8RV8kxLRjzOGIrw%3D%3D:776ce3fda428974116ddefcf88b30610&ali_trackid=1_776ce3fda428974116ddefcf88b30610&spm=a230r.1.14.1#detailhttps://item.taobao.com/item.htm?id=624152924451&ali_refid=a3_430582_1006:1307270063:N:tpvpl9C8RV8kxLRjzOGIrw%3D%3D:776ce3fda428974116ddefcf88b30610&ali_trackid=1_776ce3fda428974116ddefcf88b30610&spm=a230r.1.14.1#detail
或者这种
https://item.taobao.com/item.htm?spm=a1z09.2.0.0.2df52e8dTDETVo&id=630294552846&_u=cp01rch8defhttps://item.taobao.com/item.htm?spm=a1z09.2.0.0.2df52e8dTDETVo&id=630294552846&_u=cp01rch8def
这篇关于物联网开发121 - Micropython ESP32 C3连接TCS34725颜色识别传感器模块(I2C通信)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!