10 Replies Latest reply on Oct 20, 2018 7:18 AM by Problemchild

    Optek OPB732WZ  and the Raspberry Pi ?

    amasters

      I am hoping that I can use the  OPB732WZOPB732WZ Reflective Photo Interrupter https://uk.farnell.com/optek-technology/opb732wz/opto-switch-reflective/dp/1678639 with a Raspberry Pi which has both 5v and 3.3v supplies on the GPIO.

      I have no great experience with electronics, but attaching optical sensors to a Raspberry Pi appears to be a simple matter according to articles I have read using similar devices.

       

      Any advice on how to connect this device to the Raspberry Pi would be gratefully received.

       

      Many thanks.

        • Re: Optek OPB732WZ  and the Raspberry Pi ?
          shabaz

          Hi Alan,

           

          You can use the circuit in the datasheet, there are four there, and the first one could be tried, although the third one can be better (and more energy efficient since it can be switched off). The third one needs a transistor, something like BC547 or any other generic NPN transistor is fine.

           

          But, the datasheet circuit is powering everything from one rail called VCC. Better in your case to split it, the receiver portion should be connected to the 3.3V supply (because the GPIO operate at that voltage), and the transmit portion can be connected to the 5V supply (because it will consume more current, and there is some limit on the 3.3V rail). The calculations for the resistors are in the datasheet, e.g. RD = (5V-1.8V)/20mA = 0.16k = 160 ohms, use the next highest easily available value, which is 180 ohms.

          For RL, something like 4.7kohm is fine or that ballpark, it is not very critical.

          If you use circuit#3, then the base of the transistor needs a resistor in series with the connection to the GPIO pin that drives it, although it is not shown. That can be a 1k resistor.

           

          I hope that helps, but if you need more help let me know.

          5 of 5 people found this helpful
            • Re: Optek OPB732WZ  and the Raspberry Pi ?
              amasters

              Thanks Shabaz for taking so much trouble to reply.

               

              I have re-visited the datasheet and with your explanation this seems to be much clearer.

               

              However, I am not clear about the purpose of the additional transistor in diagram 3, is this required to turn the infrared light off?   I am planning to run the Raspberry Pi using the mains adaptor plug and it will probably be in use for a maximum of two hours in any day, after which the Raspberry Pi will be turned off.

               

              My intended application is to record the time a piece of reflective tape passes by on a large wheel which has a rotation time of approximately two seconds, but could be a little faster or slower at various times.

               

              Have I interpreted circuit 2 correctly when I think that if the permanently on infrared LED shines on the reflective strip it will turn on the GPIO input pin. It appears to me that circuit 1 would keep the GPIO pin turned on when there is no light shining on it and turn off when there is ??

                • Re: Optek OPB732WZ  and the Raspberry Pi ?
                  shabaz

                  Hi Alan,

                  You're right, the extra transistor is for switching it off, or for pulsing it, for circuitry which isn't attached to the Pi, but can be used to pick up the signal amongst background light. It isn't used in your scenario if you're directly connecting to the Pi, and are running from the mains.

                  Circuit 1 and 2 are similar, one provides an inverted output and the other doesn't. The software can usually be coded to accept either scenario. However electrically circuit 1 is better for this purpose usually, because the output can swing a bit more, and is more flexible in the way it can be wired for some use-cases, not that it matters for your application. Anyway, best to go with circuit 1 usually, unless there is a reason it cannot be handled in software. But either will work.

                  2 of 2 people found this helpful
                    • Re: Optek OPB732WZ  and the Raspberry Pi ?

                      Thanks again for your help Shabaz. I have more confidence now in proceeding with this project.

                        • Re: Optek OPB732WZ  and the Raspberry Pi ?
                          shabaz

                          Hi Alan,

                           

                          Excellent, good luck with the project! I don't think you'll need this link, but just in case it provides any ideas:

                          Cyclops-1000: An Electronic Eye for Rotational Speed Measurement

                          That link is to a microcontroller project however, not Pi based, but maybe bits of it are helpful.

                          I find myself using it from time to time, to check things like small fan speeds end-on, i.e. directed at a fan blade with a white sticker stuck to it. The commercial tachometer tool I've got is actually less useful at such end-on applications and only really works side-on to rotating shafts/objects)

                           

                          If the time you're aiming to measure is of the order of 2 seconds as you mention, then the Pi should work fine for that to accuracy within a few msec hopefully (higher accuracy becomes harder because Linux could context-switch processes at any time).

                          2 of 2 people found this helpful
                            • Re: Optek OPB732WZ  and the Raspberry Pi ?

                              I have at last got round to build the circuit board, at the moment to accommodate two optical sensors, but while testing for shorts / connectivity I found that there is some form of connection between the two inverted outputs 1 and 2 . Unfortunately my test meter does not measure resistance accurately, so I have ordered one which should.  I am hoping that the connection is through the two resistors RL and the Vcc rail and not a short. I am using Circuit 1.

                               

                              Sorry for the silly question, but am I likely to get interference between the two inverted outputs 1 and 2 when one of them detects a reflection.  I am imagining that because the current is flowing from the Vcc rail to the ground rail when  the sensor detects a reflection that there will not be any interference, but I would like to be sure before connecting up to the Raspberry Pi and potentially causing damage.

                               

                              Many thanks,    Alan.

                               

                               

                              Circuit for two (or more) optical sensors

                                • Re: Optek OPB732WZ  and the Raspberry Pi ?
                                  shabaz

                                  Hi Alan,

                                   

                                  What voltage are you intending to connect Vcc to, is it 3.3V or 5V? Ideally the LED (transmit) portion should be powered from 5V. The phototransistor (receiver) portion must be powered from 3.3V to be compatible with the Pi's inputs, The Pi's 3.3V can't supply much current, and the LEDs consume more power than the receivers, which is why they are preferred to be connected to 5V.

                                  If you're testing unpowered, then the resistance between the two outputs could be 2xRL or less. This path is through the two RL resistors so does not imply at all that there could be electrical or optical interaction when the top rail is set to a voltage. Since the rail is fixed at a voltage in operation, then the voltage at output 1 results from current passing through RL1 only, and the current through RL2 has no impact on it, and vice-versa.

                                   

                                  Before connecting to the Pi, confirm that the resistance between VCC and VEE is higher than a few hundred ohms. If it is very low (like 10 ohms or less) then you have a wiring fault and it must not be connected to the Pi.

                                  If the resistance is higher than a few hundred ohms then connect the VCC and VEE connections to the Pi. (VCC definitely being 3.3V for the receiver portions, and as mentioned 5V for the transmitter portions if possible).

                                   

                                  Next, with the meter set to Volts DC, confirm that output 1 and output 2 never go higher than 3.3V. If they go higher, then there is some wiring fault. If they don't go higher, then you're fine to make those connections too.

                                  1 of 1 people found this helpful
                                    • Re: Optek OPB732WZ  and the Raspberry Pi ?

                                      Thanks again for the comprehensive reply Shabaz.

                                       

                                      Yes, I have taken your advice and connected the LED part to the 5v and the phototransistor to the 3.3v but had not shown this in the diagram - sorry. You will recognise it as the diagram extracted from the fact sheet.

                                       

                                      I am waiting for a delivery of a test meter which is supposed to measure resistance accurately, so will resume testing when I receive it.

                                       

                                      Thanks too for the advice on connecting the 5v 3.3v and Ground only from the Pi to the board. I have some jumper leads which can be used for this. The final build will use a ribbon cable from the GPIO  to an Adafruit Pi T-Cobbler Kit Breakout for R Pi A+/B+ , so connecting that before your recommended test would be unwise.

                                       

                              • Re: Optek OPB732WZ  and the Raspberry Pi ?
                                Problemchild

                                Hi Shabaz, one thing for folk to note is that the output from  the phototransistor may not be as square as shown in the diagram afterall it is an analog device, we are assuming that for any reasonable amount of reflected light the gain of the transistor is such that it's more or less square and can easily be connected to a logic input. That said if you look at Shabaz's Cyclops Article you will see that he uses a part that already has a schmitt trigger inside and a further transistor to clean it all up.

                                 

                                If you are getting intermittent or strange results you may well need to visit this part of the circuit!

                                • Re: Optek OPB732WZ  and the Raspberry Pi ?
                                  Problemchild

                                  Hey Shabaz, the other function of that transistor can be to modulate the output of the diode. That way you can have several emitters in fairly close proximity to each other but modulated at different frequencies or even just plain off when the other units are on this would make it easier to identify signals from each interrupter and also differentiate their signal from external ambient light sources