• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

i2C using different pins

Q&A Forum › i2C using different pins
0 Vote Up Vote Down
Joe Margevicius asked 2 years ago

Hi Sara … I want to assign different i2c pins for use with a BH1750 light sensor.  You have a very nice tutorial that does this very thing for the BME280 sensor, and when I implement your code, it works fine … but when I try to do it for the BH1750 sensor, it doesn’t.  An issue is that the BME code uses the Adafruit_BME280 library, but there isn’t an Adafruit_BH1750, so the code doesn’t translate.  I tried the following code, but it won’t work:

 

BH1750 lightMeter;

#define I2C_SDA 1
#define I2C_SCL 2
bool status;
TwoWire I2CBH1750 = TwoWire(0);

//*****************************************************************************
//********************** setup ************************************************
//*****************************************************************************
void setup() {
Serial.begin(115200);
delay(2000);
Serial.println(F(“BH1750 test”));
I2CBH1750.begin(I2C_SDA, I2C_SCL, 100000);

lightMeter.begin();
Serial.println(F(“BH1750 Test begin”));

/*
status = lightMeter.begin(0x23, &I2CBH1750);
if (!status) {
Serial.println(“Could not find a valid BH1750 sensor, check wiring!”);
while (1);
}
*/

 

I’ve commented out the lightmeter.begin code since it won’t accept an address … the BH1750 object apparently doesn’t have the same thing as the Adafruit_BME280 object does.

 

Do you have any solution to this?  Thanks!

5 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 2 years ago

Hi
Which library are you using to read from the light meter? Can you share a link to the library you’re using?
Regards,
Sara
P.S. I’m currently out of the office, so I might take longer to respond.

0 Vote Up Vote Down
Joe Margevicius answered 2 years ago

HI Sara,
It turned out that the .begin statement had to include the mode for this device (For the BME280, it only needs the address and the wire object pointer).  So, the following code works fine:
#include “BH1750.h”
#include “Wire.h”
BH1750 bh1750;
void setup() {
Serial.begin(115200);
Wire.begin(1, 2);
bh1750.begin(BH1750::CONTINUOUS_HIGH_RES_MODE, 0x23, &Wire);
}
void loop() {
float lightLevel;
if (bh1750.measurementReady()) {
lightLevel = bh1750.readLightLevel();
Serial.print(“lux = “); Serial.print(lightLevel);
delay(1000); }
}

Thanks for your help (I didn’t get a message that you had responded, which I usually get).

0 Vote Up Vote Down
Sara Santos Staff answered 2 years ago

Great!
I’m glad the issue is solved.
I’ll mark this as resolved. If you need further help, you just need to open a new question in our forum.
Regards,
Sara

0 Vote Up Vote Down
Joe Margevicius answered 2 years ago

Hi Again (I know this has been resolved, but ..).  It is not clear to me why the statement bh1750.begin(BH1750::CONTINUOUS_HIGH_RES_MODE, 0x23, &Wire); had to be used.  I tried another program which also works fine, and basically uses Wire.begin(SDA_pin, SCK_pin) (where those pins are integers defined elsewhere).
Here is a simpler code that works fine:
#include <Wire.h>
#include <BH1750.h>
BH1750 lightMeter;
void setup(){
Serial.begin(115200);
delay(1000);
Wire.begin(1,2); // Wire.begin(SDA_pin, SCL_pin)
//Wire.begin(); // use this if not assigning pins, but using default ones
lightMeter.begin();  // uses the default setting for this chip
Serial.println(“BH1750 initialized”);
}
void loop() {
float lux = lightMeter.readLightLevel();
Serial.print(“Light: “); Serial.print(lux); Serial.println(” lx”);
delay(1000);
}

0 Vote Up Vote Down
Sara Santos Staff answered 2 years ago

That statement, will probably do the same thing as wire.begin(), but you are passing more parameters, probably related to the way the measurements are taken.
Usually, on those libraries, if you don’t pass those parameters, they initialize the sensor with the default settings.
 
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.