Any help? Raspberry Pi prod counter w/ reflective sensors - output concerns - Page 1
Forum section: electronics / beginners

author: MaxBishop
author_rank: Contributor
timestamp: April 13, 2024, 07:31:34 am
content: Hello. I'm building a simple manufacturing production counter using a Raspberry Pi 3 and two diffuse reflective sensors powered by a 24v power supply. I am concerned with the detection signal coming from the sensors. I don't know how to measure the output or read the data sheet. I've read about other RPi projects with photoelectric sensors and I'm concerned I may need a step up or down resistor depending on whether the sensor is used as PNP or NPN. I've also read instances where an opto-isolator was used further adding to my uncertainty. I understand the basic concept of not frying my RPi but I'm not confident I understand it well enough to risk it.Are my concerns valid? Are there other, more important issues?The sensors I am using are SICK: WTV43P2241. I've attached the data sheet.If you can offer any assistance, I would greatly appreciate your feedback.

author: wasedadoc
author_rank: Super Contributor
timestamp: April 13, 2024, 07:54:50 am
content: Safest and easiest is NPN open collector output. Connect to GPIO. Add 4k7 pullup resistor between GPIO and 3.3 Volt pin of RPi.That is the equivalent of a switch with one side connected to 0 Volts (ground) that you can see in the many "use a switch input on a RPi" tutorials on the internet.

author: BennoG
author_rank: Frequent Contributor
timestamp: April 13, 2024, 08:23:06 am
content: I usually use the schema attached for inputs.it accept:NPN (common +12-30V)PNP (common GND 12-30V)AC 12-30VBenno

author: BeBuLamar
author_rank: Super Contributor
timestamp: April 13, 2024, 10:52:47 am
content: I am quite familar with the type of sensors you use but I don't know what kind of input the Pi has.

author: xvr
author_rank: Frequent Contributor
timestamp: April 13, 2024, 12:13:59 pm
content: Quote from: BeBuLamar on April 13, 2024, 10:52:47 am3.3V CMOS logic inputs.wasedadoc already give perfectly working suggestion (if counter placed physically near Raspbery Pi).

author: BeBuLamar
author_rank: Super Contributor
timestamp: April 13, 2024, 12:31:58 pm
content: the model of sensor in the datasheet only has open collector PNP outputs. Wassadoc recommended NPN output.

author: MaxBishop
author_rank: Contributor
timestamp: April 13, 2024, 08:05:12 pm
content: Thank you, everyone, for the input! Apologies, I understand very little of it but I want to learn. This is mystarter project, I suppose. It's a 4 wire sensor. It was explained to me that after connecting to power, if I use one of the remaining two wires as the signal it would be PNP, if I use the other it would be NPN. Have I been misinformed?I don't understand the use of the term, "open collector output" but I Googled it and there are many articles including this one:https://www.instructables.com/Raspberry-Pi-GPIO-Circuits-Controlling-High-Powere/I will read through more of the articles to try to cut down on my questions.I've also done some reading on pull up and pull down resistors but currently I'm still a bit unclear. Pull up is the more common of the two but I'm not sure if the idea is to bring the voltage up or limit it to a known value.I'll read some more but if I understand what I've read to date... 4k7 is standard for 4.7 ohms. If that is correct it seems we are attempting to keep the voltage below the 5v max for the RPi. Am I in the ballpark?

author: xvr
author_rank: Frequent Contributor
timestamp: April 13, 2024, 08:41:44 pm
content: > Have I been misinformed?Yes, datasheet says 'PNP' only.> 4k7 is standard for 4.7 ohms.No, it's 4.7 KOhm> If that is correct it seems we are attempting to keep the voltage below the 5v max for the RPi.RPi will burn out from 5V. 3.3V is an maximum.First of all - you need 10-30V for power up sensor. RPi use 5V. So you need 2 power sources or 10-30V PSU and DC/DC convertor to get 5V for RPi from 10-30V.Next you need resistor divider to bring 10-30V to RPi input range (let it be 3.0V, for RPi safety)Schema:Sensor supply voltage assumed 10V, output current ~ 24ma (DS says that up to 100mA available)

author: BeBuLamar
author_rank: Super Contributor
timestamp: April 13, 2024, 09:23:00 pm
content: Both pin 2 and 4 are outputs and they are both PNP. Pin 4 output when the sensor senses something and pin 2 outputs when the sensor senses nothing.

author: MaxBishop
author_rank: Contributor
timestamp: April 14, 2024, 05:06:23 am
content: Quote from: xvr on April 13, 2024, 08:41:44 pmThank you for clarifying! I have an Allen Bradley power supply for the sensors. I have them powered up and they appear to be working as expected. I just don't have them connected to the RPi yet because I'm worried about the outputs.

