博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
张高兴的 Windows 10 IoT 开发笔记:BH1750FVI 光照度传感器
阅读量:5961 次
发布时间:2019-06-19

本文共 6239 字,大约阅读时间需要 20 分钟。

原文:

  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 :  

  需要新建一个 Windows 通用 项目 ,并且添加引用 Windows IoT Extensions for the UWP

  在项目中添加一个 C# 代码文件 BH1750FVI.cs,代码如下

using System;using System.Threading.Tasks;using Windows.Devices.I2c;namespace BH1750FVIDemo{    ///     /// I2C Address Setting    ///     enum AddressSetting    {        ///         /// ADD Pin connect to high power level        ///         AddPinHigh = 0x5C,        ///         /// ADD Pin connect to low power level         ///              AddPinLow = 0x23                };    ///     /// The mode of measuring    ///     enum MeasurementMode    {        ///         /// Start measurement at 1 lx resolution        ///         ContinuouslyHighResolutionMode = 0x10,        ///         /// Start measurement at 0.5 lx resolution        ///         ContinuouslyHighResolutionMode2 = 0x11,        ///         /// Start measurement at 4 lx resolution        ///         ContinuouslyLowResolutionMode = 0x13,        ///         /// Start measurement at 1 lx resolution once        ///         OneTimeHighResolutionMode = 0x20,        ///         /// Start measurement at 0.5 lx resolution once        ///         OneTimeHighResolutionMode2 = 0x21,        ///         /// Start measurement at 4 lx resolution once        ///         OneTimeLowResolutionMode = 0x23    }    ///     /// Setting light transmittance    ///     enum LightTransmittance    {        Fifty,        Eighty,        Hundred,        Hundred_Twenty,        Hundred_Fifty,        Two_Hundred    }    class BH1750FVI    {        I2cDevice sensor;        private byte sensorAddress;                                     private byte sensorMode;        private byte sensorResolution = 1;        private double sensorTransmittance = 1;        private byte registerHighVal = 0x42;        private byte registerLowVal = 0x65;        ///         /// Constructor        ///         /// Enumeration type of AddressSetting        /// Enumeration type of MeasurementMode        public BH1750FVI(AddressSetting address, MeasurementMode mode)        {            sensorAddress = (byte)address;            sensorMode = (byte)mode;            if (mode == MeasurementMode.ContinuouslyHighResolutionMode2 || mode == MeasurementMode.OneTimeHighResolutionMode2)            {                sensorResolution = 2;            }        }        ///         /// Constructor        ///         /// Enumeration type of AddressSetting        /// Enumeration type of MeasurementMode        /// Enumeration type of LightTransmittance        public BH1750FVI(AddressSetting address, MeasurementMode mode, LightTransmittance transmittance)        {            sensorAddress = (byte)address;            sensorMode = (byte)mode;            if (mode == MeasurementMode.ContinuouslyHighResolutionMode2 || mode == MeasurementMode.OneTimeHighResolutionMode2)            {                sensorResolution = 2;            }            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 = 2;                    }                    break;            }        }        ///         /// Initialize BH1750FVI        ///         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 });        }        ///         /// Read data from BH1750FVI        ///         /// 
A double type contains data
public double Read() { byte[] readBuf = new byte[2]; sensor.WriteRead(new byte[] { sensorMode }, readBuf); byte temp = readBuf[0]; readBuf[0] = readBuf[1]; readBuf[1] = temp; double result = BitConverter.ToUInt16(readBuf, 0) / (1.2 * sensorResolution * sensorTransmittance); return result; } /// /// Cleanup /// public void Dispose() { sensor.Dispose(); } }}

  下面解释如何使用

  代码包含三个枚举类型,两个构造函数,三个方法。

  第一步调用构造函数将 BH1750FVI 实例化。

  第二步调用 InitializeAsync() 初始化 I2C 设备

  第三步调用 Read() 读取数据,返回的是一个 double 类型的值

  当需要关闭设备时,调用 Dispose() 

转载地址:http://tyjax.baihongyu.com/

你可能感兴趣的文章
使用dotenv管理环境变量
查看>>
温故js系列(11)-BOM
查看>>
Vuex学习
查看>>
bootstrap - navbar
查看>>
切图崽的自我修养-[ES6] 编程风格规范
查看>>
服务器迁移小记
查看>>
FastDFS存储服务器部署
查看>>
Android — 创建和修改 Fragment 的方法及相关注意事项
查看>>
swift基础之_swift调用OC/OC调用swift
查看>>
Devexpress 15.1.8 Breaking Changes
查看>>
Java B2B2C多用户商城 springcloud架构- common-service 项目构建过程(七)
查看>>
杨老师课堂之ArrayList集合常用方法解析
查看>>
ElasticSearch Client详解
查看>>
新零售讲堂之时代下的传统零售业,何去何从?
查看>>
c++读取和写入TXT文件的整理
查看>>
linux安全问答(1)
查看>>
mybatis update返回值的意义
查看>>
expdp 详解及实例
查看>>
解读最具O2O属性—哈根达斯微信企业号的成功之道
查看>>
Extjs4.x (MVC)Controller中refs以及Ext.ComponentQuery解析
查看>>