BH1750 library hangs
-
Hello everyone!
I have been am trying to make a wireless light sensor to connect to my home assistant. As of now I have been successfully using one connected directly to the pi but I want to be able to eventually use different ones and spread them around the house so I decided to go with MySensors for that.
I have an Arduino mini pro connected to an NRF24L01+ wireless radio that uses my Pi as the gateway. I have successfully established a connection and I am able to send messages to the Pi and see the results in home assistant.
What I don't seem to be able to do is get information from the light sensor. I am using a BH1750 light sensor for that.I have two versions, one is the GY-302 (small one) and the other GY-30 (longer one). They both work well when connected directly to the pi. I can get light levels both through a python script and directly to home assistant.
When I connect them to my arduino though the BH1750 library doesn't work.- With the GY-302 it just crashes on calling
begin
. I have dug under the library and realized it hangs on the callendTransmission
. - With the GY-30 it does not hang but instead it always reports the same value of lux, no matter how much light I put on it or how much light I remove from it. The value is always 54612.
I am a software engineer by profession so my circuit skills are extremely low and it is likely I am doing something wrong.
At first I thought that, since I am a newbie, I fried something while soldering so I soldered a new mini pro (same manufacturer) but I got the same results.I was mostly following this guide online: http://sebastiaanschimmel.me/blog/2015/06/arduino-temp-hum-light-sensor.html so my circuit looks like the one described (without the temperature sensor). I can send pictures of it if required.
The boards I got are the following:
https://www.aliexpress.com/item/WAVGAT-Atmega328-3-3V-Version-Pro-Mini-Module-16M-For-Arduino-Compatible/32820596876.html?spm=a2g0s.9042311.0.0.rYlqk6
https://www.aliexpress.com/item/1Pcs-New-Arrival-BH1750FVI-GY-302-GY302-Digital-Light-intensity-Sensor-BH1750-16bitAD-Module-For-AVR/32821717980.html?spm=a2g0s.9042311.0.0.rYlqk6
https://www.aliexpress.com/item/1pc-Basic-Breakout-Board-For-FTDI-FT232RL-USB-to-Serial-IC-For-Hot-Top-Sale/32648158894.html?spm=a2g0s.9042311.0.0.2Wt045Hope someone is able to help!
Thanks and sorry for the long post!!
- With the GY-302 it just crashes on calling
-
@joão-gouveia Did you try to run it with different arduino / radio? Just to eliminate hardware issue. My ESP8266 is working fine with my GY-302 module.
-
@alexsh1 said in BH1750 library hangs:
ESP8266
You mean a different model? Unfortunately I don't have another model to test it, only mini pro. I have tried another mini pro (same manufacturer though). I also don't have another model of the radio. But the messages are being sent properly so I am not sure if the problem could come from that.
-
@joão-gouveia I do not mean different model, I mean GY-302. I have the same setup with atmega328p and BH1750 running fine as well. Lib is not a problem - this is what I am trying to say.
Show your wiring please. How do you connect the module?
-
I have tried with both a GY-302 and a GY-30 and both don't work although they show different problems (mentioned on my OP). Both the sensors work well when connected to the pi.
I don't have a drawing of the circuit but I can show you a picture, if it helps.
The diagram is essential like this one but without the temperature sensor and I am currently using an FTDI board to provide the power, instead of a battery.
The SCL is connected to A5 and the SDA to A4 of the mini pro.
I can also do a drawing, if further info is required but, as I said, it is like the one I linked above.
-
@joão-gouveia Do you have any other sensors you could try with Pro mini?
-
Not at the moment but I ordered a DHT22 sensor I can try that when it arrives.
-
@joão-gouveia hard to tell. I spent days trying to troubleshoot the connectivity problem and it turned out to be a bad nrf24l01+. Just to make sure - you have no RF issues? Pro Mini is talking to RPi no problem?
Another possible issue is I2C - this is why it is freezing on Wire.begin(); Double check wiring just in case. (SDA and SDL correctly connected?). You can also use an example sketch to scan i2c bus. You may need to update the lib with the correct address.
If both sensors are working with RPi, this eliminates them.
-
@alexsh1 There doesn't seem to be no RF issues, no. The freezing occurs on Wire.endTransmition(), not begin. I meant the begin function of the BH1750 library from MySensors, sorry if that wasn't very clear.
SDA and SDL seem to be connected correcly. I've checked that a few times now.
-
@joão-gouveia OK, if i2c wiring is fine, load up i2c scanner and see if you can read the sensor address.
-
@alexsh1 I ran this example: https://playground.arduino.cc/Main/I2cScanner (I'm not sure if this is what you meant).
It seems to hang on Wire.endTransmition() it prints the string "Scanning..." but nothing else.
-
@joão-gouveia Yes, that's what I meant. OK, here we go - this is an i2c problem. I am shooting a bit in the dark here - how about trying to change your power source? Maybe a different 3.3V FTDI adapter.
-
@alexsh1 Hm that seems reasonable. The FTDI board I have seems to be a bit funky it gives a lot of errors when I am trying to upload sometimes. Could that be related? I am not sure if it is the FTDI or the mini pro itself though.
I don't have another type with me though.
-
@joão-gouveia A good a reliable power source is a key unless you what to spend hours troubleshooting. Yes, FTDI could be the reason when used as a power source.
Another thing to try is to load an example sketch for BH1750, disconnect the FTDI adapter and use 2xAA batteries to power up your schematic. Then you can check your controller for a log. Unfortunately, you cannot check the log on the node.
-
@alexsh1 I think I will try to get a more reliable FTDI since this one seems to give me some troubles. It also isn't properly detected on my PC sometimes. Is there a reliable one I could get from, for example, aliexpress, you recommend? My knowledge on these is close to zero, hahaha. Thanks.
-
@joão-gouveia I would highly recommend Adafruit FTDI :
https://www.adafruit.com/product/284
Expensive, yes, but I modified with two switches - 3.3V-5V power and 3.3v-5V logic level.
Alternatively, you can try a few cheap Aliexpress ones like this one (make sure it is a genuine FTDI chip):
-
@alexsh1 Thanks I will check it out and give it a try! I'll let you know the results
-
@alexsh1 I just realized my full blown stupidity. The breadboard I am using has the buses connected half way and I was convinced they were fully connected. So the light sensor was not getting power, obviously.
It works now.
-
@joão-gouveia The problem is very often much simpler than we think
-
@alexsh1 Very true Anyways, thanks for the tips!