BH1750 double sensor coding

Hello everybody, I am a total noob at programing in all ways. Its my very first arduino project.
Till now i got help from this forum. But now i have double truble with BH1750 light sensors.

I am trying to conect two BH1750 sensors and get both readings and use this two reading to switch on and off two stepper motors. My goal is to automate a proces of sorting small ceramics tiles.

I will try to explain.

On the left side of the machine there is one stepper motor that is configure into an elevator (1) . So elevator n.1 elevates the ceramic tiles up until they reach a ledge where the top one falls over a sharp edge and slides under an angle into a slit - gauge. This is the control gauge. If the ceramic plate is good it slides further and goes into elevator n.2 which rides oposite to elevator n.1 so it can colect the tiles.

The problem is when a tile is bad and it stops in the control gauge. so when this happens i need the machine / the elevators to stop.

So i have two BH1750 sensors, sensor n1. is positioned after the elevator and before the gauge and sensor n2 is positioned after the gauge so infront of elevator n.2.

I need sensor n.1 to stop the elevators when the tile passed by, and sensor n.2 to start the elevators again.

Because its ceramics only light works.. I get around 300 lux reading froim both sensors when they are in place. When the plate passes by the reading is around 1000 lux.

if someone can help me with the code it would be fantastic.

Thank you!

I use ARDUINO MEGA 2560 board.

my code for now is only for stepper motors, but i need to overwatch the machine all the time.


have a look at adafruit-bh1750-ambient-light-sensor
there is advice on wiring the sensors and sample code
the module has an ADDR address pin which selects the I2C address enabling the use of two sensors

1 Like

What have you tried ? Can you show the code between code tags ?

The BH1750 is a 3.3V sensor (with a 3.3V I2C bus) and the Mega board is a 5V board (with a 5V I2C bus). How did you connect the 3.3V I2C bus to the 5V I2C bus ? (I'm talking about the I2C bus, not the 3.3V power).
You might need a I2C level shifter.

Please do not twirl the wires for SDA and SCL. The I2C bus can not deal with crosstalk between SDA and SCL. keep them away from each other.
When you do that twirling, then the official I2C standard gives you a length of maximum 10 cm. Page 54, paragraph 7.5: https://www.nxp.com/docs/en/user-guide/UM10204.pdf

Can you give a link to your BH1750 module ? I would like to see if there are more components on that module.

1 Like

I used this code with one sensor. It works , i get all the readings. I dont have the knowledge to progress the code into two sensors and use them to switch my steppers on and off.

and this is the buy page of the sensors i am using.

I will take your advice and uncurle the wires thank you! i know it does not help to have them curled like that, but thats what i had at hand to extend the wires..

also, i connected the gnd to gnd and VCC to 5v... as the "tutorials" shows

That module has no level shifters for SDA, SCL or ADDR. You may not apply 5V to the ADDR pin. You need a I2C level shifter for SDA and SCL, and connect ADDR to either GND or 3.3V.

It has a voltage regulator on the module, so you need to power it indeed with 5V, to let the sensor run at 3.3V.

I don't like to be the bearer of bad news, but your module is not really compatible with either 3.3V or 5V boards.

At Adafruit, they have brains and they use them, they are actually thinking about what they make. The Adafruit module is compatible with the Mega board: Adafruit BH1750 Light Sensor - STEMMA QT / Qwiic : ID 4681 : $4.50 : Adafruit Industries, Unique & fun DIY electronics and kits They also specifically say that it is compatible with 3.3V and 5V boards. They have a solder pad to connect ADDR to 3.3V or GND. It is not that hard to do it right.

2 Likes

OK, i will replace the sensors asap.

i connected the ADDR pin to GND ...

Right now i still get the readings from one sensor. And the reading is stable.

I have a problem when it comes to coding... i need to take the values i am getting on the serial monitor .... (app. 300 lux and app. 1000lux) and convert them and use them in the code in na if-else statement.

Thats the biggest problem for me right now... lack of coding knowledge.

(but I am very happy that you help me and put me on the right path! i will get better/compatible components)

You can connect the other ADDR pin to 3.3V and run a I2C Scanner sketch. You should be able to see both.

By the way, in most cases it works if you connect SDA and SCL directly to the sensor. It might work, or not. The newer Bosch sensor are sensitive for that, but most sensor don't care or become less accurate.

We like to see what you have tried. Please show us your sketch between code tags.
Is there a light source that has a constant brightness all the time ?

1 Like
#include <Wire.h>
#include <BH1750.h>

BH1750 lightMeter;

void setup(){

  Serial.begin(9600);

  // Initialize the I2C bus (BH1750 library doesn't do this automatically)
  // On esp8266 devices you can select SCL and SDA pins using Wire.begin(D4, D3);
  Wire.begin();

  lightMeter.begin();
  Serial.println(F("BH1750 Test"));

}

void loop() {

  int lux = lightMeter.readLightLevel();
  Serial.print("Light: ");
  Serial.print(lux);
  Serial.println(" lx");
  delay(1000);

}

i just copied this code in.. I tried to do some modifications to it but i didnt get any improvements. But as i say.. i am totaly new to programing .. i am more of a mechanical type of man

the brigtness in the room is gonna be constant. the light reflect of the black stone app. 300 lux, and of the white tiles app 1000 lux .. so its easy for the sensor to detect the difference

as for the stepper i used this code... i just adjusted the speeds .. and made two programs.. one for up and one for "reset"

#define stepPin1 3
#define endstopPin 7
#define directionPin2 4
#define stepPin2 5
#define stepsPerRevolution 1600

void setup() {
  // put your setup code here, to run once:

pinMode(directionPin1, OUTPUT);
pinMode(stepPin1, OUTPUT);
pinMode(directionPin2, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(endstopPin, INPUT);

}

void loop() { 

  for (int i = 0; i < stepsPerRevolution; i++)

  // put your main code here, to run repeatedly:
 
if (digitalRead(endstopPin) == HIGH) 
    {digitalWrite(directionPin1, HIGH);
    digitalWrite(stepPin1, LOW);
    delayMicroseconds(800);
    } 
    if (digitalRead(endstopPin) == LOW) 
    
    {digitalWrite(directionPin1, LOW);
    digitalWrite(stepPin1, HIGH);
    delayMicroseconds(50);
    } 
     digitalWrite(directionPin1, LOW);
digitalWrite(stepPin1, LOW);
delayMicroseconds (50);
digitalWrite (stepPin1, HIGH);
delayMicroseconds (3000);

if (digitalRead(endstopPin) == HIGH) 
    {digitalWrite(directionPin2, LOW);
    digitalWrite(stepPin2, LOW);
    delayMicroseconds(800);
    } 
    if (digitalRead(endstopPin) == LOW) 
    
    {digitalWrite(directionPin2, HIGH);
    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(50);
    } 
     digitalWrite(directionPin2, HIGH);
digitalWrite(stepPin2, LOW);
delayMicroseconds (50);
digitalWrite (stepPin2, HIGH);
delayMicroseconds (3000);
}


vpiši ali prilepi kodo

What needs to be improved ?

I think that you need a good description of the sequence, the timings, the number of steps, everything.

Can you make the text layout of the source code look better ? Every indent, every space, every comma, every bracket, every new line, everything needs to be in the right place.

I think that you need someone to write the code for you, or someone that can come over to your place to help.
You started with a project, but it would be better if you start to learn about Arduino. Such a blinking a led, then sending text to the Serial Monitor, then use the Stepper Motor, and so on.

yes, i know i am trash at coding.

i will learn in the future, for now i need someone to help me (time is a factor.. i have like a month to finish this)

the code i sent wasnt the last stepper motor code, i did much better at the end.
the sensor code i only copied from forum

thank you for your contribution, i will implement it and learn new stuff, then i will come back with my new code

=)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.