My friend and I are trying to write a library for the Pimoroni BH1745 color sensor breakout, as the existing libraries do not completely support all features. However, Wire.available() always returns 0 when reading registers, even though we have confirmed the sensor working using the official example and a Raspberry Pi Pico. For testing, I also tried the exact I2C code from another library, also confirmed working, but it didn't help. Our code is available on Codeberg:
void_panic/arduino-bh1745: Arduino bindings for the BH1745 RGB color sensor - Codeberg.org
That is telling me there is no data available. Without an annotated schematic showing exactly how this was wired and the code all I can do is make guesses which is a waist of time for both of us.
Can you implement this very simple code to check if it works? pay attention to the wire part inside the class:
BH1745.h
/**
*******************************************************************************************
* @file : BH1745.h
* @brief : BH1745 Library
*******************************************************************************************
* The BH1745 is a digital color sensor IC with I2C bus interface...
*
* @details
*
*
*******************************************************************************************
*/
#include <Arduino.h>
#include <Math.h>
#include <Wire.h>
#ifndef INC_BH1745_H_
#define INC_BH1745_H_
#define BH1745_ADDRESS_1 0x38 // If ADDR Pin = '0'
#define BH1745_ADDRESS_2 0x39 // If ADDR Pin = '1'
#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
// Registers
#define BH1745_SYSTEM_CONTROL 0x40
#define BH1745_MODE_CONTROL1 0x41
#define BH1745_MODE_CONTROL2 0x42
#define BH1745_MODE_CONTROL3 0x44
#define BH1745_RED_DATA_LSB 0x50
#define BH1745_RED_DATA_MSB 0x51
#define BH1745_GREEN_DATA_LSB 0x52
#define BH1745_GREEN_DATA_MSB 0x53
#define BH1745_BLUE_DATA_LSB 0x54
#define BH1745_BLUE_DATA_MSB 0x55
#define BH1745_CLEAR_DATA_LSB 0x56
#define BH1745_CLEAR_DATA_MSB 0x57
#define BH1745_DINT_DATA_LSB 0x58
#define BH1745_DINT_DATA_MSB 0x59
#define BH1745_INTERRUPT 0x60
#define BH1745_PERSISTENCE 0x61
#define BH1745_TH_LSB 0x62
#define BH1745_TH_MSB 0x63
#define BH1745_TL_LSB 0x64
#define BH1745_TL_MSB 0x65
#define BH1745_MANUFACTURER_ID 0x92
class BH1745{
public:
BH1745(); // Constructor. It must be defined with the same name as the class
void init(uint8_t devAddress, TwoWire *wire = &Wire);
void writeRegister(uint8_t registerAddress, uint8_t value);
uint8_t readRegister(uint8_t registerAddress);
uint8_t getManufacturerId();
private:
uint8_t _deviceAddress;
TwoWire * _wire; //The generic connection to user's chosen I2C hardware
};
#endif
BH1745.cpp
#include <BH1745.h>
#include <Wire.h>
#include "math.h"
// Constructor
BH1745::BH1745()
{
}
void BH1745::writeRegister(uint8_t registerAddress, uint8_t value)
{
_wire->beginTransmission(_deviceAddress);
_wire->write(registerAddress);
_wire->write(value);
return _wire->endTransmission();
}
uint8_t BH1745::readRegister(uint8_t registerAddress)
{
_wire->beginTransmission(_deviceAddress);
_wire->write(registerAddress);
_wire->endTransmission();
_wire->requestFrom(_deviceAddress, (uint8_t)1);
uint8_t value = _wire->read();
return value;
}
void BH1745::init(uint8_t devAddress, TwoWire *wire)
{
_deviceAddress = devAddress;
_wire = wire;
//Your code here. Yo can initialize your MODE_CONTROL registers for example
}
uint8_t BH1745::getManufacturerId()
{
uint8_t value = readRegister(BH1745_MANUFACTURER_ID);
return value;
}
BH1745_Example.ino
#include <BH1745.h>
BH1745 bh1745;
char s1[10];
uint8_t registerValue;
void setup() {
// put your setup code here, to run once:
Wire.begin();
Wire.setClock(400000);
Serial.begin(115200); // Start serial communication at 115200 baud
while (!Serial) {
// will pause Zero, Leonardo, etc until serial console opens
delay(1);
}
bh1745.init(BH1745_ADDRESS_1);
}
void loop() {
// put your main code here, to run repeatedly:
registerValue = bh1745.getManufacturerId();
sprintf(s1, "%02X", registerValue);
Serial.print("MANUFACTURER ID Register: 0x"); Serial.println(s1);
delay(1000);
}