How to Integrate BH1750 Light Sensor with AI Agent ASI Biont – Smart Lighting Automation
Introduction
Lighting accounts for about 15% of global electricity consumption (IEA, 2023). Yet most homes and offices still use static lighting schedules that ignore actual daylight availability. The BH1750 ambient light sensor, together with an AI agent like ASI Biont, can bridge this gap: the sensor measures lux in real time, and the AI decides when to dim, turn off, or adjust blinds — without you writing a single line of boilerplate integration code.
This article walks you through a complete integration of the BH1750 (digital light sensor, I²C interface) with ASI Biont. We’ll use an ESP32 as the sensor microcontroller, the ASI Biont Hardware Bridge for serial communication, and the AI agent to automate lighting based on real light levels.
What is the BH1750?
The BH1750FVI is a 16‑bit digital ambient light sensor manufactured by Rohm Semiconductor (datasheet: BH1750FVI Technical Note). It uses I²C for communication and outputs illuminance directly in lux with a range of 1–65535 lx. Its key advantages:
- High resolution – 0.5 lx (H‑Resolution Mode)
- Low power – 0.12 mA average
- I²C address – 0x23 (ADDR pin low) or 0x5C (ADDR pin high)
The sensor is widely used in smartphones, automotive dashboards, and smart lighting systems because it does not require an ADC or complex calibration.
Why ASI Biont for Sensor Integration?
ASI Biont is an AI agent that connects to almost any physical device through multiple protocols (see full list in ASI Biont documentation). Instead of manually writing and debugging Python scripts for each sensor, you simply describe the task in natural language. The AI writes the integration code, executes it in a sandbox environment, and can continually interact with the device.
For the BH1750, we will use the Hardware Bridge method – a lightweight bridge.py program running on your PC that forwards commands between ASI Biont (in the cloud) and your local serial port. The user only needs to specify the COM port and baud rate in the chat.
Integration Architecture
Below is the typical data flow:
BH1750 (I²C) → ESP32 (MicroPython) → UART (USB) → PC (bridge.py) ↔ WebSocket ↔ ASI Biont Cloud
- ESP32 reads BH1750 via I²C every few seconds.
- ESP32 prints the lux value as a text string over the serial‑over‑USB link (e.g.,
LUX: 450). - bridge.py (on user’s PC) connects to ASI Biont via WebSocket and listens for commands. It also reads the serial port continuously and sends incoming data back to the cloud.
- ASI Biont AI agent sends
industrial_commandwithserial://protocol to request a reading, or the agent can receive unsolicited data from the bridge (if configured viabridge://). - AI agent analyzes the lux values, logs them, and can send back commands to control an LED or relay via the same serial channel.
Step‑by‑Step Integration Guide
1. Hardware Wiring
| BH1750 Pin | ESP32 Pin |
|---|---|
| VCC | 3.3V |
| GND | GND |
| SCL | GPIO22 |
| SDA | GPIO21 |
| ADDR | GND (addr 0x23) |
Optionally connect a LED + resistor to GPIO2 (or any output pin) to demonstrate automated control.
2. MicroPython Firmware for ESP32
Upload the following code to your ESP32 using Thonny or ampy. It reads the BH1750 every 2 seconds and prints the result to the serial console.
# main.py – BH1750 reader for ASI Biont integration
import machine
import time
# BH1750 I2C address (ADDR = GND → 0x23)
BH1750_ADDR = 0x23
# I2C initialization (ESP32 default: SCL=22, SDA=21)
i2c = machine.I2C(0, scl=machine.Pin(22), sda=machine.Pin(21))
def init_sensor():
# Power on
i2c.writeto(BH1750_ADDR, bytes([0x01]))
# Set high resolution mode (1 lx resolution, 120 ms measurement time)
i2c.writeto(BH1750_ADDR, bytes([0x10]))
time.sleep_ms(180)
def read_lux():
data = i2c.readfrom(BH1750_ADDR, 2)
lux = (data[0] << 8) | data[1]
lux /= 1.2 # correction factor for H‑Res mode
return lux
init_sensor()
while True:
lux = read_lux()
print(f"LUX: {lux:.1f}")
time.sleep(2)
Test that you see lines like LUX: 320.5 in your serial monitor. Important: The ESP32 must be connected to your PC via USB and communicate at the same baud rate that bridge.py will use.
3. Configure and Run Hardware Bridge
- Log in to asibiont.com → Devices → Create API Key. Download the generated
bridge.py. - Identify the COM port of your ESP32:
- Windows:
COM3,COM4etc. in Device Manager. - Linux/macOS:
/dev/ttyUSB0or/dev/ttyACM0. - Install dependencies (if not already done):
bash pip install pyserial requests websockets - Launch bridge:
bash python bridge.py --token=YOUR_API_TOKEN --ports=COM3 --baud 115200
The bridge connects to ASI Biont via WebSocket and starts listening on the serial port. It automatically forwards any incoming serial data to the cloud.
4. Ask ASI Biont to Read the Sensor
Open the ASI Biont chat interface and describe your setup:
“Connect to BH1750 light sensor via COM3 at 115200 baud. Read the lux value every 5 seconds and log it. If lux falls below 100, turn on an LED on pin 2; if above 500, turn it off.”
The AI agent will:
- Recognize the device context (BH1750).
- Use industrial_command(protocol='serial://', command='serial_write_and_read', data='4c55580a') (sends "LUX\n") to request a reading.
- The bridge sends "LUX\n" to the ESP32; the ESP32’s firmware should respond to that command. (We need to adjust the firmware to listen for commands.)
Better approach: Instead of polling, configure bridge to forward all incoming data (the ESP32 prints every 2 sec). The AI agent can receive the data stream via bridge:// protocol and analyze it. The AI writes a Python script using execute_python with paho-mqtt or websockets if needed, but for simple analysis, the AI can directly use the chat context.
Note: The serial_write_and_read operation is atomic – it sends data and waits for a response. You can also use industrial_command(protocol='bridge://', command='raw', data='...') to receive unsolicited data. The exact mechanism is chosen by the AI based on your description.
5. Full Automation Scenario: Smart Lighting
Let’s assume you have a smart LED bulb controlled by a relay connected to ESP32 GPIO2. The firmware listens for:
- LED_ON → set GPIO2 high
- LED_OFF → set GPIO2 low
The ASI Biont AI agent monitors the streaming lux data and autonomously decides when to toggle the light. You can also ask the AI to create a schedule, e.g.:
“From 8 AM to 6 PM, keep LED off if lux > 300; otherwise turn it on. At night, always keep it off except if motion is detected (future sensor).”
The AI implements this logic as a Python script inside execute_python (using the paho-mqtt library if you use MQTT, or via serial_write_and_read commands sent periodically via industrial_command). The script runs in the cloud with a 30‑second timeout – for long‑running automations, the AI uses a polling loop with time.sleep inside the sandbox, which is fine as long as the total execution stays under the timeout. Alternatively, the AI can set up a recurring task by writing a script that runs on bridge (not currently supported directly, but the AI can output code for you to run locally).
For reliability, consider using MQTT instead of serial: the ESP32 can publish lux readings to a broker, and ASI Biont subscribes via paho-mqtt in execute_python. This decouples the sensor from the PC and allows wireless operation.
Alternative Connection: MQTT
If your ESP32 has WiFi, MQTT is often more robust.
- Install
umqtt.simpleon ESP32. - Publish lux to topic
sensor/bh1750/luxevery 2 seconds. - Run an MQTT broker (e.g., Mosquitto on a Raspberry Pi or cloud broker like HiveMQ Cloud).
- In ASI Biont chat, say:
“Connect to MQTT broker at test.mosquitto.org:1883, subscribe to sensor/bh1750/lux, log values every 10 seconds, and publish to a topic lamp/command when threshold is crossed.”
- The AI writes an
execute_pythonscript usingpaho.mqttthat runs in the sandbox, subscribes to the topic, processes data, and publishes control messages.
Comparison:
| Method | Latency | Range | Complexity | Best for |
|---|---|---|---|---|
| Serial (USB) | <50 ms | Physical cable (PC nearby) | Low | Prototyping, plugged device |
| MQTT | 100‑500 ms | WiFi range | Medium | Production, wireless |
| Modbus/TCP | <10 ms | Ethernet | High | Industrial environments |
For the BH1750 at home, both serial and MQTT work well. The choice depends on your hardware setup.
Why ASI Biont Saves You Time
Traditionally, integrating a sensor like BH1750 requires:
- Writing firmware (we did above)
- Writing a Python script to read serial, parse data, implement control logic
- Deploying and debugging
With ASI Biont, you only need to:
1. Upload the simple sensor‑reader firmware.
2. Run bridge.py with your API token.
3. Describe your automation in plain English.
The AI agent writes the entire integration code – including error handling, logging, and actuation – in seconds. And because ASI Biont supports over 15 protocols (Modbus, OPC‑UA, HTTP, CAN, etc.), you can scale from this single sensor to a whole factory floor without changing your workflow.
Conclusion
The BH1750 light sensor, when paired with ASI Biont, becomes a building block for truly adaptive lighting. You save energy, improve comfort, and avoid writing boilerplate glue code. Whether you connect it via serial USB or over MQTT, the AI agent handles the complexity.
Ready to automate your lighting?
Try ASI Biont for free at asibiont.com – download bridge.py, plug in your BH1750, and type: “Connect my light sensor and keep my room at 400 lux during daytime.”
Reference: Rohm Semiconductor – BH1750FVI Datasheet. IEA – Lighting Energy Efficiency, 2023.
Comments