首页 > 其他分享 >ESP32-WROOM-32 & BH1750-获取环境亮度并调节屏幕亮度

ESP32-WROOM-32 & BH1750-获取环境亮度并调节屏幕亮度

时间:2023-03-29 19:33:57浏览次数:62  
标签:begin BH1750 ser 32 亮度 lightMeter Serial include

一、电路连接

二、烧录测试程序

#include <Wire.h>
#include <BH1750.h>

BH1750 lightMeter;

void setup(){

  Serial.begin(9600);

  // Initialize the I2C bus (BH1750 library doesn't do this automatically)
  // On esp8266 devices you can select SCL and SDA pins using Wire.begin(D4, D3);
  Wire.begin();

  lightMeter.begin();
  Serial.println(F("BH1750 Test"));

}

void loop() {

  float lux = lightMeter.readLightLevel();
  Serial.print("Light: ");
  Serial.print(lux);
  Serial.println(" lx");
  delay(1000);

}

对于 ESP32,根据您使用的是什么板,SDA 和 SCL 默认引脚可能不同。如果那是 ESP32 WROOM 开发套件,那么默认情况下,您的 SDA 引脚为 21,SCL 引脚为 22。或者您可以简单地使用:
Wire.setPins(, );
并将您使用的任何引脚号替换为 SDA 和 SCL。您只需在 Wire.begin() 命令之前执行此操作。

三、通过蓝牙串口将环境亮度数据传给pc

#include <Wire.h>
#include <BH1750.h>
#include <Arduino.h>
#include "BluetoothSerial.h"
 
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BH1750 lightMeter;
BluetoothSerial SerialBT;

void setup(){
  pinMode(LED_BUILTIN, OUTPUT);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  
  Serial.begin(115200);

  // Initialize the I2C bus (BH1750 library doesn't do this automatically)
  // On esp8266 devices you can select SCL and SDA pins using Wire.begin(D4, D3);
  Wire.begin();

  lightMeter.begin();
  Serial.println(F("BH1750 Test"));

}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  float lux = lightMeter.readLightLevel();
  while (SerialBT.available() > 0){
    delay(10);
  }
  SerialBT.println(lux);
  Serial.print("Light: ");
  Serial.print(lux);
  Serial.println(" lx");
  digitalWrite(LED_BUILTIN, LOW);
  delay(100);

}

四、pc端python接收

# coding=gbk
import serial
import time

while True:
    ser = serial.Serial('com12', 115200, parity='E', stopbits=1, bytesize=8, timeout=0.5)
    print('光照强度:' + ser.readline().decode('gbk'))
    time.sleep(1)
    ser.close()

五、python修改屏幕亮度(台式机外接屏幕测试,vga不支持)

# coding=gbk
import serial
import time
import screen_brightness_control as sbc

while True:
    ser = serial.Serial('com12', 115200, parity='E', stopbits=1, bytesize=8, timeout=0.5)
    print('光照强度:' + ser.readline().decode('gbk'))
    sbc.set_brightness(float(ser.readline().decode('gbk'))/5) #亮度调节,这颗传感器的分辨率范围为(0-54612.5)
    time.sleep(1)
    ser.close()

库链接

https://github.com/claws/BH1750

标签:begin,BH1750,ser,32,亮度,lightMeter,Serial,include
From: https://www.cnblogs.com/ff888/p/17270076.html

相关文章

  • STM32F4 按键控制小灯亮灭例程
    我的公众号目前已搁置(临近注销),所以我将以前所写的文章转移到博客园。此篇公众号文章创建于2020-12-3018:05,内容后期无修改。通过一个外接按键,来控制LED灯的亮灭。......
  • 不坑盒子 V2023.0325 && 打工人插件 大众实用型办公高效利器。
    不坑盒子这是一个非常好用的插件工具,专门应用在Word文档和wps,支持Office2010以上的版本,操作也简单且实用。不坑盒子下载及使用说明 一键排版功能像是下面的自动排版......
  • 总结20230328
    今天周二,上了实用英语阅读与翻译、数据库原理、python程序设计。实用英语阅读与翻译讲的是词类转换。数据库原理讲的是第五章和第六章的一部分,具体第五章讲的是视图,第六......
  • 20230328-Epic Game更改修改更换安装目录
    最简单的办法就是卸载后重装,毕竟现在的网速都是很快的,SSD也是很快的。然而,如果是机器硬盘,如果游戏也很大,那么可以采用把旧的游戏目录先移走,再在新目录安装,中途退出,然后用......
  • ASEMI代理NXP汽车芯片MKE02Z32VLC4
    编辑-ZNXP汽车芯片MKE02Z32VLC4参数:型号:MKE02Z32VLC4资格状态:M=完全合格的一般市场流量,P=资格预审动脉炎家族:KE02型关键点属性:Z=M0+core程序闪存大小:16=16KB,32......
  • 32、K8S-配置管理之Configmap
    1、基础知识1.1、场景需求生产中所有的应用程序中,都涉及到配置文件,而配置文件经常会有变更,比如数据库连接、代码版本号等.最典型场景就是:项目经历开发环境、测试环境......
  • 产品原型4-20230327
                ......
  • 总结20230327
    今天上的是工程数学和软件工程。工程数学课上学的是点的可行方向简称FD等等知识点,我感觉这节课听得很认真,听课效率也极高,能高达百分之八十五。下午的软件工程这节课定下......
  • pywin32获取窗口截图
    importwin32guiimportwin32uiimportwin32conimportwin32api#获取窗口句柄hwnd=win32gui.FindWindow(None,'窗口标题')#获取窗口位置和大小left,top,......
  • stm32学习笔记---i2c学习
    stm32学习笔记---i2c学习1、半双工,不能同时发送数据,一个设备发送另一个设备接受2、接受到数据有有应答3、能够挂在多个模块,且通信之间不受干扰,支持一主多从,多住多从4......