Author Topic: Any help? Raspberry Pi prod counter w/ reflective sensors - output concerns  (Read 1494 times)

0 Members and 1 Guest are viewing this topic.

Offline MaxBishopTopic starter

  • Contributor
  • Posts: 25
  • Country: us
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.
 

Online wasedadoc

  • Super Contributor
  • ***
  • Posts: 1372
  • Country: gb
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.
« Last Edit: April 13, 2024, 08:01:52 am by wasedadoc »
 

Offline BennoG

  • Regular Contributor
  • *
  • Posts: 70
  • Country: nl
I usually use the schema attached for inputs.
it accept:
NPN  (common +12-30V)
PNP  (common GND 12-30V)
AC   12-30V

Benno
 

Offline BeBuLamar

  • Super Contributor
  • ***
  • Posts: 1202
  • Country: us
I am quite familar with the type of sensors you use but I don't know what kind of input the Pi has.
 

Online xvr

  • Regular Contributor
  • *
  • Posts: 159
  • Country: ie
    • LinkedIn
I am quite familar with the type of sensors you use but I don't know what kind of input the Pi has.
3.3V CMOS logic inputs.

wasedadoc already give perfectly working suggestion (if counter placed physically near Raspbery Pi).
 

Offline BeBuLamar

  • Super Contributor
  • ***
  • Posts: 1202
  • Country: us
the model of sensor in the datasheet only has open collector PNP outputs. Wassadoc recommended NPN output.
 

Offline MaxBishopTopic starter

  • Contributor
  • Posts: 25
  • Country: us
Thank you, everyone, for the input! Apologies, I understand very little of it but I want to learn. This is my starter 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?


 

Online xvr

  • Regular Contributor
  • *
  • Posts: 159
  • Country: ie
    • LinkedIn
> 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:
2118965-0

Sensor supply voltage assumed 10V, output current ~ 24ma (DS says that up to 100mA available)

 

Offline BeBuLamar

  • Super Contributor
  • ***
  • Posts: 1202
  • Country: us
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.
 

Offline MaxBishopTopic starter

  • Contributor
  • Posts: 25
  • Country: us
> 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:
(Attachment Link)

Sensor supply voltage assumed 10V, output current ~ 24ma (DS says that up to 100mA available)


Thank 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.
 

Offline MaxBishopTopic starter

  • Contributor
  • Posts: 25
  • Country: us
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.
Thank 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 Configuration
Bipolar: One current sourcing (PNP) and one current sinking (NPN) open
collector transistor (There's that term again...)
Output Rating
150 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 microamp
Output Saturation Voltage (PNP Output): less than 1 V at 10 mA, less than 2
V at 150 mA
Output Saturation Voltage (NPN Output): less than 200 millivolts at 10 mA,
less than 1 V at 150 mA
 

Offline BeBuLamar

  • Super Contributor
  • ***
  • Posts: 1202
  • Country: us
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.
« Last Edit: April 14, 2024, 11:13:44 am by BeBuLamar »
 

Online xvr

  • Regular Contributor
  • *
  • Posts: 159
  • Country: ie
    • LinkedIn
> 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 together
That's all
 

Online wasedadoc

  • Super Contributor
  • ***
  • Posts: 1372
  • Country: gb
> 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 together
That's all
As per first reply.  :)
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
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/
« Last Edit: April 14, 2024, 06:28:30 pm by rstofer »
 

Offline BeBuLamar

  • Super Contributor
  • ***
  • Posts: 1202
  • Country: us
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.
 

Online wasedadoc

  • Super Contributor
  • ***
  • Posts: 1372
  • Country: gb
Better to post on the dedicated section of the RPi forums. https://forums.raspberrypi.com/viewforum.php?f=37
 

Offline BeBuLamar

  • Super Contributor
  • ***
  • Posts: 1202
  • Country: us
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.
 

Offline MaxBishopTopic starter

  • Contributor
  • Posts: 25
  • Country: us
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.
« Last Edit: April 15, 2024, 07:21:43 am by MaxBishop »
 

Offline BennoG

  • Regular Contributor
  • *
  • Posts: 70
  • Country: nl
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
 

Offline MaxBishopTopic starter

  • Contributor
  • Posts: 25
  • Country: us
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

Thank 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!
 

Offline MaxBishopTopic starter

  • Contributor
  • Posts: 25
  • Country: us
Better to post on the dedicated section of the RPi forums. https://forums.raspberrypi.com/viewforum.php?f=37
Indeed. Point taken. I will post there as well. Thank you.
 

Offline MaxBishopTopic starter

  • Contributor
  • Posts: 25
  • Country: us
Thanks, everyone, for your input. I’ll address a couple of responses that I missed before.
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.
BeBuLamar, 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.

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


Thank 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.
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/

Thank 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 mention slow repeat rate am 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.

 

Offline BeBuLamar

  • Super Contributor
  • ***
  • Posts: 1202
  • Country: us
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.
 
The following users thanked this post: dobsonr741

Offline MaxBishopTopic starter

  • Contributor
  • Posts: 25
  • Country: us
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.
Great 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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf