Need help solving

Hi Everyone,

I've been working on a project for work and cant seem to get it to work the way I want it too. I'm pulling my hair out trying to get this done, but no matter what I do I cant seem to get this to work right.

Here is a summary of what I am trying to achieve:
I'm wanting a green LED to light up (PWM) once three individual inductive probes have have been triggered simultaneously (analog).

I thought I had it working correctly using int avg, but it doesn't seem to be correct. :frowning:

Fair warning I am new to this...

Here is my Sketch:

int s1;
int s2;
int s3;
int light = 12;



void setup() {
  Serial.begin(9600);
  pinMode(12, OUTPUT);
}


void loop() {

  s1 = analogRead(A1);
  s2 = analogRead(A2);
  s3 = analogRead(A3);

  int avg;
   
  if (avg = (s1 + s2 + s3) / 3) {
    digitalWrite(light, HIGH);
  } else {
    digitalWrite(light, LOW);
  }
  delay(100);
}

Arduino Model: UNO REV3
Probe: Taiss inductive proximity sensor (LJ12A3-4-Z/BX-5V)
Probe Listing:

LED Listing:
https://www.amazon.com/Gebildet-3V-4-5V-5V-6V-7-5V-9VDC-Waterproof-Indicator-Energy/dp/B0C27S7S57?th=1

This is all powered by an external PSU including the Uno.

Hi Delta,

I was using an average because I had found it online as a possible way for me to get the Arduino to recognize the three probes the way I wanted.

Originally I was trying to use a Bool, but could not get it to behave the way I wanted. I'm trying to build a check fixture for work, where when all three probes touch a brass insert the light will trigger through pwm. The fixture will tell the machine operator if the part has all the required inserts by lighting up the green led.

The old sketch I was trying to get to work is below, but the behavior I faced was, if I touched/triggered one probe the light would come on. I need it to only come on if they are all three triggered at once (all three analogs show read a value).

int s1;
int s2;
int s3;
int light=13;

void setup() {
Serial.begin(9600);
pinMode(13,OUTPUT);
}


void loop() {

s1=analogRead(A1);
s2=analogRead(A2);
s3=analogRead(A3);

if (s1>1000 && s2>1000 && s3>1000) {
 
  digitalWrite(light,HIGH);

}
else {
 
  digitalWrite(light,LOW);

}

delay(100);
}

Please edit both your posts and apply code tags. Select all code in a post, click the <CODE/> button and next save the post. This makes it easier to read, easier to copy and the forum software will display it correctly.


It might depend on the board that you're using as well as on how the LED is wired. If this is an Uno/Mega etc and it's the internal L-LED (not an external one) HIGH would indeed mean on and LOW would mean off.

I suggest that you print the values of s1, s2 and s3. Based on the results you can decide what the limits and the logic in the if statement should be.

Hi @gamecrazeddork,

welcome to the arduino-forum.
For decent help precise information is needed.

I want to try to describe in my own words what I have understood how it should work.

You have three electrical probes.

The probes can be moved somehow "in" and "out"

Only in case all three probes get electrical conductive contact in the same moment the LED shall be switched on.

In "the same moment" is a timing-condition.

Is this a correct description of the wanted functionality?

Confirm my description or correct my description.

I have seen too many threads where too many assumptions were made and users talked past each other (had a different understanding of too many things and misunderstandings)

After that more details can be discussed

1 Like

Hello gamecrazeddork

Welcome to the best Arduino Forum ever.

Post the data sheets of Inductive proximity switch used.

Can you provide a link to the actual probe device you are using?

I dont understand why you are using analog inputs to check a value that is binary (ie contact or not contact)

Hi, @gamecrazeddork
Welcome to the forum.

It might be advantageous to tell us what your project involves.
What model Arduino you are using?

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

As others have suggested, simplify and test. Here's a very basic example:

float s1;  // Better to use 'float' type throughout
float s2;
float s3;

byte light = 12;

void setup()
{
  Serial.begin(9600); // To allow debugging with print statements
  delay(200); // For reliability of initial printing

  pinMode(12, OUTPUT);
}

void loop()
{
  //s1 = analogRead(A1); // Avoid real sensors until code logic is correct
  //s2 = analogRead(A2);
  //s3 = analogRead(A3);

  s1 = 480; // Run with various values until satisfied
  s2 = 490;
  s3 = 500;

  float avg = (s1 + s2 + s3) / 3;

  // Showing the value could help debugging with more complex conditions
  //  Serial.print("Average of three readings = ");
  //  Serial.println(avg);

  if (avg >= 500)
  {
    Serial.println("Average of three readings is equal to or over 500");
    digitalWrite(light, HIGH); // STAYS lit until avg drops below 500
  }

  else
  {
    Serial.println("Average of three readings is under 500");
    digitalWrite(light, LOW);
  }
  delay(100); // Take about ten readings per second

}

don't you want something like

    bool trig1 = analogRead(A1) > Threshold;
    bool trig2 = analogRead(A2) > Threshold;
    bool trig3 = analogRead(A3) > Threshold;


    if (trig1 && trig2 && trig3) {
        // ...
    }

Ah ! This is a detail I have overlooked

What kindof inductive probe is it
6V to 30V powersupply
potential-free contact?
open-collector-output NPN?
open-collector-output PNP?

You should post a datasheet of the inductive proximity sensors

Are the probe signals digital (ON or OFF)? If so, you should read them with digital pins, not analog. Post a link to the sensor's datasheet or brand name and exact part number.

Hi Everyone,

