Raspberry PI 3 and BH1745NUC Luminance and Colour Sensor example in python

In this article we look at a BH1745NUC Luminance and Colour Sensor and connect it to a Raspberry PI

Lets look at that sensor first – here is some of the technical information abou  it.

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, this is called the CJMCU-1745

 

 

Parts Required

I connected a sensor shield to a Raspberry Pi 3 and then the sensor via connecting wire

You can easily use a Pi Zero for a simple example like this

Name Link
Raspberry Pi Raspberry Pi 3 Model B BCM2837 1.2G with WIFI and Bluetooth
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
Raspberry Pi 3 Expansion Board Raspberry Pi 3 Expansion Board

 

Schematic/Connection

 

Raspberry Pi Sensor
3.3v VIN
Gnd Gnd
SDA SDA
SCL SCL

Code Example

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

I pasted this into Thonny on the raspberry pi

Distributed with a free-will license.
# Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
# BH1745NUC
# This code is designed to work with the BH1745NUC_I2CS I2C Mini Module available from ControlEverything.com.
# https://www.controleverything.com/content/Color?sku=BH1745NUC_I2CS#tabs-0-product_tabset-2
 
import smbus
import time
 
# Get I2C bus
bus = smbus.SMBus(1)
 
# BH1745NUC address, 0x38(56)
# Select mode control register1, 0x41(65)
#       0x00(00)    RGBC measurement time = 160 ms
bus.write_byte_data(0x38, 0x41, 0x00)
# BH1745NUC address, 0x38(56)
# Select mode control register2, 0x42(66)
#       0x90(144)   RGBC measurement active, Gain = 1X
bus.write_byte_data(0x38, 0x42, 0x90)
# BH1745NUC address, 0x38(56)
# Select mode control register3, 0x44(68)
#       0x02(02)    Default value
bus.write_byte_data(0x38, 0x44, 0x02)
 
time.sleep(0.5)
 
# BH1745NUC address 0x38(56)
# Read data back from 0x50(80), 8 bytes
# Red LSB, Red MSB, Green LSB, Green MSB, Blue LSB, Blue MSB
# cData LSB, cData MSB 
data = bus.read_i2c_block_data(0x38, 0x50, 8)
 
# Convert the data
red = data[1] * 256 + data[0]
green = data[3] * 256 + data[2]
blue = data[5] * 256 + data[4]
cData = data[7] * 256 + data[6]
 
# Output data to the screen
print ("Red Color luminance : %d lux" %red)
print ("Green Color luminance : %d lux" %green)
print ("Blue Color luminance : %d lux" %blue)
print ("Clear Data luminance : %d lux" %cData)

 

 

Output

Red Color luminance : 78 lux
Green Color luminance : 39 lux
Blue Color luminance : 7 lux
Clear Data luminance : 19 lux

Red Color luminance : 79 lux
Green Color luminance : 40 lux
Blue Color luminance : 7 lux
Clear Data luminance : 19 lux

Place different colored objects beside the sensor and check the values