(Solved! ..Software Solution) How to prevent Output pins floating on boot

I have a project which controls some relays connected to pins 12 and 11 Vcc and GND. When pins 11 or 12 go high the relay triggers. Simple.

The problem is the relays are triggered whenever the system boots up.

Iv read this it is normal for the pins to go high during bootup however I need a way to stop this happening as I cannot have the relays trigger every time I apply power to the system. Simplest solutions would be the best.

Im using an Arduino Mega 2560 and 5v trigger relay.

How can achieve this?

Thanks in advance.

(deleted)

Iv read this it is normal for the pins to go high during bootup

No that is not true.
On power up the pins are inputs not high, that is why a pull down resistor will work because it stops the input of what ever you are connecting them to from floating.

I have come up with a simple software solution for it in the end.

At the start of setup() I do the following
pinMode(12, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);

Then after I initialize a few things such as serial comms I use
pinMode(relayOne, OUTPUT);
pinMode(relayTwo, OUTPUT);

This is preventing the relays triggering when I power on the unit

I couldn't not find ANY reference to this solution im using, only the use of hardware resisters inline with every output you want to stop floating on startup. I found the INPUT_PULLUP pin type definition in the arduino API but does not mention using it in this way.

Perhaps this should be added?

Anyways I hope this helps anyone who need to solve floating pins on startup with just a few lines of code. :wink:

I'm confused. To trigger the relays, the outputs go high. But setting those pins to input and setting the pullup will connect a 20k resistor to Vdd. Would that not trigger the relays?

Is it actually the pullup that does it, or is it just that you've quickly changed the pins to inputs and so made them Hi-Z, but the internal pullup just isn't quite enough to activate the relays? In which case, not activating the pullups would be better, I'd think.

Perhaps this should be added?

No because there is something wrong here. Can you post how you are wiring the relays up to the pins?

If you have a bootloader then the program won't run for a few seconds after powerup. The only solution is external pulldown resistors.

If you remove the bootloader then the program will start instantly. In that case then maybe software can do it. Pulldown resistors are still a good idea though in case the system fails to boot properly for some reason.

Relays take current, I suspect the situation was:

sketch read floating inputs and used them to decide how to drive relays
via OUTPUT enabled pins.

sketch later enabled INPUT_PULLUP on the floating inputs.

Fix was to enable pullups at the start of setup(), before driving the OUTPUTs.

Nothing to do with reset really, just doing things in the wrong order.

If the relays indeed had CMOS inputs then there is no fix apart from hardware
pull-down resistors.

Either way my problem is solved...

There is however one more test that needs to be done to verify my solution is valid....

Ill re load the project tonight without the pinMode(12, INPUT_PULLUP) at the start of the setup() and simply have the pinMode(12, OUTPUT) at the start of the setup()

If I DONT get a relay trigger apon bootup then all I needed to do was have the pinMod defined at the START of the setup to prevent a relay being triggered when the system boots up.

If I DO get a relay trigger at the start then this is enough evidence to say the pinMode(12, INPUT_PULLUP) was the actual solution to the problem.

Ill everyone know tonight (about 6 hours from now)

Either way my problem is solved...

It might be but without context which you keep on refusing to give it is absoloutly no use to anyone else.
As everyone here has said on the face of it what you are doing makes no sense and to be useful to anyone else a soloution has to make sense.
This is engineering not hocous pocous.

I removed pinMode(12, INPUT_PULLUP); from the start of setup() altogether and placed just pinMode(12, OUTPUT); at the top of the setup() instead to make sure the placement of the pinMode wasnt the actual fix for the problem.

This started triggering the relay once every reboot again (the original problem I was trying to solve) so this means that by defining a digital I/O pin as INPUT_PULLUP and then defining it as OUTPUT straight afterwards prevents what ever is causing this 5v relay to trigger apon startup.

This has no relay trigger on startup

