Detect Switch position with arduino

I am doing a project where the arduino will control signals on a minor model train layout. The signals will simply follow turnout positions and that will come from a switchboard which has switches to decide the position. The problem I have is that the creator of the switchboard has kind of "reversed" thesystem so I can not use the signal from the LED as input on the arduino. I live far out in the bush so it would be nice to solve the problem without anything else than resistors.

Can I somehow make the arduino see if the signal is grounded or not as a detection method? I have enough digital and analog pins for input.

what do you mean by "reversed" - is ON 0volts and OFF 5volts? if so you read the digital value and invert it so ON is a 1 and off is a 0

horace:
what do you mean by "reversed" - is ON 0volts and OFF 5volts? if so you read the digital value and invert it so ON is a 1 and off is a 0

For me as a noob it is "reversed" for a lack of better words. What I mean is that the switch grounds the LED. It doesn't power it. Since there is no voltage on the switch side I can not detect it with the arduino.

if the switch gounds the LED when it is switched ON the Arduino would read that as a 0
if the switch is OFF what happens to the LED voltage? if it has a pullup resistor the voltage would go the 3v or 5volts which the Ardunio would read that as a 1
you then invert the value read to know if the switch was ON or OFF

sandpatch:
Can I somehow make the arduino see if the signal is grounded or not as a detection method? I have enough digital and analog pins for input.

Be careful not to connect anything higher than 5v to an Arduino. If you want to detect a 14v signal than use a couple of resistors to make a voltage divider.

And connect the Arduino GND to the circuit GND.

Then using digitalRead() you can test whether the input is HIGH or LOW. It won't matter to the Arduino which of HIGH or LOW represents "ON".

...R

horace:
if the switch gounds the LED when it is switched ON the Arduino would read that as a 0
if the switch is OFF what happens to the LED voltage? if it has a pullup resistor the voltage would go the 3v or 5volts which the Ardunio would read that as a 1
you then invert the value read to know if the switch was ON or OFF

Ohh, you mean put one resistor after the LED to pull up the voltage on the other end? That I should be able to do. Thanks!

an LED requires a series resistor to limit the current, e.g.

which you circuit has - when the switch is OFF the LED pin should go to 14V - test it with a meter
use a voltage divider to feed this to an arduino digital input

I can still not get the arduino do detect anything at all. What do I have to add where to get anything successfull so it detects High or Low. I tested a0 with the ReadAnalogVoltage premade program but I can still not get anything out, only noise...

Maybe I can explain a bit more. All I get is weird waves but no steady signal. Also a switch change does nothing, there is still noise going from 0 to 5 volt. With a resistor to remove the excess voltage (floating) I get only 0 volt no matter what.

