“颜色”的版本间的差异
来自Labplus盛思维基百科
Tangliufeng(讨论 | 贡献) |
Jiangzhaohui(讨论 | 贡献) (→Arduino示例) |
||
第25行: | 第25行: | ||
=== 连接示意图 === | === 连接示意图 === | ||
=== Arduino示例 === | === Arduino示例 === | ||
+ | <pre style="color:blue"> | ||
+ | #include <Wire.h> | ||
+ | #include "SHT20.h" | ||
+ | |||
+ | DFRobot_SHT20 sht20; | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | Serial.begin(9600); | ||
+ | Serial.println("SHT20 Example!"); | ||
+ | sht20.initSHT20(); // Init SHT20 Sensor | ||
+ | delay(100); | ||
+ | sht20.checkSHT20(); // Check SHT20 Sensor | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | float humd = sht20.readHumidity(); // Read Humidity | ||
+ | float temp = sht20.readTemperature(); // Read Temperature | ||
+ | // Serial.print("Time:"); | ||
+ | // Serial.print(millis()); | ||
+ | Serial.print(" Temperature:"); | ||
+ | Serial.print(temp, 1); | ||
+ | Serial.print("C"); | ||
+ | Serial.print(" Humidity:"); | ||
+ | Serial.print(humd, 1); | ||
+ | Serial.print("%"); | ||
+ | Serial.println(); | ||
+ | delay(1000); | ||
+ | } | ||
+ | </pre> | ||
+ | |||
=== MicroPython示例 === | === MicroPython示例 === | ||
<pre style="color:blue"> | <pre style="color:blue"> |
2018年1月5日 (五) 17:07的版本
概述
基于不同颜色物体对于RGB光线反射率不同的原理来分辨物体的颜色RGB分量。采用I2C通讯,操作简单,直接输出被测物体RGB分量值。
技术参数
- 工作电压:VCC 3.3-5V
- 接口方式:I2C接口
- 模块尺寸:24x46x7.5mm
引脚定义
VCC | 电源 |
SDA | I2C数据 |
SCL | I2C时钟 |
GND | 地 |
使用教程
在使用时被测物体须要置于光线传感器1CM处,使其能反射光线。要注意环境光线对颜色测量影响,最好在密闭的黑暗环境下测量
连接示意图
Arduino示例
#include <Wire.h> #include "SHT20.h" DFRobot_SHT20 sht20; void setup() { Serial.begin(9600); Serial.println("SHT20 Example!"); sht20.initSHT20(); // Init SHT20 Sensor delay(100); sht20.checkSHT20(); // Check SHT20 Sensor } void loop() { float humd = sht20.readHumidity(); // Read Humidity float temp = sht20.readTemperature(); // Read Temperature // Serial.print("Time:"); // Serial.print(millis()); Serial.print(" Temperature:"); Serial.print(temp, 1); Serial.print("C"); Serial.print(" Humidity:"); Serial.print(humd, 1); Serial.print("%"); Serial.println(); delay(1000); }
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)