张高兴的 Windows 10 IoT 开发笔记:BH1750FVI 光照度传感器
BH1750FVI 是一款 IIC 接口的数字型光强度传感器集成电路。下面介绍一下其在 Windows 10 IoT Core 环境下的用法。
项目运行在 Raspberry Pi 2/3 上,使用 C# 进行编码。
1. 准备
包含 BH1750FVI 的传感器,这里选择的是淘宝上最多的 GY-30;Raspberry Pi 2/3 一块,环境为 Windows 10 IoT Core;公母头杜邦线 4-5 根
2. 连线
Raspberry Pi 2/3 的引脚如图
由于采用的是 IIC 通信方式,因此我们需要把 GY-30 上的 SDA 与 Pin3 相连,SCL 与 Pin5 相连。VCC 接 3.3V,GND 接地。ADD 决定了传感器的地址,将其连接至 VCC ≥ 0.7 V 的时候,地址为 0x5C,接地时为 0x23。可以不连接。
SDA - Pin3
SCL - Pin5
VCC - 3.3V
GND - GND
3. 代码
GitHub : https://github.com/ZhangGaoxing/windows-iot-demo/tree/master/BH1750FVIDemo
需要新建一个 Windows 通用 项目 ,并且添加引用 Windows IoT Extensions for the UWP
在项目中添加一个 C# 代码文件 BH1750FVI.cs,代码如下
using System;
using System.Threading.Tasks;
using Windows.Devices.I2c; namespace BH1750FVIDemo
{
/// <summary>
/// I2C Address Setting
/// </summary>
enum AddressSetting
{
/// <summary>
/// ADD Pin connect to high power level
/// </summary>
AddPinHigh = 0x5C,
/// <summary>
/// ADD Pin connect to low power level
/// </summary>
AddPinLow = 0x23
}; /// <summary>
/// The mode of measuring
/// </summary>
enum MeasurementMode
{
/// <summary>
/// Start measurement at 1 lx resolution
/// </summary>
ContinuouslyHighResolutionMode = 0x10,
/// <summary>
/// Start measurement at 0.5 lx resolution
/// </summary>
ContinuouslyHighResolutionMode2 = 0x11,
/// <summary>
/// Start measurement at 4 lx resolution
/// </summary>
ContinuouslyLowResolutionMode = 0x13,
/// <summary>
/// Start measurement at 1 lx resolution once
/// </summary>
OneTimeHighResolutionMode = 0x20,
/// <summary>
/// Start measurement at 0.5 lx resolution once
/// </summary>
OneTimeHighResolutionMode2 = 0x21,
/// <summary>
/// Start measurement at 4 lx resolution once
/// </summary>
OneTimeLowResolutionMode = 0x23
} /// <summary>
/// Setting light transmittance
/// </summary>
enum LightTransmittance
{
Fifty,
Eighty,
Hundred,
Hundred_Twenty,
Hundred_Fifty,
Two_Hundred
} class BH1750FVI
{
I2cDevice sensor;
private byte sensorAddress;
private byte sensorMode;
private byte sensorResolution = ;
private double sensorTransmittance = ; private byte registerHighVal = 0x42;
private byte registerLowVal = 0x65; /// <summary>
/// Constructor
/// </summary>
/// <param name="address">Enumeration type of AddressSetting</param>
/// <param name="mode">Enumeration type of MeasurementMode</param>
public BH1750FVI(AddressSetting address, MeasurementMode mode)
{
sensorAddress = (byte)address;
sensorMode = (byte)mode; if (mode == MeasurementMode.ContinuouslyHighResolutionMode2 || mode == MeasurementMode.OneTimeHighResolutionMode2)
{
sensorResolution = ;
}
} /// <summary>
/// Constructor
/// </summary>
/// <param name="address">Enumeration type of AddressSetting</param>
/// <param name="mode">Enumeration type of MeasurementMode</param>
/// <param name="transmittance">Enumeration type of LightTransmittance</param>
public BH1750FVI(AddressSetting address, MeasurementMode mode, LightTransmittance transmittance)
{
sensorAddress = (byte)address;
sensorMode = (byte)mode; if (mode == MeasurementMode.ContinuouslyHighResolutionMode2 || mode == MeasurementMode.OneTimeHighResolutionMode2)
{
sensorResolution = ;
} switch (transmittance)
{
case LightTransmittance.Fifty:
{
registerHighVal = 0x44;
registerLowVal = 0x6A;
sensorTransmittance = 0.5;
}
break;
case LightTransmittance.Eighty:
{
registerHighVal = 0x42;
registerLowVal = 0x76;
sensorTransmittance = 0.8;
}
break;
case LightTransmittance.Hundred:
{
registerHighVal = 0x42;
registerLowVal = 0x65;
}
break;
case LightTransmittance.Hundred_Twenty:
{
registerHighVal = 0x41;
registerLowVal = 0x7A;
sensorTransmittance = 1.2;
}
break;
case LightTransmittance.Hundred_Fifty:
{
registerHighVal = 0x41;
registerLowVal = 0x7E;
sensorTransmittance = 1.5;
}
break;
case LightTransmittance.Two_Hundred:
{
registerHighVal = 0x41;
registerLowVal = 0x73;
sensorTransmittance = ;
}
break;
}
} /// <summary>
/// Initialize BH1750FVI
/// </summary>
public async Task InitializeAsync()
{
var settings = new I2cConnectionSettings(sensorAddress);
settings.BusSpeed = I2cBusSpeed.FastMode; var controller = await I2cController.GetDefaultAsync();
sensor = controller.GetDevice(settings); sensor.Write(new byte[] { 0x01 });
sensor.Write(new byte[] { registerHighVal });
sensor.Write(new byte[] { registerLowVal });
} /// <summary>
/// Read data from BH1750FVI
/// </summary>
/// <returns>A double type contains data</returns>
public double Read()
{
byte[] readBuf = new byte[]; sensor.WriteRead(new byte[] { sensorMode }, readBuf); byte temp = readBuf[];
readBuf[] = readBuf[];
readBuf[] = temp; double result = BitConverter.ToUInt16(readBuf, ) / (1.2 * sensorResolution * sensorTransmittance); return result;
} /// <summary>
/// Cleanup
/// </summary>
public void Dispose()
{
sensor.Dispose();
}
}
}
下面解释如何使用
代码包含三个枚举类型,两个构造函数,三个方法。
第一步调用构造函数将 BH1750FVI 实例化。
第二步调用 InitializeAsync() 初始化 I2C 设备
第三步调用 Read() 读取数据,返回的是一个 double 类型的值
当需要关闭设备时,调用 Dispose()
张高兴的 Windows 10 IoT 开发笔记:BH1750FVI 光照度传感器的更多相关文章
- 张高兴的 Windows 10 IoT 开发笔记:使用 ADS1115 读取模拟信号
考虑到 Raspberry Pi 读取模拟信号是很烦人的事情,更何况是在没人玩的 Windows 10 IoT 下,所以准备正儿八经的写点东西. 需求:使用 Raspberry Pi 读取输出模拟信号 ...
- 张高兴的 Windows 10 IoT 开发笔记:使用 Lightning 中的软件 PWM 驱动 RGB LED
感觉又帮 Windows 10 IoT 开荒了,所以呢,正儿八经的写篇博客吧.其实大概半年前就想写的,那时候想做个基于 Windows 10 IoT 的小车,但树莓派原生不支持 PWM 啊.百度也搜不 ...
- 张高兴的 Windows 10 IoT 开发笔记:HC-SR04 超声波测距模块
HC-SR04 采用 IO 触发测距.下面介绍一下其在 Windows 10 IoT Core 环境下的用法. 项目运行在 Raspberry Pi 2/3 上,使用 C# 进行编码. 1. 准备 H ...
- 张高兴的 Windows 10 IoT 开发笔记:部署 ASP.NET Core 2 应用
今天是大年初二,都去走亲戚了吧,享受一下这难得的能和亲友相聚的时光.而我就不一样了,今天一回到家就又开始瞎折腾了,哈哈哈. 问题背景 最近花了点时间用 ASP.NET Core 2 写了个个人博客,中 ...
- 张高兴的 Windows 10 IoT 开发笔记:串口红外编解码模块 YS-IRTM
This is a Windows 10 IoT Core project on the Raspberry Pi 2/3, coded by C#. GitHub: https://github.c ...
- 张高兴的 Windows 10 IoT 开发笔记:无线收发芯片 nRF24L01
This is a Windows 10 IoT Core project on the Raspberry Pi 2/3, coded by C#. GitHub:https://github.co ...
- 张高兴的 Windows 10 IoT 开发笔记:FM 电台模块 KT0803L
This is a Windows 10 IoT Core project on the Raspberry Pi 2/3, coded by C#. GitHub:https://github.co ...
- 张高兴的 Windows 10 IoT 开发笔记:0.96 寸 I2C OLED
This is a Windows 10 IoT Core project on the Raspberry Pi 2/3, coded by C#. GitHub:https://github.co ...
- 张高兴的 Windows 10 IoT 开发笔记:使用 MAX7219 驱动数码管
This is a Windows 10 IoT Core project on the Raspberry Pi 2/3, coded by C#. GitHub:https://github.co ...
随机推荐
- JTable用法-实例
前几篇文章介绍了JTable的基本用法,本文实现一个简单的JTable,算是前文的一个总结,并造福供拷贝党们. Swing-JTable用法-入门 Swing-JTable的渲染器与编辑器使用demo ...
- 201521123032 《Java程序设计》第9周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 1.常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己 ...
- JVM(三)内存回收(一)
最近花了相当长一段时间在看Hotspot JVM的GC和内存分配,本节先总结和回顾一下内存回收的相关知识点,内存的分配放到下节再讨论. 一.什么是JVM的GC GC即Garbage Collectio ...
- Servlet第二篇【Servlet调用图、Servlet细节、ServletConfig、ServletContext】
Servlet的调用图 前面我们已经学过了Servlet的生命周期了,我们根据Servlet的生命周期画出Servlet的调用图加深理解 Servlet的细节 一个已经注册的Servlet可以被多次映 ...
- mongodb 3.4 集群搭建升级版 五台集群
最新版mongodb推荐使用yaml语法来做配置,另外一些旧的配置在最新版本中已经不在生效,所以我们在生产实际搭建mongodb集群的时候做了一些改进.如果大家不熟悉什么是分片.副本集.仲裁者的话请先 ...
- 简单实用的CSS网页布局中文排版技巧
由于汉字的特殊性,在css网页布局中,中文排版有别于英文排版.排版是一个麻烦的问题,小编认为,作为一个优秀的网页设计师和网页制作人员,掌握一些简单的中文排版技巧是不可或缺的,所以今天特意总结了几个简单 ...
- JavaScript随机数类型
1.Math.random(); 结果为0-1间的一个随机数(包括0,不包括1) 2.Math.floor(num); 参数num为一个数值,函数结果为num的整数部分. 3.Math.round(n ...
- 【京东账户】——Mysql/PHP/Ajax爬坑之添加购物车
一.引言 做京东账户项目中的购物车模块,功能之一就是添加购物车.要用到的是Apach环境,Mysql.PHP以及Ajax. 预计效果:用户点击->"加入购物车" 添加成功 ...
- E - 今年暑假不AC HDU - 2037
"今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" "@#$%^&* ...
- Racing Car Computer dp
Racing Car Computer Input: Standard Input Output: Standard Output The racing cars of today are equ ...