News:

Let's find out together what makes a PIC Tick!

Main Menu

BH1750 Lux Sensor

Started by shantanu@india, Mar 05, 2025, 10:26 AM

Previous topic - Next topic

shantanu@india

Hi,
Anyone got a working code?
The following does not seem to work:

Symbol LuxAddr 0x23    'Target BH1750 with address pin LOW 
Symbol HiRes   0x20    'One time high resolution mode

Declare SDA_Pin PORTA.6
Declare SCL_Pin PORTC.0

Dim lux         As Word

lux = BusIn LuxAddr,HiRes
Regards
Shantanu

flosigud

Yes, her is my suggestion:

'   ---------------------------------------------

'   Name    : BH1750.BAS                                     
'   Author  : Flosi Guðmundsson                                 
'   Notice  : Copyright (c) 2015 Flosi Guðmundsson             
'           : All Rights Reserved                               
'   Date    : 11.11.2015 - 30.10.2020                                       
'   Version : 1.0                                               
'   Notes   :                                                   
'           :                                                   
'   ---------------------------------------------
'   Include "..\..\33fj128GP802.inc"
   Include "..\..\33FJ12MC202.inc"
'   Include "..\..\33FJ32MC302.inc"

       
'   BH1750 constants
'   ---------------------------------------------

    Symbol  BH1750Write         0x46
    Symbol  BH1750Read          0x47
             
    Symbol  PowerDown           0x00
    Symbol  PowerUp             0x01
    Symbol  Reset               0x07
    Symbol  ContHiRes1          0x10
    Symbol  ContHiRes2          0x11
    Symbol  ContLoRes           0x13
    Symbol  OneTimeHiRes1       0x20
    Symbol  OneTimeHiRes2       0x21
    Symbol  OneTimeLoRes        0x23
   
    Dim LuxValue As Word
    Dim Mode As Byte 


                                           
$define BH1750init                       '
    HBusOut BH1750Write,[PowerDown]
   


Proc BH1750getLux(pMode As Byte),Byte
    HBusOut BH1750Write,[PowerUp]
    HBusOut BH1750Write,[pMode]                     
    DelayMS 125
    Result = HBusIn BH1750Read                     
    DelayMS 125
    HBusOut BH1750Write,[PowerDown]
    Result = Result*10/12
EndProc
     



                                 
    BH1750init
    Do                     
        LuxValue = BH1750getLux(ContHiRes2)
        HRSOut Dec LuxValue,13
        DelayMS 500
    Loop   

shantanu@india

Thanks Flosigud.
The following code worked:
        Symbol LuxRead 0x47        'Target BH1750 with LSB high
        Symbol LuxWrite 0x46        ' Target BH1750 with LSB low
        Symbol HiRes   0x20        'One time high resolution mode
        Symbol LoRes   0x23        'one time low resolution
        .............
        BStart
        BusOut LuxWrite
        BusOut HiRes
        BStop
        BStart
        BusOut LuxRead
        BusIn lux.HighByte
        BusAck
        BusIn lux.LowByte
        BusAck       
        BStop         
        lux=lux/12
        lux=lux*10
Regards
Shantanu

Pepe

To avoid losing resolution, first multiply by 10 and then divide by 12
lux=(lux*10)/12