张高兴的 Windows 10 IoT 开发笔记:BH1750FVI 光照度传感器

简介:   BH1750FVI 是一款 IIC 接口的数字型光强度传感器集成电路。下面介绍一下其在 Windows 10 IoT Core 环境下的用法。   项目运行在 Raspberry Pi 2/3 上,使用 C# 进行编码。

  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 = 1;
        private double sensorTransmittance = 1;

        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 = 2;
            }
        }

        /// <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 = 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;
            }
        }

        /// <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[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;
        }

        /// <summary>
        /// Cleanup
        /// </summary>
        public void Dispose()
        {
            sensor.Dispose();
        }
    }
}

  下面解释如何使用

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

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

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

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

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

目录
相关文章
|
7月前
|
Web App开发 人工智能 JSON
Windows版来啦!Qwen3+MCPs,用AI自动发布小红书图文/视频笔记!
上一篇用 Qwen3+MCPs实现AI自动发小红书的最佳实践 有超多小伙伴关注,同时也排队在蹲Windows版本的教程。
1117 1
|
监控 Ubuntu Linux
视频监控笔记(五):Ubuntu和windows时区同步问题-your clock is behind
这篇文章介绍了如何在Ubuntu和Windows系统中通过设置相同的时区并使用ntp服务来解决时间同步问题。
280 4
视频监控笔记(五):Ubuntu和windows时区同步问题-your clock is behind
|
传感器 存储 物联网
在物联网(IoT)快速发展的今天,C语言作为物联网开发中的关键工具,以其高效、灵活、可移植的特点
在物联网(IoT)快速发展的今天,C语言作为物联网开发中的关键工具,以其高效、灵活、可移植的特点,广泛应用于嵌入式系统开发、通信协议实现及后端服务构建等领域,成为推动物联网技术进步的重要力量。
453 1
|
Rust 资源调度 安全
为什么使用 Rust over C++ 进行 IoT 解决方案开发
为什么使用 Rust over C++ 进行 IoT 解决方案开发
347 7
|
存储 分布式计算 物联网
Apache IoTDB进行IoT相关开发实践
当今社会,物联网技术的发展带来了许多繁琐的挑战,尤其是在数据库管理系统领域,比如实时整合海量数据、处理流中的事件以及处理数据的安全性。例如,应用于智能城市的基于物联网的交通传感器可以实时生成大量的交通数据。据估计,未来5年,物联网设备的数量将达数万亿。物联网产生大量的数据,包括流数据、时间序列数据、RFID数据、传感数据等。要有效地管理这些数据,就需要使用数据库。数据库在充分处理物联网数据方面扮演着非常重要的角色。因此,适当的数据库与适当的平台同等重要。由于物联网在世界上不同的环境中运行,选择合适的数据库变得非常重要。 原创文字,IoTDB 社区可进行使用与传播 一、什么是IoTDB 我
516 9
Apache IoTDB进行IoT相关开发实践
|
Linux Apache C++
FFmpeg开发笔记(三十五)Windows环境给FFmpeg集成libsrt
该文介绍了如何在Windows环境下为FFmpeg集成SRT协议支持库libsrt。首先,需要安装Perl和Nasm,然后编译OpenSSL。接着,下载libsrt源码并使用CMake配置,生成VS工程并编译生成srt.dll和srt.lib。最后,将编译出的库文件和头文件按照特定目录结构放置,并更新环境变量,重新配置启用libsrt的FFmpeg并进行编译安装。该过程有助于优化直播推流的性能,减少卡顿问题。
441 2
FFmpeg开发笔记(三十五)Windows环境给FFmpeg集成libsrt
|
存储 分布式计算 物联网
Apache IoTDB进行IoT相关开发实践
IoTDB是专为物联网(IoT)设计的开源时间序列数据库,提供数据收集、存储、管理和分析。它支持高效的数据写入、查询,适用于处理大规模物联网数据,包括流数据、时间序列等。IoTDB采用轻量级架构,可与Hadoop和Spark集成,支持多种存储策略,确保数据安全和高可用性。此外,它还具有InfluxDB协议适配器,允许无缝迁移和兼容InfluxDB的API和查询语法,简化物联网项目的数据管理。随着物联网设备数量的快速增长,选择适合的数据库如IoTDB对于数据管理和分析至关重要。
649 12
|
存储 安全 数据安全/隐私保护
Windows 32 汇编笔记(一):基础知识
Windows 32 汇编笔记(一):基础知识
|
存储 分布式计算 物联网
Apache IoTDB进行IoT相关开发实践
The article introduces IoTDB, an open-source time-series database designed for efficient management of IoT-generated data. It addresses challenges like real-time integration of massive datasets and security. IoTDB supports high-performance storage,
430 0
Apache IoTDB进行IoT相关开发实践
|
存储 分布式计算 物联网
Apache IoTDB进行IoT相关开发实践
IoTDB是面向物联网的时序数据库,专注于时间序列数据管理,提供高效的数据处理、集成Hadoop和Spark生态、支持多目录存储策略。它还具有InfluxDB协议适配器,允许无缝迁移原本使用InfluxDB的业务。文章讨论了IoTDB的体系结构,包括数据文件、系统文件和预写日志文件的存储策略,并介绍了如何配置数据存储目录。此外,还提及了InfluxDB版本和查询语法的支持情况。IoTDB在物联网数据管理和分析中扮演关键角色,尤其适合处理大规模实时数据。
333 5