Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    BH1750 always give 0 lx

    Scheduled Pinned Locked Moved LoPy
    10 Posts 4 Posters 48 Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • L Offline
      Luca Albano
      last edited by Luca Albano

      Hi,
      I have a problem with BH1750 light sensor.
      It is inside a sensorial node and communicate with LoPy by I2C bus.
      In the first cicle, i have a correct measure, for example (light is the first value: 79):

      79;22.0;63.5;1017.7;17;25;32;82;2021-02-17 10:53:55
      

      From 2nd, i have always 0.
      This is the code:

      from sht10 import SHT10
      from machine import Pin
      import time
      import pycom
      import struct
      import math
      import bh1750fvi
      import bme680
      from i2c import I2CAdapter
      from hm3301 import HM3301
      from network import Sigfox
      import socket
      import utime
      from machine import I2C
      import machine
      from network import WLAN
      from machine import RTC
      from machine import SD
      import os
      import sys
      
      pycom.heartbeat(False) # disable the blue blinking
      
      # ____ SD _____
      try:
          sd = SD()
          os.mount(sd, '/sd')
      
          # check the content
          os.listdir('/sd')
          # f = open('/sd/data.csv', 'w')
      except:
          print("Error: no SD")
          sys.exit(1)
      
      # ____ WiFi connession _____
      # configure the WLAN subsystem in station mode (the default is AP)
      print("Configuring WLAN...")
      wlan = WLAN(mode=WLAN.STA)
      # go for fixed IP settings (IP, Subnet, Gateway, DNS)
      wlan.ifconfig(config=('192.168.1.107', '255.255.255.0', '192.168.1.1', '192.168.1.1'))
      wlan.scan()     # scan for available networks
      wlan.connect(ssid='*********', auth=(WLAN.WPA2, '*********'))
      while not wlan.isconnected():
          pass
      print(wlan.ifconfig())
      time.sleep(5)
      
      # ____ Update time _____
      print("Update time...")
      rtc = RTC()
      rtc.ntp_sync("it.pool.ntp.org")
      
      data_pin = 'P9'
      sck_pin = 'P10'
      
      p_out_light = Pin('P20', mode=Pin.OUT)
      p_out_light.value(1)
      time.sleep(1)
      
      #i2c_var = I2CAdapter(pins=(data_pin, sck_pin))
      i2c=I2C(0, I2C.MASTER)
      i2c.scan()
      
      #print(i2c_var.scan())
      
      p_out_light.value(0) # Pin off
      
      try:
          while True:
              f = open('/sd/data.csv', 'a')
      
              date = rtc.now()
      
              p_out_light.value(1)
              print("---- PIN ON ----")
              time.sleep(1)
      
              pycom.rgbled(0xFFA500)
              time.sleep(0.5)
              pycom.rgbled(0x000000)
              time.sleep(0.5)
              pycom.rgbled(0xFFA500)
              time.sleep(0.5)
              pycom.rgbled(0x000000)
              time.sleep(0.5)
              pycom.rgbled(0xFFA500)
              time.sleep(0.5)
              pycom.rgbled(0x000000)
              time.sleep(0.5)
              pycom.rgbled(0xFFA500)
              time.sleep(0.5)
              pycom.rgbled(0x000000)
              time.sleep(0.5)
      
              print("Measuring...")
              output = ""
      
              # Luminosità
              i2c_var = I2CAdapter(pins=(data_pin, sck_pin))
              #i2c_var.writeto(i2c_var.scan()[0], bytes([0x10]))
              light_sensor = bh1750fvi.BH1750FVI(i2c_var)
              # print("LUX:")
              print(i2c_var.scan()[0])
              lux = light_sensor.read()
              i2c_var.writeto(i2c_var.scan()[0], bytes([0x10]))
              #print(lux)
              output = str(lux) + ";"
              time.sleep(1)
              #time.sleep(1)
      
              # Temperatura
              sht = SHT10(data_pin, sck_pin)
              tmp = sht.readTemp()
              hum = sht.readHum()
              output += "{:.1f};{:.1f};".format(round(tmp,1), round(hum,1))
              time.sleep(1)
      
              # Pressione
              i2c_dev = I2CAdapter(pins=(data_pin, sck_pin))
              # print("PRESSION:")
              print(i2c_dev.scan()[1])
              # dev_add = i2c_dev.findSlaves()
              # print(dev_add)
              sensor = bme680.BME680(i2c_device=i2c_dev, i2c_addr=i2c_dev.scan()[1]) # se da errore "index out of range", cambiare da 1 a 0, poi da 0 ad 1 e ricaricare 4-5 volte
              sensor.set_pressure_oversample(bme680.OS_4X)
              sensor.set_filter(bme680.FILTER_SIZE_3)
              if sensor.get_sensor_data():
                  output += "{:.1f};".format(round(sensor.data.pressure,1))
              time.sleep(1)
      
              # PM
              time.sleep(1)
              hm = HM3301('P21', 'P22') # P21 -> data , P22 -> clock
              #hm.findSlaves()
              pm1 = hm.readStandardPM()
              dep_string = ""
              pm_index_count = 0 # indice per PM
              c_pm1 = 0
              c_pm10 = 0
              c_pm25 = 0
              for digit in pm1:
                  pm_index_count += 1
      
                  if pm_index_count == 1:
                      c_pm1 = digit
                  elif pm_index_count == 2:
                      c_pm25 = digit
                  elif pm_index_count == 3:
                      c_pm10 = digit
              time.sleep(1)
              pm2 = hm.readAtmosphericPM()
              dep_string = ""
              pm_index_count = 0 # indice per PM
              c_pm1atm = 0
              c_pm10atm = 0
              c_pm25atm = 0
              for digit in pm2:
                  pm_index_count += 1
                  if pm_index_count == 1:
                      c_pm1atm = digit
                  elif pm_index_count == 2:
                      c_pm25atm = digit
                  elif pm_index_count == 3:
                      c_pm10atm = digit
              time.sleep(1)
      
              # Media tra valori PM std e PM atm
              c_pm1_fin =  (c_pm1 + c_pm1atm) / 2
              c_pm10_fin = (c_pm10 + c_pm10atm) / 2
              c_pm25_fin = (c_pm25 + c_pm25atm) / 2
      
              output += str(round(c_pm1_fin)) + ";" + str(round(c_pm25_fin)) + ";" + str(round(c_pm10_fin)) + ";"
      
              vl_pm10 = 50 # limite giornaliero PM10 -> è 50 per la legge italiana
              vl_pm25 = 25 # limite giornaliero PM2.5
      
              # PM1 -> NON é POSSIBILE INSERIRLI PERCHé NON C'é UN LIMITE GIORNALIERO MESSO PER LEGGE
      
              # sottoindici
              i_pm10 = (c_pm10_fin / vl_pm10) * 100
              i_pm25 = (c_pm25_fin / vl_pm25) * 100
      
              index_pm = round((i_pm10 + i_pm25) / 2)
      
              if index_pm >= 0 and index_pm <= 50:
                  dep_pm = 1 # "OTTIMA"
              elif index_pm > 50 and index_pm <= 75:
                  dep_pm = 2 # "BUONA"
              elif index_pm > 75 and index_pm <= 100:
                  dep_pm = 3 # "DISCRETA"
              elif index_pm > 100 and index_pm <= 125:
                  dep_pm = 4 # "MEDIOCRE"
              elif index_pm > 125 and index_pm <= 150:
                  dep_pm = 5 # "POCO SALUBRE"
              elif index_pm > 150 and index_pm <= 175:
                  dep_pm = 6 # "INSALUBRE"
              elif index_pm > 175:
                  dep_pm = 7 # "MOLTO INSALUBRE"
      
              # init Sigfox for RCZ1 (Europe)
              print("Init SigFox...")
              sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1)
              # create a Sigfox socket
              s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW)
              # make the socket blocking
              s.setblocking(True)
              # configure it as uplink only
              s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False)
      
              # send AQI
              my_string = str(round(tmp))+";"+str(round(hum))+";"+str(dep_pm)
              my_bytes = my_string.encode('utf-8')
              s.send(my_bytes)
              time.sleep(5)
      
              time.sleep(1)
              pycom.rgbled(0x2eff00)
              time.sleep(1)
              pycom.rgbled(0x000000)
              time.sleep(1)
      
              output += str(index_pm) + ";"
              dateUTC = '{:02d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format(date[0],date[1],date[2],date[3],date[4],date[5])
              output += str(dateUTC) #timestamp
      
              print(output)
      
              # try some standard file operations
              f.seek(0, 2) # seek to end
              f.write(output+"\r\n")
              f.close()
      
              p_out_light.value(0)
              print("---- PIN OFF ----\n\n")
      
              index_time = 1 #600 # effettuo 6 misurazioni all'ora, 1 ogni 10 min
              while(index_time > 0):
                  if index_time % 2:
                      pycom.rgbled(0x4F3394)
                  else:
                      pycom.rgbled(0x000000)
                  time.sleep(1)
                  index_time = index_time - 1
      
              pycom.rgbled(0x000000)
      
              s.close()
      
              time.sleep(1)
      
      except KeyboardInterrupt:
          print("\n---- END POLLING ----\n")
          f.close()
          pass
      
      

      I have found something similar in forum (here) but i don't know how to solve this problem.

      Sorry for my english!
      Thanks a lot.

      1 Reply Last reply Reply Quote 0
      • M Offline
        Matthew Felgate 0 Global Moderator
        last edited by

        Can you try reading the sensor twice?
        (and throw away the first result)

        light_sensor_temp = bh1750fvi.BH1750FVI(i2c_var)
        light_sensor = bh1750fvi.BH1750FVI(i2c_var)
        
        L 1 Reply Last reply Reply Quote 0
        • L Offline
          Luca Albano @Matthew Felgate 0
          last edited by

          @Matthew-Felgate-0 I have tried but this is the ouput:

          Configuring WLAN...
          ('192.168.1.107', '255.255.255.0', '192.168.1.1', '192.168.1.1')
          Update time...
          ---- PIN ON ----
          Measuring...
          35
          119
          Init SigFox...
          205;21.2;62.8;1016.0;19;28;33;89;2021-02-17 15:22:11
          ---- PIN OFF ----
          
          
          ---- PIN ON ----
          Measuring...
          35
          119
          Init SigFox...
          0;21.2;62.7;1016.0;16;24;28;74;2021-02-17 15:22:44
          ---- PIN OFF ----
          
          
          ---- PIN ON ----
          Measuring...
          35
          119
          Init SigFox...
          0;21.2;62.9;1015.9;28;38;42;116;2021-02-17 15:23:16
          ---- PIN OFF ----
          

          It doesn't work correctly

          L 1 Reply Last reply Reply Quote 0
          • L Offline
            livius @Luca Albano
            last edited by livius

            @Luca-Albano
            Did you tried on different pins then P9 and P10?
            And how this sensor behave if you have only import releated to i2c and all otheres devicess are not connected?
            I meen simple code without sd card, wifi, .....
            I ask because for me it is working without any problems form months

            think about people first, then computers

            L 1 Reply Last reply Reply Quote 0
            • L Offline
              Luca Albano @livius
              last edited by

              @livius Thanks for reply.
              I tried this sensor in single in another shield (Arduino UNO) and there was always this problem.
              I could try with P21 and P22 which I am already using for the PM sensor, but for me this is not an I2C bus problem ...

              1 Reply Last reply Reply Quote 0
              • L Offline
                livius
                last edited by

                If you have tried this sensor with different hardware and still problem occurs then probably some problem with sensor itself.
                I use this sensor long long time and i have not any problems.

                think about people first, then computers

                L 1 Reply Last reply Reply Quote 0
                • L Offline
                  Luca Albano @livius
                  last edited by

                  @livius I probably damaged it: yesterday, for a second, I connected GND and Vcc by reversing them. Since then, the sensor has worked like this. The only way, i think, is trying with another BH1750 to verificate if it is damaged or not, do you think?

                  L 1 Reply Last reply Reply Quote 0
                  • L Offline
                    livius @Luca Albano
                    last edited by

                    @Luca-Albano
                    for me it looks like a fastest way to werify

                    think about people first, then computers

                    1 Reply Last reply Reply Quote 0
                    • rcolisteteR Offline
                      rcolistete
                      last edited by

                      I suggest anoter light sensor, MAX44009, better than BH1750 : low power (about 1 uA), "ultra-wide 22-bit dynamic range from 0.045 lux to 188,000 lux", etc.

                      My "MicroPython Samples". My "MicroPython Firmwares" with many options (double precision, ulab, etc).

                      L 1 Reply Last reply Reply Quote 0
                      • L Offline
                        Luca Albano @rcolistete
                        last edited by

                        @rcolistete Thank you but for this thesis' project i have only this type of sensor.
                        I'll remember it!

                        1 Reply Last reply Reply Quote 0

                        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                        With your input, this post could be even better 💗

                        Register Login
                        • 1 / 1
                        • First post
                          Last post

                        Pycom on Twitter

                        Pycom