Control PC power with arduino

Introduction:
I would like to be able to turn on/off PC by just switching on/off the switch on power extension cord with switch.


Turning on is simple - just switch on the power extension switch ! :slight_smile:
The issue is with turning off - all PCs needs some time to shut down properly :expressionless:
This idea is similar to the way how UPS communicates with PC in case of power failure.

My assumptions:

  • arduino will control the relay module which will power PC
  • arduino will be connected to mains by power adapter and to PC with USB
  • power cord switch will switch on/off the arduino power adapter not the PC
  • arduino will auto-select the power source between power adapter and USB

My idea:

  1. Turning PC on:
    a) switch on arduino power adapter
    b) arduino starts and by default puts relay to ON mode and PC boots up

  2. Turning PC off (this is tricky part and I'm not sure if it will work at all)
    a) switch off arduino adapter
    b) arduino switch itself to USB power source using built into board auto-select functionality
    c) arduino "senses" that power adaptor is switched off
    d) arduino sends message over USB (serial) to PC to power itself off
    e) arduino goes into sleep for the time needed for PC to shut down
    f) arduino wakes up (at this point PC is already down)
    g) arduino set relay to OFF mode

My questions:
ad. (2b) - will arduino switch silently to USB power after switching off power adaptor or it will shut down ? reboot ? or maybe is there some shield which allows for safe switching between power sources ?
ad. (2c) - how to do it ? is there a way to read some register in memory or something similar to get information what type of power source is currently used - USB or power adapter ? or maybe I can check the voltage on the power jack and pass it to arduino pin and measure in this way ?
ad. (2d) this is fine, should be quite easy, just need to write some script which will wait for such message on PC and if received power PC off
ad. (2e) this step is optional as some PC will after shut down power off all USB ports - in this way arduino will just switch off and relay will go to OFF mode
ad. (2f) only if PC is not powering off USB ports arduino will wake up and set relay into OFF mode

Every motherboard has a two pin header which is normally connected to the momentary power button on the case that if you momentarily connect the header, the motherboard send the shutdown command to the OS which then does an OS shutdown and then orders the motherboard to turn the power supply off. I would use that capability rather than using a serial port and having to write a driver. If the header is closed for about 1 second continuously that causes the motherboard to shut down the power supply without any further action from the OS.

JoeN:
Every motherboard has a two pin header which is normally connected to the momentary power button on the case that if you momentarily connect the header, the motherboard send the shutdown command to the OS which then does an OS shutdown and then orders the motherboard to turn the power supply off. I would use that capability rather than using a serial port and having to write a driver. If the header is closed for about 1 second continuously that causes the motherboard to shut down the power supply without any further action from the OS.

You want to use something like an optocoupler between the Arduino and the circuit for the PC shutdown, so that there isn't an electrical connection between the Arduino and the PC. An optocoupler (or optoisolator) is an integrated circuit with a LED and a light sensor inside. When you light the LED via the input power pins, the light sensor completes the circuit on the output side. Terry King has the following on his wiki for integrated circuits: http://arduino-info.wikispaces.com/Popular-ICs

In another forum, people have suggested I put resistors on the input for the optocoupler, otherwise I would run the risk of blowing out the internal LED.

MichaelMeissner:

JoeN:
Every motherboard has a two pin header which is normally connected to the momentary power button on the case that if you momentarily connect the header, the motherboard send the shutdown command to the OS which then does an OS shutdown and then orders the motherboard to turn the power supply off. I would use that capability rather than using a serial port and having to write a driver. If the header is closed for about 1 second continuously that causes the motherboard to shut down the power supply without any further action from the OS.

You want to use something like an optocoupler between the Arduino and the circuit for the PC shutdown, so that there isn't an electrical connection between the Arduino and the PC. An optocoupler (or optoisolator) is an integrated circuit with a LED and a light sensor inside. When you light the LED via the input power pins, the light sensor completes the circuit on the output side. Terry King has the following on his wiki for integrated circuits: http://arduino-info.wikispaces.com/Popular-ICs

In another forum, people have suggested I put resistors on the input for the optocoupler, otherwise I would run the risk of blowing out the internal LED.

Thanks you for answers but I used small simplification in my description - I used term PC while I'm planning to build this safe-powering-off device for specific device NAS Network-attached storage - Wikipedia which is not standard PC and doesn't have PINs on motherboard which I could use to shut down the device, but this is not a problem at all, still I can send message over serial to order NAS to shut down, but what worries me are questions (2b) and (2c).