Sorry about taking so long to respond, I added the Probe and LED link to my original post.

Unfortunately, I bought them on amazon, so they are isn't a datasheet. I'll try and search around for one.

I was thinking I would need more of a conditional statement, kind of like:
When a value is read on Probe s1, s2, and s3 (at one time), then the Light:12 = digitalWrite (light, HIGH).

I'm not entirely sure if this is possible or if it needs to be a timing condition?

Sorry I'm very new at this and decided to dive straight in head first.

Hi, @gamecrazeddork
This might help;
LJ12A3-4-Z-ETT.pdf (125.1 KB)

You are expecting a variable voltage output from the sensors.
The link you provided says that they are NPN NO, in other words they have either 0V or 5V output if the output is connected to a 5V supply via a pullup resistor.

The values you will get on the analog inputs will possibly be 0 or 255.

Why do you need an average?

All you need to do is an if statement that;

Checks if sensor1 == LOW AND sensor2 == LOW AND sensor3==LOW.

LOW is the activated output because of the NPN NO or Open Collector output of the sensor.

You need to change the three inputs to digital inputs with pullups
and digitalRead them, to get a HIGH,1 or LOW, 0 response from each.

Can you please post a schematic of your project?

Tom... :grinning: :+1: :coffee: :australia:

The video on the product link shows the metal-proximity sensor in action.

First of all:
You did not confirm or correct my description.

If you try to go on this fast without specifying some details it will be pure luck if it will work or not
and you still will have no control about if it really works reliably .

This is for a production: So my assuming is it should work ideally 100,00% reliable and practically 99% reliable. (not only 87% reliable)

reading in three inputs will take less than 1 millisecond (0,001 seconds) which means your three proximity sensors would have to switch in absolutely perfect synchronisation.

I highly doubt that this will be the case. And I doubt that you need it within the same 500 microseconds = 0,5 milliseconds = 0,0005 seconds.

This means checking if all three sensors have switched with a simple if-condition will not work.

You might be thinking keep it stupid simple (KISS) but there is a limit for this simplicity.
rhetoric question: Why don't you make it even more simple by using only one proximity-sensor?
because it would be toooo simple.

Same thing with an if-condition.

Sorry you have to learn a bit more programming than a simple if-condition to make it work.

Additionally:

You should describe the real application in detail.
Depending on the real circumstances a good suggestion can be made how to

  • detect if everything is ok
  • how to write the code for it

I was working for a special-machine-company for autombile-production. You can imagine that the reliability requirements in a production-shop were really high. Each and every minute that the production-line is running counts.

So the machines had to work very very reliable. I am talking from experience what strange things can happen in a real production 24/7.


BOM

Here is the BOM/Wiring Diagram, please don't laugh I'm new to all of this. :slight_smile:

Hi Stefan,

My apologies, I'm very new to all of this so I was trying to under stand.

The end goal of this project is creating a fixture for an injection molded part that has three metal inserts molded into it. This would be a quick check fixture to see if all the metal inserts are there, and if so it would light the green led. The probes would be touching all insert locations at the same time.

I want to describe what I have understood with my own words:

A machine is producing plastic injection molded parts. These parts have three metal inserts.

The device shown in the picture has these three inductive proximity sensors.
A worker will put the new produced injection molded part into the checking device.
The recess ensures that the part is precisely aligned and if all three metal inlets are really inside the injection molded part the green LED should switch on signalising that all three metal inserts are inside the injection molded part.

Does this mean the metal inserts are covered with plastic from all sides?
Are the metal inserts not visible?

From seeing the device now an if-condition is indeed sufficient.
The code has to check

"have all three inductive proximity sensors switched to signal "metal present" ?
And in case of yes switching on the green led.

This means a simple if-condition with two logical "AND"'s will be sufficient

// with each iteration read in all three sensors new
sensorLeftState   = digitalRead(sensorLeftPin);
sensorMiddleState = digitalRead(sensorMiddlePin);
sensorRightState  = digitalRead(sensorRightPin);

if (sensorLeftState == metalDetected && sensormiddleState == metalDetected && sensorRightState == metalDetected) {
  digitalWrite(LED_Pin, ON);
}
else {
  digitalWrite(LED_Pin, OFF);
}

The code above uses self-explaining constants which must be defined additionally

You should make a pre-test with your proximity sensors:

The pretest is done without Arduino
connect a 20kOhm-resistor to +5V.
connect other end of the resistor to red probe of digital multimeter
connect black probe of digital multimeter to signal-wire of the proximity-sensor

measure current with no metal present (current should be zero)
measure current with metal present

current should be approximately 0,25 mA


.
.

voltage measuring:

dis-connect digital multimeter
connect signal-wire of the proximity-sensor directly to the resistor.

Adjust digital multimeter to measuring voltage

measure voltage between ground and singnalwire of the proximity-sensor with no metal present should be +5V

measure voltage between ground and singnalwire of the proximity-sensor with metal present should be approx 0 V

If you have done this pre-checking you know inductive proximity-sensor works as expected

next step connecting the signalwires of the proximity-sensors to

digital

inputpins
named D4, D5, D6

The digital input-pins must be configured

pinMode (sensorLeftPin,   INPUTPULLUP);
pinMode (sensorMiddlePin, INPUTPULLUP);
pinMode (sensorRightPin,  INPUTPULLUP);

write a testcode that does nothing more than printing the logical state of the three IO-pins to the serial monitor all in one line

This will check if the proximity sensors work as expected when connected to the arduino

only after these pre-tests start writing the final code