ESP32基于Arduino驱动GY30光照传感器程序

连线 vcc-3.3v-5v gnd-gnd  scl-22 sda-21

 

 效果

 

#include <Wire.h> //IIC

#include <math.h> 

int BH1750address = 0x23; 

byte buff[2];

void setup()

{

  Wire.begin();

  Serial.begin(9600);

}




void loop()

{

 int i;

 uint16_t val=0;

 BH1750_Init(BH1750address);

 delay(200);

 if(2==BH1750_Read(BH1750address))

  {

   val=((buff[0]<<8)|buff[1])/1.2;

   Serial.print(val,DEC);     

   Serial.println("[lx]"); 

  }

 delay(150);

}




int BH1750_Read(int address) //

{

  int i=0;

  Wire.beginTransmission(address);

  Wire.requestFrom(address, 2);

  while(Wire.available()) //

  {

    buff[i] = Wire.read();  // receive one byte

    i++;

  }

  Wire.endTransmission();  

  return i;

}




void BH1750_Init(int address) 

{

  Wire.beginTransmission(address);

  Wire.write(0x10);//1lx reolution 120ms

  Wire.endTransmission();

}
posted @   昊月光华  阅读(65)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示