Light barrier NPN NO12V floating pins?

Hello guys I have a problem, i have 2 light barriers (E3F-20C1)NPN NO 6-36V.
The code is turning on LED lamp if 2 sensors have cut their lasers with some object,
Its kinda woking becouse when i give a hand between them the LED i turning on but is flickering. I have 10k pull ups resistors and I connected mass of arduino with the mass of sensors.
what can i do to stop LED from flickering??
Here is my code:

const int sensor1 = 2;  
const int sensor2 = 3;  
const int lampLED = 6;  

void setup() {
    pinMode(sensor1, INPUT_PULLUP);
    pinMode(sensor2, INPUT_PULLUP);
    pinMode(lampLED, OUTPUT);
}

void loop() {
    int state1 = digitalRead(sensor1); 
    int state2 = digitalRead(sensor2);

    if (state1 == HIGH || state2 == HIGH) {
        digitalWrite(lampLED, LOW); 
    } else {
        digitalWrite(lampLED, HIGH);  
    }
}

I suspect it's because you continuously update the LED with digitalWrite (remember the loop runs many thousands times per second!).
Do it only when the state changes (use a pair of static variables to store the statuses detected on the previous cycle), introduce a minimal pause between checks (e.g. a "delay(100);" could be enough for 10 checks per second, or even less, just because your current code does nothing else), see what happens and let us know.

PS: if you activate internal pullups with INPUT_PULLUP, never ever add external resistors! Use either internal OR external.

Draw a sketch showing how everything in’s wired.
Show power supplies, signal names etc.

Clear photo of a clear pen drawing is fine.

1 Like

i added delay(100) but now led is not working at all, I tried wirh 10 delay then it was still flickering, in few minutes ill add sketch

It looks like the input is somehow floating, so it could depend on either the double pullup (two 10k resistors in parallel gives 5k Ohms, not bad, but it depends on the sensor characteristics an cable length) or the sensors electrival requirements (in term of power and signal), or both or who knows...
Post a link to the exact sensors you have bought, to let us see the specs, together with your sketch code (where I'd expect seeing you added the "state change" condition before using digitalWrite).

And I'm sorry to say the diagram you posted now is almost unreadable, please draw it with a circuits tool or emulator (like WokWi or TinkerCAD) and post it again making sure you haven't overlapping wires.

the link: Fotokomórka laserowa E3F-20C1/20L nadajnik+odbiornik | CZUJNIKI \ Czujniki i sondy pomiarowe - Smart Planeta
Its in polish so
its the kpl. E3F1-20L + E3F1-20C1 models

sorry about the sketch im new in this, ill try to better it

What are supply specs of the lasers and the LED ? (V, mA, and possibly W)

No +V supply to the arduino?
Grounds aren’t tied together ?

Generally speaking, don’t think of the Arduino as a power supply - only if the total load is minimal,

laser: 6-36V, 300mA
LED: 2,3V, 20mA
sorry there is supply 4,5V to arduino, and grounds are tied together

A bit of google Translator will come in hand for me.
Anyway, is the sensor a NO (normally open) or NC (normally closed) version?
And to try to understand, use just one sensor connected to pin 2 without any external resistor, and load this simple sketch:

const int sensor = 2;  

void setup() {
    pinMode(sensor, INPUT_PULLUP);
}

void loop() {
  int state = digitalRead(sensor); 
  if (state == HIGH)
    Serial.println(1);
  else
    Serial.println(0);
  delay(100);
}

Then run the IDE Serial Plotter and see the values in output. First let the laser without any obstacle and see if it reports a stable value (as it'd be HIGH you should read 1 many times per second, with a flat diagram). If not, there's something wrong and we need to better investigate if the sensor has a bad connection or cable, or it's defective, or whatever else.
Then, put a hand to block the beam and you should see al 0's, and no 1's with the same principles described before.

You can’t power those from the Arduino 5V pin.

Connected how?

Connected to 12V or 5V

eh i tried doing that but there is no signal on serial plotter i dont know why maybe its something with pc port, I'll try it later this day bc I have to go for 2h

5v but now i use only pull ups internal

its powered by 12V

I found this for the E3F

Seems like the load voltage minimum is 6V, so it may not work for 5V

Where have you connected the 4.5V supply?

Do you have a DMM? Digital MultiMeter?

Tom.... :smiley: :+1: :coffee: :australia:

Which Arduino are you using?

1 Like

What about a readable and effective connections diagram? What about how you're powering the lasers? What about the code? What about the exact Arduino model?

No answers to those points we're asking you -> no answers you'll get.

And please stop writing multiple posts containing just a few words each, it's annoying and makes this topic unnecessarily long.

2 Likes