bedek:
My idea:
2. Turning PC off (this is tricky part and I'm not sure if it will work at all)
a) switch off arduino adapter
b) arduino switch itself to USB power source using built into board auto-select functionality
c) arduino "senses" that power adaptor is switched off
d) arduino sends message over USB (serial) to PC to power itself off
e) arduino goes into sleep for the time needed for PC to shut down
f) arduino wakes up (at this point PC is already down)
g) arduino set relay to OFF mode

My questions:
ad. (2b) - will arduino switch silently to USB power after switching off power adaptor or it will shut down ? reboot ? or maybe is there some shield which allows for safe switching between power sources ?\

I believe the UNO just uses whatever power source has a higher voltage and switches automatically.

bedek:
ad. (2c) - how to do it ? is there a way to read some register in memory or something similar to get information what type of power source is currently used - USB or power adapter ? or maybe I can check the voltage on the power jack and pass it to arduino pin and measure in this way ?

I've not done it, but I believe you could wire up something to the external power before connecting it to the Arduino that would drive an optocoupler or relay that feeds into an Arduino input pin. Another way would be to use a LM393/LM293/LM339 that compares the two voltages from the VIN and 5v pins and returns LOW/HIGH which can be read with a digital pin. Terry King has a series of tutorials on various basic integrated circuits here: http://arduino-info.wikispaces.com/Popular-ICs

Note, the UNO has a 'feature' that may be problematical to you, in that when the serial port is opened, the Arduino gets reset: Quoting from the fine manual:
This setup has other implications. When the Uno is connected to either a computer running Mac OS X or Linux, it resets each time a connection is made to it from software (via USB). For the following half-second or so, the bootloader is running on the Uno. While it is programmed to ignore malformed data (i.e. anything besides an upload of new code), it will intercept the first few bytes of data sent to the board after a connection is opened. If a sketch running on the board receives one-time configuration or other data when it first starts, make sure that the software with which it communicates waits a second after opening the connection and before sending this data.

I give you my simple way to implement it. To power it on through 'Wake On LAN' (Wake-on-LAN - Wikipedia), and to power it off with a small service program which runs on PC and receives a specific UDP(or TCP) message then to shutdown the machine. All you have to do is setting your BIOS and OS on PC and writing a simple PC service program, and all you have to have is a network shield.

With these, you can even control the power of your PC through Internet remotely. :stuck_out_tongue:

MichaelMeissner:
I believe the UNO just uses whatever power source has a higher voltage and switches automatically.

But is it going to be quick enough so it will not reset accidentally while switching between sources ?
(I will test it anyway in few days when I will get my arduino)

MichaelMeissner:
I've not done it, but I believe you could wire up something to the external power before connecting it to the Arduino that would drive an optocoupler or relay that feeds into an Arduino input pin. Another way would be to use a LM393/LM293/LM339 that compares the two voltages from the VIN and 5v pins and returns LOW/HIGH which can be read with a digital pin. Terry King has a series of tutorials on various basic integrated circuits here: http://arduino-info.wikispaces.com/Popular-ICs

Thnx, I'm total noob in electronics I suppose that the easiest thing you be just use another relay which will be set to ON state when the power adapter is on and I will just read state of the relay using one of arduino PINs ?

MichaelMeissner:
Note, the UNO has a 'feature' that may be problematical to you, in that when the serial port is opened, the Arduino gets reset: Quoting from the fine manual...

I would like to use Arduino connected to USB in Linux machine so as far as I understand setting correct serial port using following command should block reset functionality of Arduino:
stty -F /dev/ttyUSB0 cs8 115200 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts
from: Arduino Playground - LinuxTTY

wdzhou:
I give you my simple way to implement it. To power it on through 'Wake On LAN' (Wake-on-LAN - Wikipedia), and to power it off with a small service program which runs on PC and receives a specific UDP(or TCP) message then to shutdown the machine. All you have to do is setting your BIOS and OS on PC and writing a simple PC service program, and all you have to have is a network shield.

With these, you can even control the power of your PC through Internet remotely. :stuck_out_tongue:

I don't think this is something what would meet my requirements, the target is to "fully disconnect from mains" the whole system and not to put in sleep/wait mode.
Additionally I still need to send those UDP messages - I assume from arduino which would required using ethernet shield :frowning:
And last but not the least - I'm not really dealing in my project with "real" PC, it's NAS running on linux, this is quite specific, that device is not supporting wake-on-lan.
Nevertheless thanks for your thoughts !

bedek:

MichaelMeissner:
I believe the UNO just uses whatever power source has a higher voltage and switches automatically.

