Home Code Raspberry Pi 3 and BH1745NUC Luminance and Colour Sensor example

Raspberry Pi 3 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 a Raspberry Pi 3

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 the module that I used

 

 

Parts Required

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

Name Link
Raspberry Pi 3 2018 new original Raspberry Pi 3 Model B+ (plug) Built-in Broadcom 1.4GHz quad-core 64 bit processor Wifi Bluetooth and USB Port
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

 

Schematic/Connection

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

Raspberry Pi 3 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 had to change the print’s as the Mu editor did not like the original code using double quotes

[codesyntax lang=”python”]
# 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
[/codesyntax]

 

Output

This is the output that I saw

Red Color luminance : 224 lux
Green Color luminance : 124 lux
Blue Color luminance : 34 lux
Clear Data luminance : 48 lux

Place different colored objects beside the sensor and check the values

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More