void setup()
{ 
  //Prevent relay trigger on bootup
  pinMode(12, INPUT_PULLUP);
 //Define the relay pins
  pinMode(12, OUTPUT);

This causes the relay on digital pin 12 to toggle on then off apon startup

void setup()
{ 
 //Define the relay pins
  pinMode(12, OUTPUT);

This will be useful to people as it does not require wiring any resistors inline with the relay triggers.

Sorry if iv been to vague about the project

  • Arduino Mega2560
  • 3.2Inch TFT display and LCD Shield
  • DS3231 RTC
  • 2 channel relay with 5v triggering
  • Project capable of setting 50 scheduled time events (saved to EEPROM) in 24h time on a per day of the week basis complete with 10 scheduled holiday events, auto daylight saving adjustment, pulse time setting of the RTC time and date, factory reset, etc all via the 3.2 TFT using a GUI I made.

Wiring

  • LCD and Shield attached to mega
  • Arduino supplying 5v and GND to 2 channel relay and RTC
  • 2 channel relay triggers connected to digital pins 12 and 11

Thats it.

Are there any other details you would like?

If anyone has an explanation to why this *crude fix" works that would be great.
Im not an electrical engineer, if someone who has an explanation to why this works that would be very helpful for everyone.

Note this has been a long standing problem for multiple relay projects iv done and im very pleased with the "simple solution" I come across.

And no hocous pocous was required

I haven't followed this in detail but it confuses me: since the relay pin being high is the problem, how can pulling the pin high make the problem go away. The hardwired pull-DOWN solution seems more logical to me.....

But then I'm not an electrical engineer either 8) just a simple mud-mechanic.

No hard wired pulldown resistor required.

Why do people keep saying it is

Horendus:
No hard wired pulldown resistor required.

Why do people keep saying it is

If that's aimed at me, I didn't say it. I'm confused that your solution works and I'm just wondering why.

Why do people keep saying it is

Because you have provided nothing so far that would possibly explain what you see.

I removed pinMode(12, INPUT_PULLUP); from the start of setup() altogether and placed just pinMode(12, OUTPUT); at the top of the setup() instead to make sure the placement of the pinMode wasnt the actual fix for the problem.

This started triggering the relay once every reboot again

Now when the processor is reset all the pins automatically become inputs, so putting a line that defines that pin as an input should make no difference.
What it looks like you might be doing is pre-setting the state of the pin12 data register so that when you switch it to be an output it immediately becomes a logic ( high or low I know not ).
Immediately before the pinMode(12, OUTPUT) write the correct level to pin 12 so as not to have the the pin power up in the wrong state and then do it again after to make sure.

This will be useful to people as it does not require wiring any resistors inline with the relay triggers.

No it won't because it is not a generic solution. It is something very peculiar to your specific setup. The explanation of the wiring as :-

2 channel relay with 5v triggering

Is not an sufficient description of your setup to understand what is happening. What is the relay? How is it powered? Has it got a diode across it? How is it driven?

once every reboot again

There is the world of difference between boot, as in from power up and reboot as in when the reset line is triggered. This is the first time you have mentioned reboot.

I dont know what else to say guys.

Arduino compatible 2 channel relay. Can be triggered by arduino digital pin going high. Required 5v and GND as well.

Its powered from the arudino with one of the 5v pins. Its also grounded to an arduino GND pin.

These relays toggle close then open if connected to an Uno, Mega or Micro digital pin if you define the digital pin its connected to as an OUTPUT in the setup(), which of course is required to control the relay during the running of the program.

Solution

Define the digital pin the relay is connected too as an INPUT_PULLUP and then redefine it as an OUTPUT if you dont want the relay to close then open when you disconnect the power to the arudino and re connect it, or press the reset button on the arduino.

Im not sure what else to say.

A problem with a cheap mass produced Arduino relay and a simple software based solution to it.

Moderator edit: removed reference to company selling counterfeit board

But why do you keep setting the pullup?

Arduino compatible 2 channel relay.

It is a bit like dragging blood out of a stone. A link would have been nice. Never mind I will put in the extra work and google it.

Define the digital pin the relay is connected too as an INPUT_PULLUP and then redefine it as an OUTPUT if you dont want the relay to close then open when you disconnect the power to the arudino and re connect it,

As I and others keep pointing out this is rubbish.

Something is going on that you don't understand and so you are not able to convey it to us.

Are you saying you still get the problem with defining that pin as just INPUT without the pull up? Still makes little sense.

Can you post a sketch where you simplify it down to initialing and a few delays, and produce a version that shows the fault and one that doesn't.

The reason why it is important to get to the bottom of it is that if we don't it becomes a "superstition", "some person said this and it worked for him" sort of thing and it is what we want to avoid at all costs on a quality forum like this.
I know some other forums are like the blind leading the blind but that is not how we work here.

Thanks for that John, so it is just like I said in reply #14, pre load the output register before enabling it.

1 Like

Grumpy_Mike:
Thanks for that John, so it is just like I said in reply #14, pre load the output register before enabling it.

So is this the reason why defining a pin as INPUT_PULLUP before OUTPUT prevents the relay from triggering whenever you reboot the arduino?

Moderator edit: remove link to company selling counterfeit board