But is it going to be quick enough so it will not reset accidentally while switching between sources ?
(I will test it anyway in few days when I will get my arduino)

I just did it, switching from 9 volts via wall wart to USB and back. As long as one of the two power supplies are providing power, it will not reset. According to Nick Gammon's page on the Uno R3, it will automatically switch to the external power if the external power has at least 6.6 volts (at least 7 volts are recommended so that the Uno can deliver 5 volts to its pins): Gammon Forum : Electronics : Microprocessors : Arduino Uno Rev3 pinouts photo

bedek:
Thnx, I'm total noob in electronics I suppose that the easiest thing you be just use another relay which will be set to ON state when the power adapter is on and I will just read state of the relay using one of arduino PINs ?

You probably don't want a relay, since those are fairly slow acting, but an optocoupler is essentially a relay.

bedek:
I would like to use Arduino connected to USB in Linux machine so as far as I understand setting correct serial port using following command should block reset functionality of Arduino:
stty -F /dev/ttyUSB0 cs8 115200 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts
from: Arduino Playground - LinuxTTY

It has been awhile since I've had to deal with Linux stty, but that looks reasonably correct. Other ways to do it include a custom cable that doesn't pass the reset signal or to use software serial on different ports.

MichaelMeissner:
I just did it, switching from 9 volts via wall wart to USB and back. As long as one of the two power supplies are providing power, it will not reset. According to Nick Gammon's page on the Uno R3, it will automatically switch to the external power if the external power has at least 6.6 volts (at least 7 volts are recommended so that the Uno can deliver 5 volts to its pins): Gammon Forum : Electronics : Microprocessors : Arduino Uno Rev3 pinouts photo

Thnx, so this part will work fine.

MichaelMeissner:

bedek:
Thnx, I'm total noob in electronics I suppose that the easiest thing you be just use another relay which will be set to ON state when the power adapter is on and I will just read state of the relay using one of arduino PINs ?

You probably don't want a relay, since those are fairly slow acting, but an optocoupler is essentially a relay.

I don't mind about the speed as that relay will just be used to inform arduino that power adapter was trunde off, but optocoupler looks as more natural solution - as I said I'm total
noob in electronics so wish me luck !

MichaelMeissner:

bedek:
I would like to use Arduino connected to USB in Linux machine so as far as I understand setting correct serial port using following command should block reset functionality of Arduino:
stty -F /dev/ttyUSB0 cs8 115200 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts
from: Arduino Playground - LinuxTTY

It has been awhile since I've had to deal with Linux stty, but that looks reasonably correct. Other ways to do it include a custom cable that doesn't pass the reset signal or to use software serial on different ports.

So if the trick with the setting serial port on linux will not work then I could use either:
a) special cable which will not pass the reset signal - could you give me some links where I can buy such cable ?
b) software serial - could you confirm I understand this solution correctly ?
This solution will require following steps:
-- use PINs (0/1) of arduino to be used for serial communication
-- to communicate using this solution I will need to use following library http://arduino.cc/en/Reference/SoftwareSerial
-- on the linux machine side I need to buy USB-to-Serial converter to be able to communicate with PINs (0/1) of arduino
-- main USB will only be used as second power source in case when externa power adapter will be switched off and there will be no communication established over this connection
and in this way reset functionality or arduino will never be triggered

bedek:

MichaelMeissner:

bedek:
Thnx, I'm total noob in electronics I suppose that the easiest thing you be just use another relay which will be set to ON state when the power adapter is on and I will just read state of the relay using one of arduino PINs ?

You probably don't want a relay, since those are fairly slow acting, but an optocoupler is essentially a relay.

I don't mind about the speed as that relay will just be used to inform arduino that power adapter was trunde off, but optocoupler looks as more natural solution - as I said I'm total
noob in electronics so wish me luck !

I relation to distinguishing between power sources I found interesting article here:

http://code.google.com/p/tinkerit/wiki/SecretVoltmeter

So as I can understand I can measure the voltage of the power source, assuming that USB power voltage is about 5V and I will use external power adapter producing 12V I should be able to use "SecretVoltmeter" approach to recognise which power source is currently connected ? Do you think that would work ?

JoeN:
Every motherboard has a two pin header which is normally connected to the momentary power button on the case that if you momentarily connect the header, the motherboard send the shutdown command to the OS which then does an OS shutdown and then orders the motherboard to turn the power supply off. I would use that capability rather than using a serial port and having to write a driver. If the header is closed for about 1 second continuously that causes the motherboard to shut down the power supply without any further action from the OS.

