how to connect four BH1750 on Arduino

Hello, I’m looking to make a luxmeter with 4 sensors BH1750 and an Arduino Nano, I found the code and the diagram for a sensor and I liked if someone from you can help me to make it to 4 sensors ,

/*
Connection:
BH1750 -   Arduino
------------------
VCC    -   5v
GND    -   GND
SCL    -   SCL(Analog Pin 5)
SDA    -   SDA(Analog Pin 4)
ADD    -   NC (or GND)
*/
 #include <Wire.h>
#include<LiquidCrystal.h>

int BH1750address = 0x23;
byte buff[2];
LiquidCrystal lcd (7,6,5,4,3,2); //RS, E, D4, D5, D6, D7
void setup()
{
 Wire.begin();
 //Serial.begin(9600);
 lcd.begin(16,2);
 lcd.print("  BH1750 Light  ");
 lcd.setCursor(0,1);
 lcd.print("Intensity Sensor");
 delay(2000);
}
void loop()
{
 int i;
 uint16_t value=0;
 BH1750_Init(BH1750address);
 delay(200);
 if(2==BH1750_Read(BH1750address))
 {
   value=((buff[0]<<8)|buff[1])/1.2;
   lcd.clear();
   lcd.print("Intensity in LUX");
   lcd.setCursor(6,1);
   lcd.print(value);
      //Serial.print(val);    
   //Serial.println("[lux]");
 }
 delay(150);
}
int BH1750_Read(int address)
{
 int i=0;
 Wire.beginTransmission(address);
 Wire.requestFrom(address, 2);
 while(Wire.available())
 {
   buff[i] = Wire.read();
   i++;
 }
 Wire.endTransmission();  
 return i;
}
void BH1750_Init(int address)
{
 Wire.beginTransmission(address);
 Wire.write(0x10);
 Wire.endTransmission();
}

Maybe this page will help. Found with a search for "multiple BH1750 on Arduino".

Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.

thank you, for your answer, this is the first time I use an arduino,

Can you give a link to your BH1750 modules ? A link to where you bought it.

The BH1750 is a 3.3V sensor and the Arduino Nano is a 5V board. The means there is a voltage mismatch on the I2C bus, and you may not connect digital outputs from the Nano to the address line of the sensor.

Perhaps you better find a library for the BH1750, because you use the functions of the Wire library in the wrong way. See also my alternative explanation.

I found the diagram and the code on the site BH1750 Ambient Light Sensor with Arduino - Interfacing with Arduino UNO
I bought the BH1750 on the website Module Capteur d'Intensité de Lumière BH1750FVI - Arduiplanet Maroc

Did you check my alternative explanation and did you see how to use the functions of the Wire library ? That example from that website is wrong.

I think the schematic of that module is here: GY-302 BH1750 Digital Light Intensity Module - Wiki. It has the same wrong example sketch !

You may not connect a digital output of the Nano to the address line. That means that it is not possible to select the addres in the sketch. It might work when using a I2C multiplexer: TCA9548A I2C Multiplexer : ID 2717 : $6.95 : Adafruit Industries, Unique & fun DIY electronics and kits.
If you would upgrade to a 3.3V Arduino board, then there are more possibilities.

I don’t know anything about Arduino, I need to measure the light intensity in 4 rooms, looking on the internet I came across this model with Arduino, I bought the components that are on the schematic and I’m trying to find someone who can help me make it

azizouzzat:
I don’t know anything about Arduino, I need to measure the light intensity in 4 rooms, looking on the internet I came across this model with Arduino, I bought the components that are on the schematic and I’m trying to find someone who can help me make it

I can see from the replies that this project is flawed right from the starting gate, and would require a lot of work and changes to the design to do what you ask. So, people here don't think it is fun to help with that kind of project, it's often just an exercise in frustration. Also it seems like you won't be able to code it yourself, and generally people don't take on assignments like this for free, unless it is fun in some way. So, you might have to move the thread to "Gigs and Collaborations" and let people know how much you would pay to have it made for you.

@azizouzzat, aarg is right. Your project can not work. The I2C bus length is for 50 cm, not to another room. You can not connect those sensors to a Arduino Nano just like that. I even wrote a page to scare new users for the I2C bus. There are many bad examples online, and sadly you found a bad sketch.

Arduino is for learning about electronics and programming and having fun along the way.

Start with something that you can do and that will work. Adafruit has the best tutorials. For example a tutorial for ledstrips.

azizouzzat:
Hello, I’m looking to make a luxmeter with 4 sensors BH1750 and an Arduino Nano, I found the code and the diagram for a sensor and I liked if someone from you can help me to make it to 4 sensors

Do you like to try (as an exercise) the following proposed solution?

i2cmuxZ.png

i2cmuxZ.png

thank you for everything that we replied to my message, finally I just found what I'm looking for on the Blog

That is not bad, but also not good :confused:

A single SCL for everything is okay.
A multiplexer for SDA requires that SDA has pullup resistors on both sided of the multiplexer.
A 3.3V I2C bus connected to a 5V Arduino board might cause trouble.
You can not use long wires to put sensors in other rooms.
The sketch in that Blog is not okay. It uses the Wire library in the wrong way.

The I2C bus is not supposed to go through a cable. It is not that kind of "bus".

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.