Is here anyway around those LEDs that you can measure the actual voltages with your multimeter?
Find a spot near those LEDs that toggles between 0V and a higher value (any value from about 0.5-14V will work as long as it's a stable readout). Or even two positive values will work, again as long as it's a reliable readout.
As those LEDs are switched on and off, the voltage level changes within the circuit, so there must be a way to hook that up to your Arduino.
Come to think of it: connect Arduino GND to the negative side of the LED, the ADC to the other side of the LED, and you can read the voltage across the LED (about 1.8V normally, so you'll see an analog reading of around 350 when the LED is lit, 0 when it's off). If your LED doesn't pull down the ADC properly add a 10k or so resistor between the ADC and GND. It won't affect the LED but it does make sure the ADC is pulled to zero when the LED is off.
Note, this only works if Arduino and your railway have separate power supplies as you're creating a new GND reference. If they are the same power supply, and the GND is connected that way, you will run into problems.

There should not be any need to use analogRead(). digitalRead() is simpler and faster.

@sandpatch, have you the Arduino GND connected to the led circuit GND?

And MAKE CERTAIN that nothing higher than 5v can get to an Arduino pin.

...R

Robin2:
There should not be any need to use analogRead(). digitalRead() is simpler and faster.

Ill happily use that if I get above 3 volts.

Robin2:
@sandpatch, have you the Arduino GND connected to the led circuit GND?

And MAKE CERTAIN that nothing higher than 5v can get to an Arduino pin.

...R

Huh? I need to connect the arduino GND (any of them? (mega2560) to my 17v power supply GND to make this work?

wvmarle:
Is here anyway around those LEDs that you can measure the actual voltages with your multimeter?
Find a spot near those LEDs that toggles between 0V and a higher value (any value from about 0.5-14V will work as long as it's a stable readout). Or even two positive values will work, again as long as it's a reliable readout.
As those LEDs are switched on and off, the voltage level changes within the circuit, so there must be a way to hook that up to your Arduino.

If you check my first drawing there is nowhere where I can check that. The led wires are constantly powered or they are grounded (0v)

wvmarle:
Come to think of it: connect Arduino GND to the negative side of the LED, the ADC to the other side of the LED, and you can read the voltage across the LED (about 1.8V normally, so you'll see an analog reading of around 350 when the LED is lit, 0 when it's off). If your LED doesn't pull down the ADC properly add a 10k or so resistor between the ADC and GND. It won't affect the LED but it does make sure the ADC is pulled to zero when the LED is off.
Note, this only works if Arduino and your railway have separate power supplies as you're creating a new GND reference. If they are the same power supply, and the GND is connected that way, you will run into problems.

I am a noob at this so I am having some troubles following you. Since this is 17v I am a bit afraid of connecting the LED ground to the arduino. The LED's are connected to a power supply by both sides of the LED.
The GND are not connected so does that mean that if I ex. would put 5 volt into the start of LED in addition the original power, wouldn't I overload it?

This is what wvmarle is describing:

led on arduino.PNG

Connected this way the Arduino input will see the 1.8 volts developed across the LED only when it's on.

.

led on arduino.PNG

Hi,
This will be safer , all gnds connected together, the A0 voltage stepped down by the R!, R2 voltage divider.
led on arduinoedit.jpg

What is your power supply, is it pure DC or just rectified DC (pulsed DC)?
Tom... :slight_smile:
PS. If you use the other version and have multiple LEDs to monitor then all the LEDs cathodes would be connected together through the Arduino gnd.

Hi,
If you use the circuit in my post, you may be able to massage the R1, R2 potential divider values to feed the Arduino voltages that are logic LOW and HIGH levels.
That way you can us digital inputs, rather than use the limited number of analog inputs.

Tom... :o

Shouldn't be too hard to get to 4-5V levels on the pin.

1.8V per LED, 17V input, 10 mA, means you'd have a series resistor of 1.5k, which is becoming part of the voltage divider.
Let's say 4k7 for R2.
If using 10k for R1, that would give 4.9V on the pin. Using 12k would be safer, bringing the voltage down to 4.4V. Still comfortably in the HIGH detection range.

Indeed it may be pulsed (rectified) DC. That's a problem as you get a much higher peak value on the pin, the peak is about 24V that way. The clamping diodes of the pin should be able to handle this, rather prevent it from happening. Also the pin will constantly switch between HIGH and LOW and that makes detection unreliable.

Solution: a small smoothing capacitor across the pins (a common 100nF should be enough for this), and increase the value of R1 to 20k for a 4.3V level at the pin.

Interesting! I would definitively prefer the logic gates before the analog inputs. The layout right now looks like this:

What my goal is to read 7 of these switches so the arduino knows what position they are. To my understanding I can not increase the voltage to make the digital pins work.

The thing is that the arduino will ground 12 other signal LED lights (the ones it will control) so I am getting worried about the amount of current through it. The lights have a common + so actually the arduino will use LOW to power the LEDs and HIGH to turn them off. So a total of 24 signal LED will be powered through that. I have killed a USB cable tough to power the +5 v so that doesn't have to come from the arduino.

On the DC power, I am quite sure that it is a pure DC coming out. It is a model train transformer for DC. It is 17 volt across with the multimeter.

Model train transformers often do without the massive smoothing caps needed for the current those motors draw, and the train motors don't care about pulsed DC. They run just fine. Very likely it's not smoothed.

sandpatch:
The thing is that the arduino will ground 12 other signal LED lights (the ones it will control) so I am getting worried about the amount of current through it. The lights have a common + so actually the arduino will use LOW to power the LEDs and HIGH to turn them off.

That is a total change of what you said before, which had the LEDs powered by the 17V transformer, and your Arduino needing to read whether they're on or off. if the LEDs are powered by the Arduino the whole having to read signals is of course moot.

So you'll have to explain the ACTUAL circuit before I bother to continue.

The picture I attached to the previous post are the LED lights that indicate to position of the switches. 7 of them are needed for the logic. They will give information to the arduino. These are the ones with 17V.

The arduino will control 12 signals (36 LED total, 24 powered and 12 grounded, that means 12 burning) that are controlled by 5 volt. The signals I got to work.