That works fine and can simplify the project (for example there will be no serial communication so auto-reset issue of arduino goes away) but I'm not using standard PC, it's NAS Network-attached storage - Wikipedia device, my one don't have any button (yes - none) so it can only be controlled over usb or ethernet.

bedek:

bedek:

MichaelMeissner:

bedek:
Thnx, I'm total noob in electronics I suppose that the easiest thing you be just use another relay which will be set to ON state when the power adapter is on and I will just read state of the relay using one of arduino PINs ?

You probably don't want a relay, since those are fairly slow acting, but an optocoupler is essentially a relay.

I don't mind about the speed as that relay will just be used to inform arduino that power adapter was trunde off, but optocoupler looks as more natural solution - as I said I'm total
noob in electronics so wish me luck !

I relation to distinguishing between power sources I found interesting article here:

Google Code Archive - Long-term storage for Google Code Project Hosting.

So as I can understand I can measure the voltage of the power source, assuming that USB power voltage is about 5V and I will use external power adapter producing 12V I should be able to use "SecretVoltmeter" approach to recognise which power source is currently connected ? Do you think that would work ?

You can't use SecretVoltmeter , it measure the voltage of the AVcc, which in both case all 5V.

BillHo:

bedek:

bedek:

MichaelMeissner:

bedek:
Thnx, I'm total noob in electronics I suppose that the easiest thing you be just use another relay which will be set to ON state when the power adapter is on and I will just read state of the relay using one of arduino PINs ?

You probably don't want a relay, since those are fairly slow acting, but an optocoupler is essentially a relay.

I don't mind about the speed as that relay will just be used to inform arduino that power adapter was trunde off, but optocoupler looks as more natural solution - as I said I'm total
noob in electronics so wish me luck !

I relation to distinguishing between power sources I found interesting article here:

Google Code Archive - Long-term storage for Google Code Project Hosting.

So as I can understand I can measure the voltage of the power source, assuming that USB power voltage is about 5V and I will use external power adapter producing 12V I should be able to use "SecretVoltmeter" approach to recognise which power source is currently connected ? Do you think that would work ?

You can't use SecretVoltmeter , it measure the voltage of the AVcc, which in both case all 5V.

Well, looks like this SecretVoltmeter is used in this project:
http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/
where you can test the voltage of the battery powering the arduino, from that article:
"
...
You could also use it to determine if you are connected to a power source or running from batteries.
...
"
so maybe there is some change still ?!

bedek:

BillHo:

bedek:
I relation to distinguishing between power sources I found interesting article here:

Google Code Archive - Long-term storage for Google Code Project Hosting.

So as I can understand I can measure the voltage of the power source, assuming that USB power voltage is about 5V and I will use external power adapter producing 12V I should be able to use "SecretVoltmeter" approach to recognise which power source is currently connected ? Do you think that would work ?

You can't use SecretVoltmeter , it measure the voltage of the AVcc, which in both case all 5V.

Well, looks like this SecretVoltmeter is used in this project:
http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/
where you can test the voltage of the battery powering the arduino, from that article:
"
...
You could also use it to determine if you are connected to a power source or running from batteries.
...
"
so maybe there is some change still ?!

Yes, may be 5% to 10% of 5V.

BillHo:

bedek:

BillHo:

bedek:
I relation to distinguishing between power sources I found interesting article here:

Google Code Archive - Long-term storage for Google Code Project Hosting.

So as I can understand I can measure the voltage of the power source, assuming that USB power voltage is about 5V and I will use external power adapter producing 12V I should be able to use "SecretVoltmeter" approach to recognise which power source is currently connected ? Do you think that would work ?

You can't use SecretVoltmeter , it measure the voltage of the AVcc, which in both case all 5V.

Well, looks like this SecretVoltmeter is used in this project:
http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/
where you can test the voltage of the battery powering the arduino, from that article:
"
...
You could also use it to determine if you are connected to a power source or running from batteries.
...
"
so maybe there is some change still ?!

Yes, may be 5% to 10% of 5V.

So it looks like it's is not reliable method to test if arduino is powered from power adapter or from USB.
Thread bellow contains discussion which sums up the problem and advices to use "voltage divider" as the correct solution.

http://arduino.cc/forum/index.php/topic,107860.0.html

Hi, finally I accomplished my project ! :slight_smile:
Big thanks to the forum users for the help !
Here is the link:
https://code.google.com/p/mpd-lirc/wiki/PowerSwitch
Please send me any comments and suggestion how to improve my project ! :slight_smile: