Go Down

Topic: Trouble with analog input from reed switch (Read 986 times) previous topic - next topic

erogbass

Hi all,

I'm having a problem with what I believe is analog inputs tripling when they are not supposed to.
I'm really stuck so any help is hugely appreciated.

So first I will explain my setup and then what is wrong with it.

I am using an Arduino Uno, 3 magnetic pnp 5-24v/0.2A reed switches, and a bank of four 5v relays of which I am using three. What I want to happen is that when 1 reed switch is in the on position, it fires 1 relay and the relay is off at all other times.

What is happening is that sometimes when one reed switch is activated more than just one relay fires or partially fires.

Things to know:
-When all switches are off they show 0 volts.
-When 1 switch is on it shows 4.9V and the other two show .22V
-When 2 switches are on (intentionally) they both show 4.84V with .22V over the off switch.
-When 3 switches are on (intentionally) they all show 4.74V
-The unintentionally firing happens in an order. 1 fires 2, 2 fires 3, 3 fires 1.
-Sometime it can happen that a switch will fire the next one in order and partially or completely fires the 3rd one.
-Only inputs in the code are affected.
-The sensor are not tripping accidentally because it happens with only 1 sensor attached as well.
-When I increase my activation threshold the problem improves.
-When I lower my activation threshold the problem gets worse.

Here is my code

int gatePin = 2;
int clampPin = 4;
int machinePin = 6;
int threshold1 = 1020;
int threshold2 = 1020;
int threshold3 = 1020;
int analogPin1 = A0;
int analogPin2 = A2;
int analogPin3 = A4;
int val = 0;
void setup() {
  // put your setup code here, to run once:
 pinMode (gatePin, OUTPUT);
 pinMode (clampPin, OUTPUT);
 pinMode (machinePin, OUTPUT);
 pinMode (analogPin1, INPUT);
 pinMode (analogPin2, INPUT);
 pinMode (analogPin3, INPUT);
 Serial.begin(9600);
}

void loop() {

  int analogValue1 = analogRead(analogPin1);

  // if the analog value is high enough, turn on gate
  if (analogValue1 > threshold1) {
    digitalWrite(gatePin, HIGH);
  } else {
    digitalWrite(gatePin, LOW);
  }
  delay(1);

 {

  int analogValue2 = analogRead(analogPin2);

  // if the analog value is high enough, turn on clamp
   if (analogValue2 > threshold2) {
    digitalWrite(clampPin, HIGH);
  } else {
    digitalWrite(clampPin, LOW);
  }
  delay(1);
}
{

  int analogValue3 = analogRead(analogPin3);

  // if the analog value is high enough, turn on machine
  if (analogValue3 > threshold3) {
    digitalWrite(machinePin, HIGH);
  } else {
    digitalWrite(machinePin, LOW);
  }
  delay(1);
}
}


The reed switches and the relays are all wired in parallel with no resistors or transistors.

This is going to be used for an industrial process, so it is very important that I find a way to make it work reliably and consistently.     

Thanks so much everyone,
erogbass                   

gpsmikey

A couple of things you need to address - put your code in code tags so it makes it easier for folks to read.  You need to show us a schematic - your description of "The reed switches and the relays are all wired in parallel with no resistors or transistors." does not make sense with the description of how things work.  One of the most common issues when driving relays is failure to put any snubbing/transient suppression across the relay coil - when you turn a relay off, the collapsing magnetic field in the coil generates a pulse of the reverse polarity to what was applied and often many times larger than the supplied voltage.  Failure to deal with this causes all sorts of strange things to happen when that pulse gets back into your control system.  You also need to make sure you are not exceeding the drive capability of the Arduino that it sounds like you are driving the relay with.  Start with a schematic though of what you have that we can see.

 
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3

outsider

Just wonder why you use analog inputs to read reed switches which are either full on (5V) or full off (0V)?

MarkT

Lack of free-wheel diodes would explain things as gpsmikey has explained.  Relays are
inductive loads, inductive loads kick back _hard_ on switch-off if no path for the current is
provided.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Paul_KD7HB

I thought the OP was in error when he wrote " magnetic pnp 5-24v/0.2A reed switches". However, Google told me they are not real "reed switches", but solid state devices emulating reed switches.

Paul

gpsmikey

He also said they were all wired in parallel.  Since he has not returned to update us with an actual schematic etc, it is hard to say just what is going on.

mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3

Go Up