I have a Arduino UNO with a Ethernet shield which hosts a basic HTML page with a button on it which controls a Relay module.

I'm trying to connect to a computer's power button so I can turn it on remotely.

I have found out that the relay basically does the opposite to the digital pin's output

Normally open Relay
HIGH = Open
LOW = Closed

I connected it through a NOT Gate made using a NPN transistor and it works perfectly how I wanted it.

Now to the question... Is there a way to write the code to make the digital pin = HIGH straight away and fast enough to not allow the relay to close the circuit and accidentally turn the computer on/off when the Arduino restarts, so I don't have to use a NOT Gate.

  • 1
    It sounds like you have a relay module with an opto-coupler that is wired so HIGH = IR LED on = Phototransistor ON = power transistor OFF. Either that or the IR LED is wired so that HIGH = OFF and LOW = ON. You will have to show your schematic and relay module for us to be sure.
    – Majenko
    Commented Oct 29, 2015 at 0:29
  • @Majenko Yes I think your right, I can't find a schematic but here's the link to it. amazon.co.uk/gp/product/…
    – Marius
    Commented Oct 31, 2015 at 22:16
  • What you actually have is one of these: sainsmart.com/arduino/arduino-components/relays/…
    – Majenko
    Commented Oct 31, 2015 at 22:57

5 Answers 5


The relay module you have is this one from SainSmart:

The schematic is like this:

enter image description here

As you can see you connect VCC to +5V and IN2 to your IO pin. Setting the IO to LOW will then turn on the LED and the optocoupler together.

But that's not the only way you can connect it up.

Connect VCC to the IO pin and IN2 to GND instead. Then you set your output HIGH to turn it on.

The arrangement shown in the schematic is commonly done because some MCUs are able to sink more current than they can source thorugh an IO pin. That is not the case with an Arduino which has symmetric IO pins that can sink and source the same amount of current.


You could use a less invasive way to turn on your computer: wake on LAN. In this case you only need to generate the magic packet.

It won't work in forcibly turning off the PC, but it's more generic and doesn't require HW hacking.

  • The motherboard is too old to support Wake On LAN (it still has a floppy disk driver on it :D ) By the way, can a Arduino be used to send Wake On LAN packets?
    – Marius
    Commented Oct 31, 2015 at 22:25
  • Did you check in the BIOS? I can't remember having a PC without this feature, for at least 15 years. If you check the link I provided, you will see that you only have to generate an UDP packet with the specific content described and send it in broadcast mode. I have not tried it, but I do not see why it shouldn't work. Commented Oct 31, 2015 at 23:06

To ensure that relay doesn't switch between NO-NC when controller is away relay will need some memory.

Imagine having a 1 bit memory driving relay. If arduino dies/restart, its own GPIO may change, but relay's memory will remember last state and stay there, till controller instructs otherwise, granted relay circuit and its memory element don't lose power.

This also means there will be at least 2 lines required from uC to control this relay now.

I'd prefer to use some kind of serial shift register, e.g. CD4094, CD4042, CD4099 etc because that allows me to control more than one relays from same 2 lines on uC.

You may find some other part more suitable for your application.

  • I could also think of an alternate. If instead of BJT, you were using a mosfet, with low gate capacitance, you could just keep a capacitor at the gate to hold the charge, in absence of driving inputs from uC. This design would require that uC's lines go tristated when not driven, e.g. restarts. Such a capacitor would be able to hold the charge for a limited time, so your uC must reappear within some time limits.
    – GPS
    Commented Oct 30, 2015 at 13:18
  • Great idea, however I was trying to operate the relay with having a circuit between the Arduino and Relay board. At the moment I've build an NOT gate on a bread board which seems to be working.
    – Marius
    Commented Oct 31, 2015 at 22:34

I've solved the problem, Thanks to everyone who helped.
I don't need a NOT gate anymore, here's what I added to make it work:

void setup() {


void startup() {
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);

void loop() {


(I removed the rest of the code to keep it simple and easy to understand)


Try This:

const On  = LOW;    // For non inverted logic swap on and off.
const Off = HIGH;
const pump = 5;

void setup() {
  pinMode(pump, OUTPUT);    // sets the digital pin 13 as output

digitalWrite(pump, On);
digitalWrite(pump, Off);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.