Lost + Found BH1750 ambient light sensor driver library for Raspberry Pi Pico C SDK
  • C 83.8%
  • Python 10.4%
  • CMake 5.8%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-08-02 11:24:24 -07:00
ideas Add design strategy for calculating Daily Light Integral (DLI) 2026-08-02 11:24:24 -07:00
include Initial commit of lf-bh1750 light sensor driver 2026-08-02 10:51:55 -07:00
src Initial commit of lf-bh1750 light sensor driver 2026-08-02 10:51:55 -07:00
CMakeLists.txt Initial commit of lf-bh1750 light sensor driver 2026-08-02 10:51:55 -07:00
conanfile.py Initial commit of lf-bh1750 light sensor driver 2026-08-02 10:51:55 -07:00
README.md Initial commit of lf-bh1750 light sensor driver 2026-08-02 10:51:55 -07:00

lf-bh1750

Reusable BH1750 Ambient Light Sensor driver library for the Raspberry Pi Pico C SDK, packaged as a Conan package. This is designed for the BH1750 / GY-302 light intensity sensor breakout modules.

This is part of the Lost + Found suite of projects.


🛠️ Usage with CMake

Copy the driver directory into your project as a subdirectory:

add_subdirectory(lib/lf-bh1750)

target_link_libraries(your_project PRIVATE
    lf-bh1750
)

And in your C/C++ code:

#include "lf_bh1750.h"

🔌 Hardware Setup

Connect the BH1750/GY-302 breakout I2C pins to your Raspberry Pi Pico:

  • Pico GP4 (SDA) <-> Breakout SDA
  • Pico GP5 (SCL) <-> Breakout SCL
  • Pico GND <-> Breakout GND
  • Pico 3V3 OUT <-> Breakout VCC
  • Breakout ADD <-> Left unconnected (NC) for default address 0x23 or connected to 3V3 for alternate address 0x5C.

(Note: Pins can be configured on any available Pico I2C bus and GPIOs).


💡 Example

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/i2c.h"
#include "lf_bh1750.h"

#define I2C_PORT i2c0
#define PIN_SDA 4
#define PIN_SCL 5

int main() {
    stdio_init_all();
    sleep_ms(2000);

    // Initialize I2C
    i2c_init(I2C_PORT, 100 * 1000);
    gpio_set_function(PIN_SDA, GPIO_FUNC_I2C);
    gpio_set_function(PIN_SCL, GPIO_FUNC_I2C);
    gpio_pull_up(PIN_SDA);
    gpio_pull_up(PIN_SCL);

    // Create and initialize device context
    lf_bh1750_t sensor;
    if (!lf_bh1750_init(&sensor, I2C_PORT, LF_BH1750_I2C_ADDR_DEFAULT)) {
        printf("Failed to initialize driver context!\n");
        return -1;
    }

    // Ping device to verify it's connected
    if (!lf_bh1750_ping(&sensor)) {
        printf("BH1750 sensor not responding at address 0x%02X!\n", sensor.i2c_addr);
        return -1;
    }
    printf("BH1750 sensor detected successfully.\n");

    // Continuous Mode example: High Resolution Mode 2 (0.5 lx precision)
    if (lf_bh1750_set_mode(&sensor, LF_BH1750_CMD_CONT_H_RES_2)) {
        sleep_ms(180); // Wait for the first measurement to be ready
        
        while (true) {
            float lux = 0;
            if (lf_bh1750_read_lux(&sensor, LF_BH1750_CMD_CONT_H_RES_2, &lux)) {
                printf("Light Intensity: %.2f lx\n", lux);
            } else {
                printf("Failed to read BH1750 light sensor!\n");
            }
            sleep_ms(1000);
        }
    }

    return 0;
}

⚙️ Prerequisites: Setting up Conan with uv

To install the Conan CLI package manager securely and in an isolated environment on your system, it is recommended to use uv:

# Install Conan globally as an isolated tool
uv tool install conan

📦 Consumption via Forgejo Conan Registry

The package is hosted and distributed through the Forgejo Conan Package Registry at git.clowderlabs.com.

1. Add the Remote Registry

Add the remote repository to your local Conan configuration:

conan remote add clowder https://git.clowderlabs.com/api/packages/blong/conan

2. Authenticate

Log in using your personal access token:

conan remote login -p <your_token> clowder <username>

3. Add to your Project

Add the dependency to your conanfile.txt (or conanfile.py):

[requires]
lf-bh1750/0.0.1

Then install the dependencies:

conan install . --output-folder=build --build=missing

🛠️ Local Testing

To test building and exporting this Conan package locally without uploading, run:

conan export .

This will register lf-bh1750/0.0.1 in your local Conan cache for offline testing.