极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

12
返回列表 发新帖
楼主: hunduncn

arduino操作光照传感器BH1750(数字型,I2C接口)

[复制链接]
发表于 2013-5-7 11:06:27 | 显示全部楼层
#include <Wire.h>
#include <math.h>

int BH1750address = 0x23;//BH1750 I2C地址
byte buff[2];

void setup()
{
  Wire.begin();
  Serial.begin(9600);
}

void loop()
{
    Serial.print( BH1750() );
    Serial.println("[lx]");
}

double BH1750() //BH1750设备操作
{
  int i=0;
  double  val=0;
  //开始I2C读写操作
  Wire.beginTransmission(BH1750address);
  Wire.write(0x10);//1lx reolution 120ms//发送命令
  Wire.endTransmission();  

  delay(200);
  //读取数据
  Wire.beginTransmission(BH1750address);
  Wire.requestFrom(BH1750address, 2);
  while(Wire.available()) //
  {
    buff[i] = Wire.read();  // receive one byte
    i++;
  }
  Wire.endTransmission();
  if(2==i)
  {
   val=((buff[0]<<8)|buff[1])/1.2;
  }
  return val;
}
回复 支持 反对

使用道具 举报

发表于 2013-5-7 11:07:01 | 显示全部楼层
修改后正确的
回复 支持 反对

使用道具 举报

发表于 2013-10-15 11:34:50 | 显示全部楼层
请问一个arduino只能通过I2C总线接一个传感器吗?
回复 支持 反对

使用道具 举报

发表于 2013-10-15 11:59:34 | 显示全部楼层
为什么不直接把loop里的代码放进timer函数里?
回复 支持 反对

使用道具 举报

发表于 2013-12-20 19:35:05 | 显示全部楼层
这个传感器读数敏感不?
回复 支持 反对

使用道具 举报

发表于 2014-4-13 14:29:51 | 显示全部楼层
hunduncn 发表于 2012-8-20 18:01
我接5V正常的。

BH1750 dataSheet 的限值是4.5,5.5的噪波太大
回复 支持 反对

使用道具 举报

发表于 2014-5-3 15:30:51 | 显示全部楼层
請問如何多個BH1750要加接
地址要如何編碼
int BH1750address = 0x23;//BH1750 I2C地址
回复 支持 反对

使用道具 举报

发表于 2014-7-13 08:45:18 | 显示全部楼层
ADDR引脚也是接到Arduino的GND口?
回复 支持 反对

使用道具 举报

发表于 2014-9-13 11:44:57 | 显示全部楼层
问下math.h在哪里能下载,谢谢!
回复 支持 反对

使用道具 举报

发表于 2015-2-11 21:00:21 | 显示全部楼层
可否請假一下math.h  MsTimer2.h 哪裡可以下載 thank
回复 支持 反对

使用道具 举报

发表于 2015-5-19 21:06:11 | 显示全部楼层
有没有光照度传感器的库
回复 支持 反对

使用道具 举报

发表于 2015-6-30 18:25:09 | 显示全部楼层
麻烦帮忙看一下为什么BH1750和DS3231一起用时显示屏就白屏?

#include <UTFT.h>
#include <dht11.h>
#include "SR04.h"
#include <DS3231.h>
#include <Wire.h>
#include <math.h>
int BH1750address = 0x23;
int DS3231address = 0x68;
byte buff[2];

extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];

UTFT myGLCD(QD220A,A2,A1,A5,A4,A3);   // Remember to change the model parameter to suit your display module!
float cm;


dht11 DHT11;
int ledpin=(5,6);
#define DHT11PIN 2
const int TrigPin = 3;
const int EchoPin = 4;
int potPin=A6;

DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;

byte year, month, date, DoW, hour, minute, second;


double Fahrenheit(double celsius)
{
        return 1.8 * celsius + 32;
}    //摄氏温度度转化为华氏温度

double Kelvin(double celsius)
{
        return celsius + 273.15;
}     //摄氏温度转化为开氏温度

// 露点(点在此温度时,空气饱和并产生露珠)
// 参考: http://wahiduddin.net/calc/density_algorithms.htm
double dewPoint(double celsius, double humidity)
{
        double A0= 373.15/(273.15 + celsius);
        double SUM = -7.90298 * (A0-1);
        SUM += 5.02808 * log10(A0);
        SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
        SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
        SUM += log10(1013.246);
        double VP = pow(10, SUM-3) * humidity;
        double T = log(VP/0.61078);   // temp var
        return (241.88 * T) / (17.558-T);
}

// 快速计算露点,速度是5倍dewPoint()
// 参考: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
        double a = 17.271;
        double b = 237.7;
        double temp = (a * celsius) / (b + celsius) + log(humidity/100);
        double Td = (b * temp) / (a - temp);
        return Td;
}
  double BH1750()
{
  int i=0;
  double  val=0;
  Wire.beginTransmission(BH1750address);
  Wire.write(0x10);//1lx reolution 120ms
  Wire.endTransmission();  
  delay(200);
  Wire.beginTransmission(BH1750address);
  Wire.requestFrom(BH1750address, 2);
  while(Wire.available()) //
  {
    buff = Wire.read();  // receive one byte
    i++;
  }
  Wire.endTransmission();
  if(2==i)
  {
    val=((buff[0]<<8)|buff[1])/1.2;
  }
  return val;
}
void setup()
{
  randomSeed(analogRead(0));
  myGLCD.InitLCD(LANDSCAPE);
  myGLCD.clrScr();
  pinMode(ledpin,OUTPUT);
  pinMode(EchoPin,INPUT);
  pinMode(TrigPin,OUTPUT);
  Wire.begin();
        
}
void ReadDS3231()
{
  int second,minute,hour,date,month,year,temperature;
  second=Clock.getSecond();
  minute=Clock.getMinute();
  hour=Clock.getHour(h12, PM);
  date=Clock.getDate();
  month=Clock.getMonth(Century);
  year=Clock.getYear();
}

