Skip to content

RVX/Rpi_Lux_Monitor_BH1750_Oled

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

Raspberry Pi OLED + BH1750 Monitor

Real-time Raspberry Pi monitor for an SSD1306 OLED display (128x64, I2C) with one or two BH1750 light sensors.

Displayed information:

  • User and hostname
  • Local IP address
  • CPU temperature
  • L1 and L2 with individual sensor readings
  • Av with the average of all available sensors
  • Right-side vertical trend graph
  • Low-light warning
  • NoS when a sensor is not responding

Hardware

  • Raspberry Pi (tested on Raspberry Pi 5)
  • SSD1306 I2C OLED display (typical address: 0x3C)
  • Primary BH1750 at 0x23 (ADDR tied to GND)
  • Optional secondary BH1750 at 0x5C (ADDR tied to 3.3V)

Typical GPIO wiring:

  • VCC -> 3.3V
  • GND -> GND
  • SDA -> GPIO2 (pin 3)
  • SCL -> GPIO3 (pin 5)

If you use two BH1750 sensors, both share the same I2C bus. The only difference is the ADDR pin:

  • Sensor 1: ADDR -> GND -> 0x23
  • Sensor 2: ADDR -> 3.3V -> 0x5C

1) Enable I2C

sudo raspi-config nonint do_i2c 0

Optional verification:

i2cdetect -y 1

You should normally see:

  • 23 for the first BH1750
  • 5c for the second BH1750, if connected
  • 3c for the OLED display

2) Virtual Environment and Installation

cd ~
python3 -m venv screen_sensor --system-site-packages
source screen_sensor/bin/activate
pip install --upgrade pip
pip install luma.oled smbus2 pillow

3) Manual Execution

cd ~
source screen_sensor/bin/activate
python3 rpi_lux_monitor_bh1750_oled.py

Current OLED Layout

Current on-screen layout:

  • Top row: user@host and CPU temperature
  • Next row: full local IP address
  • Next row: L1 and L2 on the same line
  • Lower-left row: Av with the average of valid sensor readings
  • Right side: vertical trend graph

Behavior:

  • If one sensor fails, its field shows NoS
  • If both sensors fail, the display shows SENSOR ERROR
  • If only one sensor fails, Av is calculated from the remaining valid sensor

4) systemd Service (Automatic Startup)

Create the service file:

sudo nano /etc/systemd/system/rpi_lux_monitor_bh1750_oled.service

Contents:

[Unit]
Description=Rpi Lux Monitor BH1750 OLED service
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=rvx
WorkingDirectory=/home/rvx
ExecStart=/home/rvx/screen_sensor/bin/python /home/rvx/rpi_lux_monitor_bh1750_oled.py
Environment=PYTHONUNBUFFERED=1
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

Enable and start it:

sudo systemctl daemon-reload
sudo systemctl enable rpi_lux_monitor_bh1750_oled.service
sudo systemctl restart rpi_lux_monitor_bh1750_oled.service

5) Operation and Diagnostics

sudo systemctl status rpi_lux_monitor_bh1750_oled.service
sudo journalctl -u rpi_lux_monitor_bh1750_oled.service -f

6) Debugging

If something is not working, check these three areas in this order.

Service Status

systemctl is-active rpi_lux_monitor_bh1750_oled.service
systemctl is-enabled rpi_lux_monitor_bh1750_oled.service

Expected result:

  • active: the monitor is currently running
  • enabled: the monitor will start automatically at boot

If you do not see those values, restart the service:

sudo systemctl restart rpi_lux_monitor_bh1750_oled.service

Service Logs

sudo journalctl -u rpi_lux_monitor_bh1750_oled.service -n 50 --no-pager

Expected log entries:

  • Started rpi_lux_monitor_bh1750_oled.service
  • OLED monitor iniciado.

If you see errors such as Error inicializando hardware, the issue is usually related to I2C, wiring, device addressing, or permissions.

Detected I2C Devices

i2cdetect -y 1

Expected result:

  • 23 for the primary BH1750
  • 5c for the secondary BH1750, if connected
  • 3c for the SSD1306 OLED display

Quick interpretation:

  • If 23 or 3c is missing, check wiring, power, and confirm that I2C is enabled.
  • If you are using two sensors and 5c is missing, check the second BH1750 wiring and its ADDR pin.
  • If all expected addresses are visible, the I2C bus is working and the issue is more likely in the service configuration or the script.

Code Improvements Applied

  • Removed shell=True from command execution
  • Optimized temperature reading through /sys/class/thermal/thermal_zone0/temp
  • Added system telemetry caching for IP and temperature to reduce CPU load
  • Added support for two BH1750 sensors at 0x23 and 0x5C
  • Added automatic averaging of valid sensor readings
  • Added robust sensor error handling (NoS per sensor and SENSOR ERROR if all sensors fail)
  • Added clean shutdown handling with SIGINT and SIGTERM
  • Added a compact vertical graph to preserve space for individual readings

Quick Settings in rpi_lux_monitor_bh1750_oled.py

Useful parameters:

  • REFRESH_SECONDS: display refresh interval
  • SYSINFO_REFRESH_SECONDS: IP/temperature update interval
  • LOW_LUX_THRESHOLD: low-light warning threshold
  • NIGHT_LUX_THRESHOLD: night contrast threshold
  • MIN_CONTRAST / MAX_CONTRAST: OLED contrast range

About

Real-time Raspberry Pi OLED monitor using an SSD1306 display and 2 BH1750 light sensor, with adaptive contrast, live lux tracking of both sensors and average reading, CPU temperature, IP info, and systemd auto-start support.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages