PinMode Activates Pin

When trying to animate LED light strings with a relay, all the lights would initially turn on and they weren’t sequencing as planned so I ran a simple experiment. First, I’m using an Uno to drive an Elegoo 8-channel relay and for the experiment I hooked up a single light to Relay 1 in the NO configuration. For my first test, the only code I had in the sketch was
‘pinMode(4, OUTPUT);’
When the sketch was uploaded, Relay 1 activated and the light turned on. This was unexpected so then I performed a second test to turn off the relay and light. The code in the second sketch was
‘’’
pinMode(4, OUTPUT);
delay(1000);
digitalWrite(4,HIGH);
‘’’
When this sketch was uploaded, the relay activated and the light came on, then after the one second delay, the relay and light turned off. This was even stranger in that HIGH turned it off. I originally tried LOW, but it did nothing.

Any suggestions as to why pinMode() activated the relay and then why digitalWrite(4, HIGH) turned it off? Thanks .

The relay has an opto-coupler (opto-isolator) on the input. See this page >> https://robu.in/the-basics-of-optocoupler-relay/

Try:

digitalWrite(4, relayOFF); //where relayOFF is either HIGH or LOW, as needed
pinMode(4, OUTPUT);

Here is how those relays are typically wired. A LOW on the output allows current to flow through the LED so the transistor will turn on and activate the relay. A HIGH on the output puts the same voltage on both sides of the LED so it does not light and the transistor remains off.

image

An Arduino output defaults to LOW at setup when the pin is initially changed to OUTPUT so that is why the relay will activate on
pinMode(pin, OUTPUT);

Thanks LarryD.

Thanks groundFungus. It makes sense. I’m fairly new to this. All other applications I did had HIGH for turning something on and LOW for turning it off. I had the light animation working after some trial and error, but I couldn’t figure out why it was reversed. Appreciate the explanation.

You are welcome. Glad to help.

If your questions have been answered, please mark the thread as Solved so that members that wish to help will not waste time opening the thread to help only to find the questions answered.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.