Pimoroni BH1745 luminance and colour sensor breakout

I’ve been trying to activate The leds on the BH1745 colour sensor breakout using a Arduino microcontroller but I can’t seem to get it to work. I only found one arduino library for it on github which didn’t have a function for it and the python library that has one but I don’t know how to translate it to the library I’m currently using in a way that works. Can someone please help me out?
The Arduino library I found: GitHub - sunfounder/arduino-lib-bh1745: Arduino library for BH1745
The python library I found: GitHub - pimoroni/bh1745-python: Python library for the bh1745 colour sensor breakout

The function to read the sensor is in the read example:


What result did you get when you ran the example?

If you look at the library functions there is this one:

write8(uint8_t cmd, uint8_t data)

It appears that the LEDS are turned on by setting the status bit (bit 0) of the Interrupt Register (0x60) to 1, and turned off by setting it to zero… So if you were to execute the function with

write8(0x60, 1);

That would turn the LEDS on. Good practice would be to read the register first, set the bit and then write the register back, but it appears that the python code doesn’t so that, and the other bits of the register are only relevant if the interrupts need to be configured for a special purpose. If you could find the module documentation for how the interrupt is actually used to drive the LEDs that would be helpful.

According to the datasheet "INT pin is Nch open drain terminal so this terminal should be pull-up to some kind of voltage source by an external resister. " so it is possible that the module simply uses the interrupts to drive the LEDs in a pulsed mode - the settings for the interrupt could affect the brightness of the LEDs because the interrupts would occur at a different rate. Experimenting with the settings values would be interesting to see how the LEDs are affected. For instance, it might be possible to latch the signal using bit 4 (Latch) in addition to the Enable bit.

The settings could be set at each turn on or off, so there is no need for a separate configuration call.

When I ran the example I got a numeric value for red, green or Blue based on the light level. I tried the method you suggested to turn the led on but it still didn’t work

I have modified the file while messing around with the different functions and values from the library

What values did you try? I would suggest
0x01 - Enable. Apparently not enough, but presumably always required.
0x11 - Enable + Latch. Once the interrupt fires it stays set until reset
0x81 - Enable + Active. The Active bit setting does not have any description, but worth a try.
0x91 - Enable + Active + Latch.

The python initialisation of the register does a setup before setting the enable bit, but I can’t see why. Inserting the setLeds right alongside the other setxxx commands like you have should remove any requirement for additional initialisation. However the Python setup does set the latch bit, so perhaps latch + enable is the one.


Great suggestions @Jeff105671 Jeff …
@Jaden272654 feel free to post the whole modified file once you’ve had a crack and we’re happy to take a look for any red flags

While playing around with the sensor I found that if I connected one of the leds to ground they turned on so I think it might be that there’s a dodgy ground connection with the leds which stop them from working. Or is there another reason this could happen?

Hi Jaden,

Could you get a picture to explain what you mean here?

@Jaden272654 that’s expected, since the sensor IC will be controlling the LED using an open-drain output ie. It will connect one leg of the LED to ground, and the other end of the LED is permanently connected to Vcc.

If this solution is enough to get you over the line then go forth and prosper! You could bypass the coding troubles and solder a small ground wire to the LED - just make sure the current limiting resistor is in the path too!

If you want us to check the board, please upload as high-quality a macro image as you can.

1 Like

My current library code for the sensors
BH1745.zip (19.0 KB)
Could you please help me find out why it won’t work.

The essential documentation to enable an understanding of why it is not working is:

  1. The code you are now using. (Cut and paste the code is much better than a screen image)
  2. The actual setup you are using including any changes you made to the module. (Diagrams are usually better than pictures, but both is best)
  3. How far you got in the process of compiling and executing
  4. What you expected to happen
  5. What actually happened


#include <Arduino.h> //colour sensor library
#include <BH1745.h> //colour sensor library

// COLOUR SENSOR initialisation

#define SENSOR1_ADDRESS 0x38 // defines address for colour sensor1
#define SENSOR2_ADDRESS 0x39 // defines address for colour sensor2

BH1745 bh(SENSOR1_ADDRESS); // creates a BH1745 object to control colour sensor 1 using bh
BH1745 bh1(SENSOR2_ADDRESS); // creates a BH1745 object to control colour sensor 2 using bh1

// for ESP32 Change the SDA and SCl
#ifdef ESP32
#define SDA A5 // sets SDA pin to A5
#define SCL A4 // sets SCL pin to A4

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); // begins serial monitor at 9600
  //Colour sensors 
  // checks if a condition is met than does the following
  #ifdef ESP32 
  bool result = bh.begin(SDA, SCL); // initialises colour sensor 1
  //bool result1 = bh1.begin(SDA, SCL); // initialises colour sensor 2
  #else // if the condition above isn't met than it does the following
  bool result = bh.begin(); // initialises colour sensor 1
  bool result1 = bh1.begin(); // initialises colour sensor 2
  #endif // if none of those work than do the folling
  if (!result){ // if no result do the following
    Serial.println("Device Error"); // prints "Device Error" in the serial monitor
    while (1){;;} //prevents this from repeating more than once
  if (!result1){ // if no result1 do the following
    Serial.println("Device Error1"); // prints "Device Error1" in the serial monitor
    while (1){;;} //prevents this from repeating more than once

  bh.setGain(bh.GAIN_16X); // sets the gain of bh (colour sensor 1)
  bh.setRgbcMode(bh.RGBC_8_BIT); // sets the RgbcMode of bh (colour sensor 1)
  bh1.setGain(bh.GAIN_16X); // sets the gain of bh1 (colour sensor 2)
  bh1.setRgbcMode(bh.RGBC_8_BIT); // sets the RgbcMode of bh1 (colour sensor 2)

void loop() {
  // put your main code here, to run repeatedly:
  bh.read(); // reads colour sensor 1
  bh1.read(); // reads colour sensor 2*

  // colour sensor 1 values

  // colour sensor 2 values
  Serial.print(" RGBH2:");Serial.print(bh1.red);Serial.print(',');Serial.print(bh1.green);Serial.print(',');Serial.print(bh1.blue);Serial.print(',');Serial.print(bh1.hue);Serial.println(',');   

I’m using Arduino Uno using the break out pins on the sensor not the jst clip provided for raspberry pi. When I tried you earlier suggestions the leds on the sensor didn’t turn on and but based off the data sheet and the python program it should have. It compiled and uploads but it does give this error when uploaded to the microcontroller.

I expected the lights to turn on but they didn’t. I also modified the library to take two sensor addresses, as well as the set led function for the library

The messages in the compile log are indicating that the literal arguments passed in the call to the function can be resolved to either a uint8_t or an int, and therefore the function overloads are ambiguous. The compiler will choose one of them, and I doubt it makes any difference which is chosen. In any case the compilation completes. You could edit the library to replace the overloads with unique function names, but it’s not worth the trouble.

If the LED still doesn’t turn on then I am afraid that I do not have any more ideas about what code might be required to implement this. You could post your problem on some other forums, and re-run your search for a solution every now and again in case someone else has the same problem.

I looked through and realised I didn’t have all the required libraries so I need to fix that.

Does anyone know of a wire.h library that has a Send function, and available function?

I would suggest that the above question is a different topic that deserves its own thread. You should describe what you expect these functions to do. Depending on exactly what you are after I would expect that ‘send’ is part of the wire protocol and ‘available’ depends on the device, but some clarification would be required.