当前位置:网站首页>[luatos sensor] 1 light sensing bh1750
[luatos sensor] 1 light sensing bh1750
2022-07-03 04:37:00 【Birds hate fish】
1 Preface
Sort out the codes of several sensors .
BMP pressure :https://doc.openluat.com/wiki/21?wiki_page_id=2729
Compass :https://doc.openluat.com/wiki/21?wiki_page_id=2750
The light intensity :https://doc.openluat.com/wiki/21?wiki_page_id=2668
color :https://doc.openluat.com/wiki/21?wiki_page_id=2746
This chapter is about the light intensity sensor BH1750.
2 Hardware connection


esp32 Only one... Is supported i2c,id by 0
View source code :https://gitee.com/dreamcmi/LuatOS-ESP32/blob/master/components/luat/port/luat_i2c_esp32.c
3 Official instance code
PROJECT = "sensor"
VERSION = "1.0.0"
require "log"
require "sys"
require "misc"
-- i2c ID
i2cid = 2
-- i2c rate
speed = 100000
-- initialization
function init(address)
if i2c.setup(i2cid, speed, -1, 1) ~= speed then
log.error("i2c", "setup fail", addr)
return
end
addr = address
end
-- Reading data
function send(...)
sys.wait(10)
if not addr then
log.info("i2c", "addr err")
return
end
local t = {
...}
if i2c.send(i2cid, addr, t) ~= #t then
log.error("i2c", "send fail", #t)
return
end
return true
end
-- send data
function read(n)
sys.wait(10)
if not addr then
log.info("i2c", "addr err")
return "\x00"
end
val = i2c.recv(i2cid, addr, n)
if val and #val>0 then
return val
end
return "\x00"
end
-- Color recognition sensor
function TCS34725()
init(0x29)
send(0x83, 0xff)
send(0x81, 0x00)
send(0x8f, 0x00)
send(0x80, 0x03)
sys.wait(800)
while true do
sys.wait(1000)
send(0x94)
_, c, red, green, blue = pack.unpack(read(8), "<HHHH")
if red and green and blue then
lux = (-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)
else
log.info("TCS34725", "err")
end
end
end
sys.taskInit(function()
sys.wait(3000)
TCS34725()
end)
sys.init(0, 0)
sys.run()
4 Change code
sen_BH1750.lua
--- Module function :BH1750
-- @module BH1750
-- @author Dozingfiretruck ,youkai
-- @license MIT
-- @copyright OpenLuat.com
-- @release 2021.03.14
-- Source code https://doc.openluat.com/wiki/21?wiki_page_id=2668
BH1750_i2c_id = 0 --BH1750_i2c_id esp32 i2c 0
BH1750_ADDRESS_AD0_LOW = 0x23 -- address pin low (GND), default for InvenSense evaluation board
BH1750_ADDRESS_AD0_HIGH = 0x24 -- 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 Mode
local 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
end
local function BH1750_power_on()
BH1750_i2c_send(BH1750_POWER_ON)
end
local function BH1750_power_down()
BH1750_i2c_send(BH1750_POWER_DOWN)
end
local function BH1750_set_measure_mode(mode,time)
BH1750_i2c_send(BH1750_RESET)
BH1750_i2c_send(mode)
sys.wait(time)
end
local 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.2
return light;
end
function BH1750_init(t_spi_id)
BH1750_i2c_id = t_spi_id
log.info("BH1750_i2c_id",BH1750_i2c_id)
if i2c.setup(t_spi_id,i2c.SLOW) ~= i2c.SLOW then
log.error("I2c.init","fail")
return
else
print("BH1750_init ok.")
end
BH1750_power_on()
end
function 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") -- Light sense
-- load I²C Function test module
T1_BH1750 = 1
-- i2c ID
esp32_i2c_id = 0 -- esp32 Only one way i2c, id by 0
sys.taskInit(function()
if T1_BH1750 == 1 then
BH1750_init(esp32_i2c_id)
end
while 1 do
if T1_BH1750 == 1 then
BH1750_get()
end
sys.wait(100)
end
end)
sys.run()
result

边栏推荐
- I've been in software testing for 8 years and worked as a test leader for 3 years. I can also be a programmer if I'm not a professional
- IPhone x forgot the boot password
- Reptile exercise 02
- C language series - Section 3 - functions
- Summary of training competition (Lao Li's collection of questions)
- MC Layer Target
- 2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test
- Youdao cloud notes
- Small sample target detection network with attention RPN and multi relationship detector (provide source code, data and download)
- FuncS sh file not found when using the benchmarksql tool to test kingbases
猜你喜欢

The simple problem of leetcode: dismantling bombs

关于开学的准备与专业认知

Some information about the developer environment in Chengdu

Introduction to message queuing (MQ)

Symbol of array element product of leetcode simple problem

Basic use of continuous integration server Jenkins

Prefix and (continuously updated)

Internationalization and localization, dark mode and dark mode in compose

Introduction to JVM principle

Function introduction of member points mall system
随机推荐
stm32逆向入门
Auman Galaxy new year of the tiger appreciation meeting was held in Beijing - won the double certification of "intelligent safety" and "efficient performance" of China Automotive Research Institute
Kingbasees plug-in KDB of Jincang database_ date_ function
Kubernetes source code analysis (I)
AWS VPC
商城系统搭建完成后需要设置哪些功能
Redis persistence principle
Number of uniform strings of leetcode simple problem
Games101 Lesson 9 shading 3 Notes
FISCO bcos zero knowledge proof Fiat Shamir instance source code
Drf--- quick start 01
vulnhub HA: Natraj
【SQL注入】联合查询(最简单的注入方法)
The simple problem of leetcode: dismantling bombs
解决bp中文乱码
[fairseq] error: typeerror:_ broadcast_ coalesced(): incompatible function arguments
Integration of Android high-frequency interview questions (including reference answers)
After reviewing MySQL for a month, I was stunned when the interviewer of Alibaba asked me
When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
Dive Into Deep Learning——2.1数据操作&&练习