Using an arduino to control a signal on a model railway

Hi,
I have a signal which uses a red led and a green led but wired to have opposite polarity. ie, when the current flows in one direction, the red led lights up and when the polarity is reversed, the green led lights up. My question is: As there only one set of wires connected to the leds, how can I reverse the polarity using only one pin?

I think you could use an inverter IC. Connect an LED with its current limit resistor across the inverter, one LED lead @ input and resistor @ output.

Or use two pins.

Will the signal always be lit, i.e. will it always be either red or green, and never off?

No I would need the ability to switch off as well.

Hi, Yes using 2 pins would definitely work but the signal has only 2 wires so how would I be able to connect to 2 pins?

I think, then, you would need two pins.

One side of the LEDs receives the inverted voltage, and the other side is connected to GND?
Or are both sides of the LEDs "free"?

If you can use 2 pins, it's easy.

First LED on:

   digitalWrite(ledPin1, HIGH);
   digitalWrite(ledPin2, LOW);

Other LED on:

   digitalWrite(ledPin1, LOW);
   digitalWrite(ledPin2, HIGH);

Both off:

   digitalWrite(ledPin1, HIGH);
   digitalWrite(ledPin2, HIGH);

With an appropriate current limiting resistor in series with the LEDs, of course.

Something like this:

2024-04-20-200022_1703x966_scrot

3 Likes

If you use a 5V micro (like UNO, Nano, Mega) and the LEDs are "common" you can connect them like this

2 Likes

as a model RRer, i'm curious what model signal you have?

you could replace the 2-pin LED with a 3-pin red/green LED, $2.20/100

See post #10.

If you wire the leds together so each + leg is paired with the other led's - leg and use 2 pins... make one HIGH and the other LOW, one of the leds will shine. Swap the HIGH and LOW and the other led will shine. Both pins LOW, both leds OFF.
One pin will need a resistor of 220 Ohms or more.

BTW there are red-green 2 junction leds that do just that but also need a resistor. They are used in logic probes, AC shows amber.

Post # 10 is your best bet - you can vary the resistors to balance the intensity, and simply make the output an input in order to turn off the LEDs.

Then you can make Pin an INPUT to turn the LEDs off?

Why does that surprise you, @herbschwarz?

It doesn't surprise me. I just wanted to know that we were all on the same page.

Yes. A pin as input has high impedance so the very small current that can flow is not enough to light the LEDs.

Yup. OUTPUT, low, sinks up to 20 mA. Output, High, sources up to 20 mA. Input, can't source much at all. If you enable pullup, you have a 40 kOhm-ish path to 5V, but otherwise, input impedance is very high.

I do have a concern, someone who knows the ATMEGA328P well might be able to confirm or allay. If an input is held at VCC/2, there's a warning in this version of the datasheet:
https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf
section 13.2.6 has this to say:

If some pins are unused, it is recommended to ensure that these pins have a defined level. Even though most of the digital
inputs are disabled in the deep sleep modes as described above, floating inputs should be avoided to reduce current
consumption in all other modes where the digital inputs are enabled (reset, active mode and idle mode).
The simplest method to ensure a defined level of an unused pin, is to enable the internal pull-up. In this case, the pull-up will
be disabled during reset. If low power consumption during reset is important, it is recommended to use an external pull-up or
pull-down. Connecting unused pins directly to VCC or GND is not recommended, since this may cause excessive currents if
the pin is accidentally configured as an output.

In this case, the pin is connected; it has very definite, relatively low impedances holding it somewhere near 2.5V; while that's in what we used to call the 'linear region' for classic logic, and oscillation could be observed in some circumstances, I'm not at all sure it applies here. I think it would be worth trying. It's not a valid input level, but then, we're not trying to read it, just shut off the LEDs.

Anyone who has gone down this rabbit hole, or has definitive knowledge about the processor itself, please chime in.

1 Like

We're not. INPUT LOW makes the pin, when not being read, doesn't make the pin ground but does make it un-terminated.

Why I suggested 2 pins only with 1 resistor is there's no +5 or GND connection.

With AVR (Uno, Nano, Mega) pins ---
As pins (say) 6 and 7 are LOW-LOW it will be OFF, where if 1 pin is HIGH and the other LOW (either way), current will run in 1 direction and only the led (a diode) with + on the HIGH pin and - on the LOW pin you only need 1 resistor either way to limit that flow. The resistor keeps the leds and pins safe with fewer parts and wiring.

AVR pins max current is 40mA, continuous current up to 25mA is safe for pins and leds --- make leds VERY bright!

Dimming a led takes blinking the HIGH pin only.