当前位置: 代码迷 >> 综合 >> 【LuatOS-sensor】1 光感BH1750
  详细解决方案

【LuatOS-sensor】1 光感BH1750

热度:77   发布时间:2023-12-05 20:27:09.0

1 前言

整理几个传感器的代码。
BMP气压:https://doc.openluat.com/wiki/21?wiki_page_id=2729
罗盘仪:https://doc.openluat.com/wiki/21?wiki_page_id=2750
光强:https://doc.openluat.com/wiki/21?wiki_page_id=2668
色彩:https://doc.openluat.com/wiki/21?wiki_page_id=2746

本章是光照强度传感器BH1750。

2 硬件连接

在这里插入图片描述
在这里插入图片描述
esp32只支持一个i2c,id为0
查看源码:https://gitee.com/dreamcmi/LuatOS-ESP32/blob/master/components/luat/port/luat_i2c_esp32.c

3 官方实例代码

PROJECT = "sensor"
VERSION = "1.0.0"require "log"
require "sys"
require "misc"-- i2c ID
i2cid = 2-- i2c 速率
speed = 100000-- 初始化
function init(address)if i2c.setup(i2cid, speed, -1, 1) ~= speed thenlog.error("i2c", "setup fail", addr)returnendaddr = address
end-- 读取数据
function send(...)sys.wait(10)if not addr then log.info("i2c", "addr err")return endlocal t = {
    ...}if i2c.send(i2cid, addr, t) ~= #t thenlog.error("i2c", "send fail", #t)returnendreturn true
end-- 发送数据
function read(n)sys.wait(10)if not addr then log.info("i2c", "addr err")return "\x00" endval = i2c.recv(i2cid, addr, n)if val and #val>0 thenreturn valendreturn "\x00"
end-- 颜色识别传感器
function TCS34725()init(0x29) send(0x83, 0xff)send(0x81, 0x00)send(0x8f, 0x00)send(0x80, 0x03)sys.wait(800)while true dosys.wait(1000)send(0x94)_, c, red, green, blue = pack.unpack(read(8), "<HHHH")if red and green and blue thenlux = (-0.32466 * red) + (1.57837 * green) + (-0.73191 * blue)log.info("red", red)log.info("green", green)log.info("blue", blue)log.info("c, lux", c, lux)elselog.info("TCS34725", "err")endend
endsys.taskInit(function()sys.wait(3000)TCS34725()
end)sys.init(0, 0)
sys.run()

4 改动代码

sen_BH1750.lua

--- 模块功能:BH1750
-- @module BH1750
-- @author Dozingfiretruck ,youkai
-- @license MIT
-- @copyright OpenLuat.com
-- @release 2021.03.14-- 源码https://doc.openluat.com/wiki/21?wiki_page_id=2668BH1750_i2c_id = 0 --BH1750_i2c_id esp32 i2c 0BH1750_ADDRESS_AD0_LOW     =   0x23 -- address pin low (GND), default for InvenSense evaluation board
BH1750_ADDRESS_AD0_HIGH    =   0x5c -- address pin high (VCC)BH1750_i2c_slaveaddr = BH1750_ADDRESS_AD0_LOW     -- ADDRES-- BH1750 registers define
BH1750_POWER_DOWN   	    =   0x00	-- power down
BH1750_POWER_ON			=   0x01	-- power on
BH1750_RESET			    =   0x07	-- reset
BH1750_CON_H_RES_MODE	    =   0x10	-- Continuously H-Resolution Mode
BH1750_CON_H_RES_MODE2	=   0x11	-- Continuously H-Resolution Mode2
BH1750_CON_L_RES_MODE	    =   0x13	-- Continuously L-Resolution Mode
BH1750_ONE_H_RES_MODE	    =   0x20	-- One Time H-Resolution Mode
BH1750_ONE_H_RES_MODE2	=   0x21	-- One Time H-Resolution Mode2
BH1750_ONE_L_RES_MODE	    =   0x23	-- One Time L-Resolution Modelocal function BH1750_i2c_send(data)i2c.send(BH1750_i2c_id, BH1750_i2c_slaveaddr, data)
end
local function BH1750_i2c_recv(num)revData = i2c.recv(BH1750_i2c_id, BH1750_i2c_slaveaddr, num)return revData
endlocal function BH1750_power_on()BH1750_i2c_send(BH1750_POWER_ON)
endlocal function BH1750_power_down()BH1750_i2c_send(BH1750_POWER_DOWN)
endlocal function BH1750_set_measure_mode(mode,time)BH1750_i2c_send(BH1750_RESET)BH1750_i2c_send(mode)sys.wait(time)
endlocal function BH1750_read_light()BH1750_set_measure_mode(BH1750_CON_H_RES_MODE2, 180)_,light = pack.unpack(BH1750_i2c_recv(2),">h")light = light / 1.2return light;
endfunction BH1750_init(t_spi_id)BH1750_i2c_id = t_spi_idlog.info("BH1750_i2c_id",BH1750_i2c_id)if i2c.setup(t_spi_id,i2c.SLOW) ~= i2c.SLOW thenlog.error("I2c.init","fail")returnelseprint("BH1750_init ok.")endBH1750_power_on()
endfunction BH1750_get()log.info("BH1750_read_light", BH1750_read_light()*10)sys.wait(100)
end

main.lua

PROJECT = "sensor"
VERSION = "1.0.0"_G.sys = require("sys")require("sen_BH1750")       -- 光感-- 加载I?C功能测试模块
T1_BH1750 = 1-- i2c ID
esp32_i2c_id = 0        -- esp32只支持一路i2c, id为0sys.taskInit(function()if T1_BH1750 == 1 thenBH1750_init(esp32_i2c_id)endwhile 1 doif T1_BH1750 == 1 thenBH1750_get()endsys.wait(100)end
end)sys.run()

结果

在这里插入图片描述

  相关解决方案