Can't get a reading from an industrial sensor

I’m trying to get reading from an industrial photoelectric sensor CX-421-P.

It is connected to a 12V source and using a meter I indeed find it distributes 12V on the black wire when it senses an object. In order to not fry my brand new Uno, I used 7805CT regulator to get voltage down to 5V. A meter shows it only gives 5V out of the pin. There is a relay module which I want to change position according to the sensor. It works properly using simple coding of On/Off.

I tried the next code (and some other variations) but all I got is rapid switching unrelated to the sensor itself. Am I miswiring anyhing or is there some component I should add? (adding a capacitor between 5V and GND didn’t help)

Please see image file for the circuit (or at least as close as I could get to it).

Thank you!!

#define RELAY1  10    //Define relay pin #
#define SENSOR A0     //Define sensor pin #
int SensorValue = 1;  //Initilize var
void setup()
// Initialise the Arduino data pins for OUTPUT
  pinMode(RELAY1, OUTPUT);   
  pinMode(SENSOR, INPUT);
  //digitalWrite(SENSOR, HIGH);

void loop()
  SensorValue = analogRead(SENSOR);
  if(SensorValue < 900)
    digitalWrite(RELAY1, HIGH);
    digitalWrite(RELAY1, LOW);
 // delay(1000);

Why not use a voltage divider to drop the 12V from the sensor to 5V for the Arduino? voltage divider_2.jpg

Your circuit is crap.
I don't see a sensor or a relay.

This seems to be a digital sensor.
If it is a PNP-type, use the diagram from groundfungus.
Connect a digital sensor to a digital pin.

There are couple of issues (for me at least) that doesn't help me help you

  1. The circuit (or breadboard layout) is different from what your code and even the problem description is trying to address! you say 12v supply yet you show 6v (4x1.5v batteries). You've plugged an LED yet want to switch an Relay. If it's all representative at this stage for your testing, then perhaps make that clear in your write up.

  2. I am left to guess why you are using the TIP120! Perhaps you want to use it because of it's inbuilt diode protection on the Relay Coil... Obviously the circuit doesn't represent that!

I think groundfungus and Wawa have given you sound advise, I would us a voltage divider.

have you kept the grounds of the sensor and Arduino separate?

Good luck and report back please...

A misrepresentative diagram is worse than no diagram, it creates confusion.

A voltage regulator is used for providing power, not for sensing voltage presence. The output
capacitor of the regulator will hold the output at 5V for a long time on its own since there
is no load.

The resistive divider is a simple, safe and cheap way to reduce analog voltages for sensing
or measurement.

In the resistor divider circuit in posting #1 the capacitor C1 is probably not needed (unless there
is a lot of electrical noise in the environment. Two resistors is enough.

Thank you for all the feedback. I applogize for the bad diagram. I made it on website which didn't have the right components. I haven't mentioned it previously but I'm a complete noob and can use all the help the community may provide.

I have tried a resistive divider as groundfungus suggested but I wasn't able to get the feedback either.

I sent the reading to the Serial Monitor (which I have only learnt to use today) and I get the readings below. I have also put a meter and on it I get 4V reading (I used different resistors which were available to me). As you can see, it switches between 0 and 1023. I've also tried digitalRead and there as well got randomly 0 or 1. The meter shows the sensor works as it shifted between 4V and 0 but I just wasn't able to get the right reading. I used delay of 1000.

Any help will be appreciated.


Are the motion detector ground and voltage divider ground connected to the Arduino ground? Can you post a new schematic of your circuit wiring?

Maybe you should read datasheet I don’t think it output’s any thing it just switching on or off so you read the state or use it to switch something on or off

Current consumption 20 mA or less

NPN open-collector transistor
• Maximum sink current: 100 mA
• Applied voltage: 30 V DC or less (between output and 0 V)
• Residual voltage: 2 V or less (at 100 mA sink current)
1 V or less (at 16 mA sink current)

PNP open-collector transistor
• Maximum source current: 100 mA
• Applied voltage: 30 V DC or less (between output and +V)
• Residual voltage: 2 V or less (at 100 mA source current)
1 V or less (at 16 mA source current)
Output operation Switchable either Detection-ON or Detection-OFF
Short-circuit protection Incorporated

Thank you again for your help!

