Will this work correctly?

Hi all,

I’ve got some (12) photosensors 5V DC 3-wire NPN NO witch i would like to connect to my project.

I think I bought them wrong, because when they are interupted, i got 0,26v between the blue GND line and the black OUT line, BUT i got 5volts between the brown 5Volt VDD line and the black OUT line. So when i connect theblack OUT line to the MEGA 2560 there is no “HIGH” because thats a value of -5 volts at that moment.

I hope to work around this problem with a dual channel HCPL-2531 opto-coupler (see attachment below)

i’m no master in electronics, so could anyone help me with some tips to get a +5volt high out of these photoswitches with the opto-coupler.

Thanks in advance guys,

Yves

sensorboard.jpg

First point: Resistors have a purpose. Please try to begin to understand why they exist and why they get used in designs. And when doing a drawing... if you used them, don't omit them.

If your optocoupler still works (the LEDS are not fried) they may be hope in getting this to work.

Problem #1:

The photosensor (based on my understanding of them) is basically an an open collector NPN output. It can only pull to GND or be open-circuit. So, you correctly have the collector from the sensor going to cathode pf the internal LED. What is missing is a 220 Ohm resistor between the anode side 5V supply.

Problem #2:

The sensor is open collector (meaning very versatile) and this means that I fail to see why you needed an optocoupler there at all. If you have placed a 10K resistor from the sensor NPN output to +5v you would have a TTL Logic (and therefore arduino pin) compatible level change... You would see logic state changes correctly

Problem #3:

If you want to use an opto-coupler, they usually have open collector npn outputs. (just like your sensor) The same solution applies... Tie the collector (output) pin to 5V through a 10K pullup resistor... then attach the collector to a digital pin for state sensing. When the internal LED is not lit, the mega will see +5V or logic "1" , when the internal LED is lit, the mega will see a logic "0".

So, please explain why you have an opto coupler here because I feel some important information has been skipped.

Hi Pwillard,

so many thanks for your explanation !!!

i was i bit in "panic" because of the cost of those sensors, and as i sayed i'm no electronics engineer.

so if i understand your explanation well, i really don't need the opto-couplers !! yeah that's great!

is there a way to find a schematic of what you did explain to me.

you lost me halfway your text, at the point of the "open collector" =(

of if i read your text correctly, i can work it out with some resistors to get the "HIGH" from the output line of the photosensors?

Grtz,
Yves

pwillard:
If you want to use an opto-coupler, they usually have open collector npn outputs. (just like your sensor) The same solution applies... Tie the collector (output) pin to 5V through a 10K pullup resistor... then attach the collector to a digital pin for state sensing. When the internal LED is not lit, the mega will see +5V or logic "1" , when the internal LED is lit, the mega will see a logic "0".

Actually, it turns out that he has connected the transistors in the optocoupler to 5V on the Mega2560.

He should have connected the emitters to ground, the collectors to the input pins, and enabled the internal pull-ups on those input pins. External pull-ups are quite unnecessary.

Indeed, the sensors can be used without the optocouplers, again using the internal pull-ups and no extra resistors.

Details are difficult to see as the diagram is not quite big enough (a change from absurdly big diagrams) and somewhat blurry to boot.

{And while optocouplers usually contain NPN transistors, it would of course make no difference if they were to use PNPs!}

Hi Paul_B,

thanks,

but when i measure between OUT and VDD (with com of the multimeter on the OUT line) I get 5volt, is that correct then?

on the MEGA 2560 i have mounted a protoboard on whitch i have a pull down resistor before goin to the PIN32, that one should leave then?

i did this because i also use a lot of switches and buttons in the project, and i did this to have a clear “LOW” and “HIGH”

my idea was that a photoswitch is in fact a simple “button” so i did the same for the PIN32, and putted a pull-down…

that was not a good idea then :disappointed_relieved:

i have made a new drawing (attached), so with my drawing i could go directly to the PIN32 then?
or do i miss that one out again?

i also have attached the shematic of the seller’s photocell

Grtz,
Yves

Example:

I am going to ignore the matter of measurements with a meter.

YvesD:
on the MEGA 2560 I have mounted a protoboard on which I have a pull down resistor before going to the PIN32, that one should leave then?
I did this because I also use a lot of switches and buttons in the project, and I did this to have a clear “LOW” and “HIGH”.
my idea was that a photoswitch is in fact a simple “button” so i did the same for the PIN32, and putted a pull-down…

You should not be using any pull-downs. It is bad engineering practice. It is most unfortunate that the Arduino tutorials depict switches going to Vcc and pull-downs as this causes immense confusion. All (single-throw) switches should be connected to ground and use the internal pull-ups in the Arduino, becoming essentially equivalent to “open collector” outputs. In some particular cases it may be necessary to use a “stiffer” external pull-up resistance less than 5k.

YvesD:
my idea was that a photoswitch is in fact a simple “button” so I did the same for the PIN32, and put a pull-down…
I have made a new drawing (attached), so with my drawing I could go directly to the PIN32 then?

The drawing now appears correct.

YvesD:
I also have attached the schematic of the seller’s photocell

Actually, it would always be far more helpful to post a link to the manufacturer’s website or datasheet, making it much easier for us to determine any special requirements or capabilities of the sensor.

Hi Paul,

I am going to ignore the matter of measurements with a meter.

:slight_smile: :slight_smile:

I will try this ASAP, and will post my findings back here, they can always be of use for anyone else later on.

Thanks in advance,

Grtz,
Yves

Hi Paul,

nope, did as you have done on you drawing… =(

the photocells are on a separate 5volt supply, because of the power they consume.

so i followed your drawing,

R10K between 5Volt of the arduino and the incomming line from the photocell
on that crossing i went to the Pin30 on the board.

then i made some quick testing code:

int FotocelA = 30;
void setup() 
{
  pinMode(FotocelA, INPUT);
.....
}
void loop() 
{
if (digitalRead(FotocelA) == HIGH)
{
clear_groen(); // clear ledstrip
rood();               // color ledstrip RED
}
if (digitalRead(FotocelA) == LOW)
{
clear_rood(); // clear ledstrip
groen();           // color ledstrip GREEN
}
} // end of loop

if i break the lightbeam, the ledstrip stays GREEN !?
if i let the lightbeam open, the ledstrip stays also GREEN !?

I really don’t know what i’ve done wrong now :blush:

Thanks in advance,
Grtz,
Yves

edit : att. added

NPN NO intern schema.jpg

I gather that you are in fact, not using pullup resistors at this point.

  pinMode(FotocelA, INPUT);
  digitalWrite(FotocelA, HIGH);  // Switches on internal pullup.

Can't speak for the rest of your code that you have not supplied.

What is that diagram supposed to be? It is certainly not in any remote way correct.

HI All,
The last time I used a photo sensor (reflective) I connected the trany collector to Vcc and had a 10K resistor in the emitter circuit to Gnd, taking the output from the emitter! always seems to work.

The transistor acts has a switch and changes the output from about 0v to almost VCC.

Regards

Mel.