找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

【Arduino】108种传感器模块系列实验(129)---BH1750光照传感器

查看数: 6649 | 评论数: 17 | 收藏 0
关灯 | 提示:支持键盘翻页<-左 右->
    组图打开中,请稍候......
发布时间: 2019-9-22 15:20

正文摘要:

37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的。鉴于本人手头积累了一些传感器和模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动 ...

回复

ID:513258 发表于 2019-9-27 19:51
  1. /*
  2. 【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
  3. 实验一百二十九:GY-302 数字量光强度检测 光照传感器 BH1750FVI 光线检测模块
  4. 项目:依据光亮度控制LED
  5. Module        UNO
  6. 5 VCC   ——   5V
  7. 6 GND  ——   GND
  8. 7 SCL    ——   A5
  9. 8 SDA   ——   A4
  10. 9 ADD  ——   NC
  11. */

  12. #include <Wire.h> //IIC库

  13. #include <math.h>

  14. int BH1750address = 0x23;//芯片地址为16位23

  15. byte buff[2];

  16. void setup()

  17. {
  18.   pinMode(13,OUTPUT);
  19.   Wire.begin();

  20.   Serial.begin(9600);

  21. }




  22. void loop()

  23. {

  24. int i;

  25. uint16_t val=0;

  26. BH1750_Init(BH1750address);

  27. delay(100);

  28. if(2==BH1750_Read(BH1750address))

  29.   {

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

  31.    Serial.print(val,DEC);     

  32.    Serial.println("[lx]");

  33.   }

  34. delay(150);
  35.    if (val<100)
  36.   {
  37.     digitalWrite(13,HIGH);
  38.   }
  39.   else
  40.   {
  41.     digitalWrite(13,LOW);
  42.   }
  43. }




  44. int BH1750_Read(int address) //

  45. {

  46.   int i=0;

  47.   Wire.beginTransmission(address);

  48.   Wire.requestFrom(address, 2);

  49.   while(Wire.available()) //

  50.   {

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

  52.     i++;

  53.   }

  54.   Wire.endTransmission();  

  55.   return i;

  56. }




  57. void BH1750_Init(int address)

  58. {

  59.   Wire.beginTransmission(address);

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

  61.   Wire.endTransmission();

  62. }
复制代码


ID:513258 发表于 2019-9-27 19:18
  1. /*
  2. 【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
  3. 实验一百二十九:GY-302 数字量光强度检测 光照传感器 BH1750FVI 光线检测模块
  4. 项目:测试环境光亮度
  5. Module        UNO
  6. 5 VCC   ——   5V
  7. 6 GND  ——   GND
  8. 7 SCL    ——   A5
  9. 8 SDA   ——   A4
  10. 9 ADD  ——   NC
  11. */

  12. #include <Wire.h>

  13. #define ADDRESS_BH1750FVI 0x23    //ADDR="L" for this module
  14. #define ONE_TIME_H_RESOLUTION_MODE 0x20
  15. //One Time H-Resolution Mode:
  16. //Resolution = 1 lux
  17. //Measurement time (max.) = 180ms
  18. //Power down after each measurement

  19. byte highByte = 0;
  20. byte lowByte = 0;
  21. unsigned int sensorOut = 0;
  22. unsigned int illuminance = 0;

  23. void setup()
  24. {
  25.      Wire.begin();
  26.      Serial.begin(115200);
  27. }

  28. void loop()
  29. {
  30.      Wire.beginTransmission(ADDRESS_BH1750FVI); //"notify" the matching device
  31.      Wire.write(ONE_TIME_H_RESOLUTION_MODE);     //set operation mode
  32.      Wire.endTransmission();
  33.          
  34.      delay(180);

  35.      Wire.requestFrom(ADDRESS_BH1750FVI, 2); //ask Arduino to read back 2 bytes from the sensor
  36.      highByte = Wire.read();  // get the high byte
  37.      lowByte = Wire.read(); // get the low byte
  38.      
  39.      sensorOut = (highByte<<8)|lowByte;
  40.      illuminance = sensorOut/1.2;
  41.      Serial.print(illuminance);    Serial.println(" lux");

  42.      delay(1000);
  43. }
复制代码


小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表