Home Code Arduino and BH1745NUC Luminance and Colour Sensor example

Arduino and BH1745NUC Luminance and Colour Sensor example

by shedboy71

In this article we look at a BH1745NUC Luminance and Colour Sensor and connect it to an Arduino

The BH1745NUC is digital color sensor IC with I²C bus interface. This IC senses Red, Green and Blue light (RGB) and converts them to digital values. The high sensitivity, wide dynamic range and excellent Ircut characteristics makes this IC the most suitable to obtain the illuminance and color temperature of ambient light for adjusting LCD backlight of TV, mobile phone and tablet PC. It is possible to detect very wide range light intensity. (0.005 – 40k lx)

Specifications:

VCC Voltage Range: 2.3V to 3.6V
Maximum Sensitivity: 0.005Lx/step
Current Consumption: 130μA (Typ)
Standby Mode Current: 0.8μA (Typ)
Operating Temperature Range: -40°C to +85°C

Features

The High Sensitivity and Wide Dynamic Range (0.005 – 40k lx)
Supports Low Transmittance (Dark) Window
Correspond to I²C Bus Interface
Low Current by Power Down Function
Rejecting 50Hz/60Hz Light Noise
Correspond to 1.8V Logic Interface
Programmable Interrupt Function
It is possible to select 2 type of I²C bus slave address (ADDR =’L’: “0111000”, ADDR =’H’: “0111001”)

Here is a typical module that I used

 

 

Parts Required

I connected a sensor shield to an Arduino and then the sensor via connecting wire

Name Link
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO
BH1745NUC BH1745NUC Digital Color Sensor RGB Detecting Sensor Light Module
Connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire
sensor shield Expansion IO Board Sensor Shield

 

Schematic/Connection

Be careful as I used a CJMCU-1745 – the sensor is rated at 2.3V to 3.6V. So use the 3.3v out

 

Arduino Sensor
3.3v VIN
Gnd Gnd
SDA SDA
SCL SCL

Code Example

 

This is a controleverything example – they have code examples for various platforms

[codesyntax lang=”cpp”]

#include <Wire.h>

// I2C address of the BH1745NUC
#define Addr 0x38

void setup()
{
    // Initialise I2C communication as MASTER
    Wire.begin();
    // Initialise serial communication, set baud rate = 9600
    Serial.begin(9600);

    // Start I2C Transmission
    Wire.beginTransmission(Addr);
    // Select mode control register1
    Wire.write(0x41);
    // Set RGBC measurement time 160 msec
    Wire.write(0x00);
    // Stop I2C Transmission
    Wire.endTransmission();
    
    // Start I2C Transmission
    Wire.beginTransmission(Addr);
    // Select mode control register2
    Wire.write(0x42);
    // Set measurement mode is active, gain = 1x
    Wire.write(0x90);
    // Stop I2C Transmission
    Wire.endTransmission();
    
    // Start I2C Transmission
    Wire.beginTransmission(Addr);
    // Select mode control register3
    Wire.write(0x44);
    // Set default value
    Wire.write(0x02);
    // Stop I2C Transmission
    Wire.endTransmission();
    delay(300);
}

void loop()
{
    unsigned int data[8];
    for(int i = 0; i < 8; i++)
    {
        // Start I2C Transmission
        Wire.beginTransmission(Addr);
        // Select data register
        Wire.write((80+i));
        // Stop I2C Transmission
        Wire.endTransmission();
        
        // Request 1 byte of data from the device
        Wire.requestFrom(Addr, 1);
        
        // Read 8 bytes of data
        // Red lsb, Red msb, Green lsb, Green msb, Blue lsb, Blue msb
        // cData lsb, cData msb
        if(Wire.available() == 1)
        {
            data[i] = Wire.read();
        }
        delay(300);
    }

    // Convert the data
    int red = ((data[1] & 0xFF) * 256) + (data[0] & 0xFF);
    int green = ((data[3] & 0xFF) * 256) + (data[2] & 0xFF);
    int blue = ((data[5] & 0xFF) * 256) + (data[4] & 0xFF);
    int cData = ((data[7] & 0xFF) * 256) + (data[6] & 0xFF);
    
    // Output data to serial monitor
    Serial.print("Red Color luminance  : ");
    Serial.println(red);
    Serial.print("Green Color luminance : ");
    Serial.println(green);
    Serial.print("Blue Color luminance : ");
    Serial.println(blue);
    Serial.print("Clear Data Color luminance : ");
    Serial.println(cData);
}

[/codesyntax]

 

Output

Open the serial monitor and you should see something like the following

Red Color luminance : 46
Green Color luminance : 54
Blue Color luminance : 25
Clear Data Color luminance : 7
Red Color luminance : 44
Green Color luminance : 54
Blue Color luminance : 25
Clear Data Color luminance : 8
Red Color luminance : 45
Green Color luminance : 48
Blue Color luminance : 21
Clear Data Color luminance : 9

Place different colored objects beside the sensor and check the values

Share

You may also like

Leave a Comment