Are the motion detector ground and voltage divider ground connected to the Arduino ground? Can you post a new schematic of your circuit wiring?

I have attached new schematics as you requested. It is important to note I’m not using a regular relay but one module to which I connect VCC, GND and IN.

I’ve tried to ground to the Arduino board but then I didn’t get any reading on my meter so I grounded it to the 12V source and I was able to get a reading on the meter. I didn’t ground to both since it seemed to me like a bad idea that may fry the board.
I also eliminated the capacitor in groundfungus sketch b/c I wanted an immediate change of state though I’ve tested with or without a capacitor.

Any ideas?

Don't guess you read every post but here how it works it's a pnp that's switched on or off
not good for ADC reading's

I have attached new schematics as you requested.

Another misleading Fritzing diagram.

What type of sensor do you have.
A PNP CX-421-P or an NPN CX-421-P .
The diagram in post#1 can ONLY be used for a PNP type sensor.

An NPN sensor is easier.
Just connect the output to a digital pin with INPUT_PULLUP enabled in PINMODE.
Or a lower value external pullup resistor with long interconnect leads and/or in noisy environments.

Make sure you got it right.
A PNP sensor connected directly to a pin will fry the pin.

Hi be80be,

This is indeed a chart from the sensor broucure. Here one can find the full PDF.

Can you please explain why isn't it suitable for ADC readings? Can you recommand another highly reliable sensor (for industrial use) which is suitable?

Thank you!


CX-421-P is a PNP sensor while the CX-421 is NPN.

be80be writes this type of sensors aren't suitable at all. What is your opinion? Can you please draw a diagram with the right circuitry?

You don't say what your trying to do. But what you say you have is the pnp sensor it's made to drive a load up to 100 mA at 12 volts
You could clamp the output to 5 volts
But I don't see it giving any useful ADC readings.
It was made to switch something on when you move by it and block the beam
It's a on or off like a switch

+1 This is a digital sensor. YES/NO. ON/OFF.
Hope this daigram helps.

OK, let me try and describe the greater picture.

We have a conveyor at work and the sensor is used to pull boxes off it. The sensor job is to determine the location of the box in relation to the exit point. When it sees a box, it closes the circuit and sends current and once the box passed it, it opens the circuit which stops the current through the black wire.

There is an old PLC (which nobody wants to try and reprogram and is also connected to tons of other stuff) which gives a command when it STOPS receiving current from the sensor. It doesn't do anything when it starts receiving current or is between ON/OFF cycles.

There is a relay (1st) down the line, controlled by a computer with barcode scanner, which opens and closes according a piece of software that determines whether the box should exit or stay. Because of that, we face instances where the sensor is CLOSED and the 1st relay opens which triggers the PLC.

My idea was to get the pulse from the sensor to the Arduino and that way control a 2nd relay. Thus, instead of having current for lengthy period of time, during which the 1st relay can close/open, the Arduino will receive the current directly from the sensor and it will control a 2nd relay to pass a current to the PLC for just a few millisecond. This will prevent the 1st relay from affecting the circuit by opening the circuit b/c most time the circuit will be open anyway and if it opens during those few milliseconds when it is closed, it doesn't really matter.

Does this make any sense???

OK guys. We've done it!!!

My big mistake was - connecting the ground only to the 12V ground but not the Arduino. Wawe/Leo - your diagram showed me it was wrong. Thank you so much for it. (I wrote earlier "I didn't ground to both since it seemed to me like a bad idea that may fry the board.")

I attach my final code as a token of appreciation (and maybe some feedback as well :slight_smile: )

int Relay = 10;
int Sensor = A4;
int state = 0;
int lastState = 0;

void setup() {
pinMode(Relay, OUTPUT);
pinMode(Sensor, INPUT);

void loop() 
  state = analogRead(Sensor);
  if(lastState == 0 && state>500)
    digitalWrite(Relay, LOW);
    digitalWrite(Relay, HIGH);
  lastState = state;  

Once again guys - THANK YOU for all your help!

Good result. I was trying to establish if you had on my first post.


Here is a video of the board in work.

Now that the concept works, I plan on buying a FA-DUINO-12RA which is meant to work in 24V envirnment and should be more robust.

Thank you all!

Hmm no video. But thanks for the link.

I'm thinking of make a redundant or hot standby by using the serial comm.