void loop()
{
  int buf[218];
  int x, x2;
  int y, y2;
  int r;
  int i;
  uint16_t val=0;
  ReadDS3231();
  int chk = DHT11.read(DHT11PIN);
  digitalWrite(TrigPin,LOW);
  delayMicroseconds(2);
  digitalWrite(TrigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(TrigPin,LOW);
  cm=pulseIn(EchoPin,HIGH)/58.0;
  cm=(int(cm*100.0))/100.0;
  int n=analogRead(potPin);
  float vol=n*0.1399265895812715;
  
   myGLCD.setBackColor(0, 0, 0);
   myGLCD.setFont(BigFont);
   myGLCD.setColor(0, 255, 0);
   myGLCD.print("20", 2, 6);  
   myGLCD.printNumI(Clock.getYear(), 35, 6);
   myGLCD.print("/", 65, 6);
   myGLCD.setColor(150, 150, 0);  
   myGLCD.printNumI(Clock.getMonth(Century), 95, 6);
   myGLCD.print("/", 115, 6);
   myGLCD.setColor(200, 200, 200);
   myGLCD.printNumI(Clock.getDate(), 135, 6);
   
   
  
   myGLCD.setColor(VGA_FUCHSIA);
   myGLCD.printNumI(Clock.getHour(h12, PM), 30, 33);
   myGLCD.print(":", 63, 33);
   myGLCD.setColor(VGA_TEAL);
   myGLCD.printNumI(Clock.getMinute(), 75, 33);
   myGLCD.print(":", 105, 33);
   
   myGLCD.setFont(SevenSegNumFont);
   myGLCD.setColor(VGA_YELLOW);
   myGLCD.printNumI(Clock.getDoW(), 184, 2);

   myGLCD.setFont(SmallFont);
   myGLCD.setColor(150, 10, 0);
   myGLCD.printNumI(Clock.getSecond(), 121, 37);
   
     myGLCD.setColor(50, 200, 50);
     myGLCD.print("Atm", 28, 62);
     
     myGLCD.setColor(200, 50, 200);
     myGLCD.print("High", 26, 82);
   
     myGLCD.setColor(200, 100, 20);
     myGLCD.print("Illuminance", 2, 102);  
     myGLCD.printNumI(val, 103, 102);
     myGLCD.setColor(200, 200, 200);
     myGLCD.print("Lx", 118, 102);
   
   
   myGLCD.setColor(255, 255, 0);
   myGLCD.print("Distance", 12, 122);   
   myGLCD.setColor(255, 0, 0);
   myGLCD.print("cm", 134, 122);
   myGLCD.printNumF(cm, 1, 95, 122);
   
  myGLCD.setColor(0, 255, 0);
  myGLCD.print("Humidity", 12, 142);
  myGLCD.printNumI(DHT11.humidity, 103, 142);
  myGLCD.setColor(0, 0, 255);
  myGLCD.print("%", 119, 142);
  if ( DHT11.humidity<=40)
  {
    myGLCD.setColor(255, 0, 0);
    myGLCD.print("Dry", 172, 142);
  }
  else if( DHT11.humidity<=60)
  {
    myGLCD.setColor(200, 200, 0);
    myGLCD.print("Comefort", 149, 142);
  }
  else
  {
    myGLCD.setColor(0, 0, 200);  
    myGLCD.print("Moist", 163, 142);
  }
  
  myGLCD.setColor(255, 0, 255);  
  myGLCD.print("Temperature", 2, 162);
  myGLCD.setColor(200, 255, 50);
  myGLCD.print("oC", 131, 162);
  myGLCD.printNumF(vol, 1, 99, 162);
  if ( vol<=25)
  {
    digitalWrite(5,HIGH);
    digitalWrite(6,LOW);
    myGLCD.setColor(0, 0, 255);
    myGLCD.print("Cold", 172, 162);
  }
  else if( vol<=30)
  {
    myGLCD.setColor(200, 200, 0);
    myGLCD.print("Comfort", 153, 162);
  }
  else
  {
    digitalWrite(5,LOW);
    digitalWrite(6,HIGH);
    myGLCD.setColor(255, 0, 0);  
    myGLCD.print("Hot", 172, 162);
  }

  myGLCD.setColor(0, 0, 255);
  myGLCD.drawRect(0, 0, 218, 174); //外框
  myGLCD.drawLine(90, 52, 90, 172); //传感器名称右侧竖线
  myGLCD.drawLine(151, 52, 151, 172); //传感器显示数值右侧竖线
  
  myGLCD.drawLine(180, 2, 180, 52); //星期左侧竖线
  myGLCD.drawLine(180, 52, 219, 52); //星期下面横线
  myGLCD.drawLine(1, 28, 181, 28); //日期下面横线
  myGLCD.drawLine(1, 52, 181, 52); //时间下面横线
  
  myGLCD.drawLine(1, 75, 219, 75); //光照度上面横线
  myGLCD.drawLine(1, 95, 219, 95); //光照度上面横线
  myGLCD.drawLine(1, 118, 219, 118); //距离上面横线
  myGLCD.drawLine(1, 138, 219, 138); //湿度上面横线
  myGLCD.drawLine(1, 158, 219, 158); //温度上面横线
  
  
  delay (500);
  
}
回复 支持 反对

使用道具 举报

发表于 2015-6-30 22:17:58 | 显示全部楼层
还没有用过iic,难不难?
回复 支持 反对

使用道具 举报

发表于 2019-3-30 11:51:26 | 显示全部楼层

我可以问一下你这个怎么连的引脚吗?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 00:22 , Processed in 0.043362 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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