“颜色”的版本间的差异
来自Labplus盛思维基百科
Tangliufeng(讨论 | 贡献) (→使用教程) |
Tangliufeng(讨论 | 贡献) (→MicroPython示例) |
||
第50行: | 第50行: | ||
while True: | while True: | ||
− | c = getColor() | + | c = getColor() # 返回3字节依次为RGB值 |
print("R: %d, G: %d, B: %d" % (c[0], c[1], c[2])) | print("R: %d, G: %d, B: %d" % (c[0], c[1], c[2])) | ||
sleep(200) | sleep(200) |
2017年12月20日 (三) 16:24的版本
概述
基于不同颜色物体对于RGB光线反射率不同的原理来分辨物体的颜色RGB分量。采用I2C通讯,操作简单,直接输出被测物体RGB分量值。
技术参数
- 工作电压:3.3V~5V
- 接口方式:I2C接口
- 模块尺寸:24x46x7.5mm
引脚定义
VCC | 电源 |
SDA | I2C数据 |
SCL | I2C时钟 |
GND | 地 |
使用教程
在使用时被测物体须要置于光线传感器1CM处,使其能反射光线。要注意环境光线对颜色测量影响,最好在密闭的黑暗环境下测量
连接示意图
Arduino示例
MicroPython示例
from microbit import * import math def getColor(): color = [0, 0, 0] i2c.write(0x0a, bytearray([1])) sleep(100) i2c.write(0x0a, bytearray([2])) state = i2c.read(0x0a, 1) if state[0] == 3: i2c.write(0x0a, bytearray([3])) c = i2c.read(0x0a, 6) color[0] = c[5]*256+c[4] # color R color[1] = c[1]*256+c[0] # color G color[2] = c[3]*256+c[2] # color B maxColor = max(color[0], color[1], color[2]) if maxColor > 255: scale = 255/maxColor color[0] = int(color[0]*scale) color[1] = int(color[1]*scale) color[2] = int(color[2]*scale) return color while True: c = getColor() # 返回3字节依次为RGB值 print("R: %d, G: %d, B: %d" % (c[0], c[1], c[2])) sleep(200)