author: MaxBishop
author_rank: Contributor
timestamp: April 14, 2024, 05:18:30 am
content: Quote from: BeBuLamar on April 13, 2024, 09:23:00 pmThank you. I have a different sensor that is not powered up yet. Either I misunderstood or I may have mixed my sensor information. I pasted some of the data sheet below and attached the data sheet.Output ConfigurationBipolar: One current sourcing (PNP) and one current sinking (NPN) opencollector transistor(There's that term again...)Output Rating150 mA maximum each output at 25 °C, derated to 100 mA at 70 °C (derate≈ 1 mA per °C)OFF State Leakage Current: less than 1 microampOutput Saturation Voltage (PNP Output): less than 1 V at 10 mA, less than 2V at 150 mAOutput Saturation Voltage (NPN Output): less than 200 millivolts at 10 mA,less than 1 V at 150 mA

author: BeBuLamar
author_rank: Super Contributor
timestamp: April 14, 2024, 10:01:14 am
content: The Banner sensor indeed has 1 PNP and 1 NPN output. Both outputs are on/off at the same time. Open collector means that the collector of the output transistor is the output and is connected to nothing internal to the sensor.I can see why the NPN is better because there is no voltage from the sensor between the output and the common. The voltage would come from the PI. For the PNP output it would put whatever voltage you use to power the sensor to the output and even at the lowest 10V it's still too high for the PI.If you don't need fast response and or having the output activates too often you can use an interposing relay so that it doesn't matter whether the output is PNP or NPN.

author: xvr
author_rank: Frequent Contributor
timestamp: April 14, 2024, 11:13:59 am
content: > I have a different sensor that is not powered up yet.This one could be connected in a much simpler manner - just add 1 resistor.NPN output should be connected directly to RPi GPIO pin. Resistor (300-500 Ohm) should be connected between join of NPN output & RPi GPIO pin and +3.3V from RPi.Grounds of RPi and sensor also should be connected togetherThat's all

author: wasedadoc
author_rank: Super Contributor
timestamp: April 14, 2024, 12:36:39 pm
content: Quote from: xvr on April 14, 2024, 11:13:59 amAs per first reply.

author: rstofer
author_rank: Super Contributor
timestamp: April 14, 2024, 05:52:55 pm
content: NPN output, 4.7K resistor from output to +3.3V (get it from the Pi) and things will probably work out fine. Just make sure the sensor power supply ground is connected to the Pi logic ground.Looking at the sensor datasheet, I'm now sure how you are going to use this sensor. You could use 10V and connect the 'Load' outputs to an opto-isolator. You will need to calculate a series resistor based on the opto LED current and the sensor voltage. The output will be a totally isolated open collector transistor (NPN is likely). Then pull the GPIO pin to ground with a 4.7k resistor to +3.3V from Pi.Where things are going to come off the rails is writing the code for the Pi. If your signal has a slow rep rate, an application program might be written to detect a changing edge and do some kind of counting. But, under Linux, you can't count on finding every edge but you can count on missing them.Your signal would ideally generate an interrupt on the falling edge forcing the code to immediately branch to an interrupt handler to do the counting. I don't know how to write interrupt handlers for Linux. I DO know how to do that kind of thing for bare metal. If I was going to output the count directly with no other outputs to the world (like Ethernet) then any other processor could do the job. Easiest might be an Arduino with interrupt driven code. My favorite would be the mbed LPC1768 with the Teensy 4.1 a close second and a much faster device. Both can handle concurrent networking.Before you lock yourself in to the Pi, make sure you know how to generate an interrupt on the falling (or rising) edge of the input signal and capture the change with interrupt handler code.If your rep rate is low, the Pi code can just loop waiting for a change. I don't know how low.Maybe this will help:https://roboticsbackend.com/raspberry-pi-gpio-interrupts-tutorial/

author: BeBuLamar
author_rank: Super Contributor
timestamp: April 14, 2024, 06:15:38 pm
content: As I said I am not familiar with the PI although I have a one of them nor that I know how to program under Linux. However, I used a lot of those type of sensor and especially the Banner. Although you could get response time to 1 millisecond I would not use them for anything higher than 100Hz.

author: wasedadoc
author_rank: Super Contributor
timestamp: April 14, 2024, 06:20:19 pm
content: Better to post on the dedicated section of the RPi forums.https://forums.raspberrypi.com/viewforum.php?f=37

author: BeBuLamar
author_rank: Super Contributor
timestamp: April 14, 2024, 06:41:25 pm
content: I guess it has to do with what the counter is for. Where the count will be used and what it is counting. But however I guess the OP knows how to do all that and that's why he does it that way he only asked us for the connection between the sensor to the RPi.

author: MaxBishop
author_rank: Contributor
timestamp: April 15, 2024, 07:09:58 am
content: BeBuLamar, xvr, wasedadoc, rstofer, thank you all for the replies in the last 24 hrs. I had a busy weekend and there is a lot here for me to chew through! I need to spend some time on each response to understand better. Seems my project isn't so simple after all. I will add that in regard to response time these sensors will have to detect items up to 1,600 per minute. That said, I know they are capable as this is standard equipment on the machines at work. Making it work with the RPi is the trick.Edit: We use a number of sensors. I’ll check to be sure these will operate as fast as I stated.

author: BennoG
author_rank: Frequent Contributor
timestamp: April 15, 2024, 08:16:22 am
content: That will be 50 edges per second, for the PI this is no problem.If you use my schema leave out the capacitor, then it can go up to several thousands of edges per second. (not that the PI can handle this in standard mode)Benno

author: MaxBishop
author_rank: Contributor
timestamp: April 15, 2024, 06:19:39 pm
content: Quote from: BennoG on April 15, 2024, 08:16:22 amThank you, BennoG! I hope to sit down and go through all of this tonight. It is kids stuff for many of you but like a new language to me. Thanks for trying to help!

author: MaxBishop
author_rank: Contributor
timestamp: April 15, 2024, 06:23:29 pm
content: Quote from: wasedadoc on April 14, 2024, 06:20:19 pmIndeed. Point taken. I will post there as well. Thank you.

author: MaxBishop
author_rank: Contributor
timestamp: April 18, 2024, 06:13:01 am
content: Thanks, everyone, for your input. I’ll address a couple of responses that I missed before.Quote from: BeBuLamar on April 14, 2024, 06:41:25 pmBeBuLamar, Apologies for missing this… My project is embarrassingly simple. I want to make a production machine surveillance system. The first step will be to use the sensors I’ve shared here previously to count production pieces. I want to count input with one and output with the other. Then, with some basic programming I can determine the % waste (raw material). If that all goes well, I’ll add another sensor at a different location in the process for greater insight. With two sensors in place, it will tell us a lot about our process. Adding the third would be a bonus!You are correct, my biggest struggle is getting the sensors hooked to the RPi and working properly.Quote from: BennoG on April 15, 2024, 08:16:22 amThank you, BennoG. I read somewhere that the Pi can handle the number of units we are producing and I've seen these sensors work in a different type of system. I'm just not knowledgeable enough to put it all together and make it work. I appreciate all the feedback! My next hurdle is learning what your schematic is telling me to do. I don't know how to read it. I think it's telling me that brown and blue are power and ground coming from the power supply. From there, I'm struggling. I Googled some things but I don't understand what it's telling me yet. I'm getting closer. I'll write back when I think I have it.Quote from: rstofer on April 14, 2024, 05:52:55 pmThank you for your input, rstofer. There is quite a lot here that I don't understand. The list is growing rather than shrinking. A couple of terms you shared have come up before but I need to do more reading on them such as, opto-isolator and interrupt handler. Also, when you mentionslow repeat rateam I wrong to assume you are referring to the number of units the sensors will count, such as per minute or per second? I'll be counting up to 1600 per minute. To me that seems fast but I don't have a reference.

author: BeBuLamar
author_rank: Super Contributor
timestamp: April 18, 2024, 10:46:25 am
content: Since you are using it in an industrial environment why don't you use a PLC for the job instead of the Pi. You can buy a PLC for a bit more than $100 (with today inflation) and it has the input just right for the sensor.

author: MaxBishop
author_rank: Contributor
timestamp: April 19, 2024, 04:57:56 am
content: Quote from: BeBuLamar on April 18, 2024, 10:46:25 amGreat question! I've wondered if that is the route I should take as well. I have a few reasons although I'm not against the idea. Mainly, I have a few Pi's lying around and I want to build something robust with one of them. Also, I'm getting more comfortable programming in Python which is widely used with RPi but isn't typically listed as a good PLC language.

author: xvr
author_rank: Frequent Contributor
timestamp: April 19, 2024, 07:59:27 am
content: PLC is a much more robust than RPi. It intended to be used in Industrial environment, RPi is not .

author: BeBuLamar
author_rank: Super Contributor
timestamp: April 19, 2024, 10:05:06 am
content: With a PLC it's simple to have the PLC record 2 count values and store it in the PLC however to read these values you need to use the software that you use to program the PLC to read them or you have to use an HMI software or a hardware HMI which is more costly and and involved. However, I think you have all the parts and tools needed to use the PLC for counting I think it's a good idea to just do that first and see if the count values help you then develop something else.

author: MaxBishop
author_rank: Contributor
timestamp: April 23, 2024, 03:54:43 am
content: I'm going to stick with the RPi for now. The PLC's are pricey and I'm not ready to take on a new language. Thank you for the input, though.

author: BeBuLamar
author_rank: Super Contributor
timestamp: April 23, 2024, 11:46:04 am
content: And I under the impression that you have all the PLC parts needed. You have Sick and Banner sensors. You have Allen Bradley power supply. So I guess you have an Allen Bradley PLC laying around some where.