First time working with real power...

So I have fallen in love with my Arduino and have been doing multiple things with servo's, motor's, and LED's, but all of these things have one thing in common. Nothing over 9v batteries. So when I make a mistake or am being careless, there is only a slight chance I burn out the Arduino, or my nervous system...

So I am taking baby steps toward wall power. This afternoon's project is going to be relatively simple (in my mind).

My computer currently sits under my desk and turned away from me. To make life easier I have a pushbutton wired to my computer PWR and GND and run the wire up to my desk so that I can power it on and off without getting under/behind the desk.

My first venture into the world of wall power is to wire this setup to an arduino and successfully not fry the arduino. For the programming, I plan to just use the Pushbutton example, albeit a little modified from the original LED example.

int ComputerPin = 13; // choose the pin for the computer
int ButtonPin = 7;   // choose the input pin (for a pushbutton)
int ButtonState = 0;     // variable for reading the button status

void setup() {
  pinMode(ComputerPin, OUTPUT);  // declare Computer as output
  pinMode(ButtonPin, INPUT_PULLUP);    // declare pushbutton as input
}

void loop(){
 ButtonState = digitalRead(buttonPin);  // read ButtonState
  if (ButtonState == HIGH) {         // check if the input is HIGH (button released)
    digitalWrite(ButtonPin, HIGH);  // turn Computer OFF
  } else {
    digitalWrite(ButtonPin, LOW);  // turn Computer ON
  }
}

My only questions would be, how to wire it correctly without sending 110V's straight to the arduino (In the LED example its a built in LED with internal power so the wiring is a little more simple), and I believe most TV's are momentary switches, so after I push and release the button, it should stay on without me having to hold it...but just in case...

Uh - first off, your code doesn’t look quite right; first off, your “digitalRead” is reading “buttonPin” - but you only declare (and init as an input) “ButtonPin” - C is case-sensitive, so those are two different variables, and one (“buttonPin”) isn’t even declared - so I am not sure how this is going to work.

Secondly, you then try to -write- to “ButtonPin” - which was declared and initialized as an input! You can’t write to an input - you’re going to have problems.

Ok - so that’s the basics - but here’s what you want to do: That button on your computer? Unless your machine is fairly old (ie - manufactured prior to the ATX standard), then all that button is doing is momentarily shorting out a couple of pins on the motherboard (it doesn’t need to be held) - so you aren’t really controlling any kind of mains voltage (I’m surprised you don’t know this - since you custom wired your own button, and I can only surmise that since you have your PC oriented in such a weird fashion, that you are likely always fiddling with the cables in the back, meaning you likely have more than a bit of experience with PC hardware and how it works - anyhoo…)

So - all you need to do is simulate that button press. To make life simple, and to isolate the Arduino from the PC hardware - just replace the button with the NO (normally open) contacts of a relay, then control the relay with the Arduino. The relay only needs to be “pulsed” - that is, when the button is pressed (momentarily), turn the relay on, wait a bit (say 100ms), then turn the relay off. While you could keep the contacts of the relay closed (it won’t hurt anything) - it isn’t necessary.

That said - no matter how you look at things, you (well, the Arduino) won’t know whether the computer is on or off. I don’t know if this is important to you or your application. If it is, then probably the best way to monitor things would be to use the power LED from the PC - connect it thru an optocoupler (again, to isolate the PC from the Arduino), so you can see the status of the PC - when the LED is lit (ie - the optocoupler is output is HIGH or LOW - depending on the type of optocoupler you select - read the datasheet) - then the PC is on.

I leave the task of how to connect and control the relay, and the optocoupler up to your research. Good luck. :slight_smile:

cr0sh:
Uh - first off, your code doesn't look quite right; first off, your "digitalRead" is reading "buttonPin" - but you only declare (and init as an input) "ButtonPin" - C is case-sensitive, so those are two different variables, and one ("buttonPin") isn't even declared - so I am not sure how this is going to work.

Secondly, you then try to -write- to "ButtonPin" - which was declared and initialized as an input! You can't write to an input - you're going to have problems.

Ok - so that's the basics - but here's what you want to do: That button on your computer? Unless your machine is fairly old (ie - manufactured prior to the ATX standard), then all that button is doing is momentarily shorting out a couple of pins on the motherboard (it doesn't need to be held) - so you aren't really controlling any kind of mains voltage (I'm surprised you don't know this - since you custom wired your own button, and I can only surmise that since you have your PC oriented in such a weird fashion, that you are likely always fiddling with the cables in the back, meaning you likely have more than a bit of experience with PC hardware and how it works - anyhoo...)

So - all you need to do is simulate that button press. To make life simple, and to isolate the Arduino from the PC hardware - just replace the button with the NO (normally open) contacts of a relay, then control the relay with the Arduino. The relay only needs to be "pulsed" - that is, when the button is pressed (momentarily), turn the relay on, wait a bit (say 100ms), then turn the relay off. While you could keep the contacts of the relay closed (it won't hurt anything) - it isn't necessary.

That said - no matter how you look at things, you (well, the Arduino) won't know whether the computer is on or off. I don't know if this is important to you or your application. If it is, then probably the best way to monitor things would be to use the power LED from the PC - connect it thru an optocoupler (again, to isolate the PC from the Arduino), so you can see the status of the PC - when the LED is lit (ie - the optocoupler is output is HIGH or LOW - depending on the type of optocoupler you select - read the datasheet) - then the PC is on.

I leave the task of how to connect and control the relay, and the optocoupler up to your research. Good luck. :slight_smile:

Wow, thanks for the time to post all that, I will try to dissect...I am at school without my IDE so the code isnt going to be the actual code. I just copy and pasted and updated to fit my situation since some people here find it lazy when there is no code posted at all. So I used the LED pushbutton example from the site and just updated the variables, so the typos and capitalization will be corrected.

For the computer, its fairly knew, I updated to an Asus Sabretooth a couple years back when I realized that my AMD motherboard wasn't cutting it anymore. For the button, it was pretty simple, here's the PWR, heres the GND, this button connects them and boom we have power. The only reason I mentioned the holding thing was not because I didn't understand the button, just more of how the arduino was going to behave.

Don't let me fool you into thinking I know what im doing when it comes to computers. The pc is turned away from me because every since I loaded the motherboard, the mouse connected to the computer stops working after a reboot so i have to unplug it from usb1 and plug it into usb2. Then vice versa on the next reboot -_-.

I was considering what you said with the relay...well honestly I had just finished a project with a transistor and how they worked and was thinking about that route. But relays are similiar. Essentially providing the small amount of current to bridge the connection without having the 110v's coming through the arduino itself. (The less fried arduino's I have, the happier I am).

Then after that im not worried about if it knows that its on or off. So I am safe there. I appreciate your help though. Seems like I shouldn't have to worry too much when I go to connect it. I just always fear worse case scenerio as soon as low voltage batteries are not the only thing in play.

Wait a second -

If I understand it correctly, you've got a pushbutton wired in parallel with the power switch in your PC? If that's the case, and we're talking an ATX PSU, then it's not a latching switch, but a momentary one. Hence, you DO need to open the connection again, otherwise the motherboard is going to think you're keeping the button pressed.

syntaxterror:
Wait a second -

If I understand it correctly, you've got a pushbutton wired in parallel with the power switch in your PC? If that's the case, and we're talking an ATX PSU, then it's not a latching switch, but a momentary one. Hence, you DO need to open the connection again, otherwise the motherboard is going to think you're keeping the button pressed.

Yea, its a momentary, but the connection will be open again, that's why I put the code in the void loop. It will check if the button is pressed, see that it is, turn on the computer, check if the button is pressed again, see that it isn't, open the connection :).

Aye, I see what your digitalWrites will do; simply mimic the switch. I was just wondering if you intended it that way since the comments seem to suggest otherwise. It’s not so much “turn computer on” and “turn computer off” as it is “toggle computer on/off” and “do nothing”.

No

  1. You press the button
  2. Arduino reads the button as pressed
  3. Arduino turns the computer on
  4. You release the button
  5. Arduino reads the button as released
  6. Arduino turns the computer off

BTW, it isn't the void loop, it is the loop function.

An ATX power supply does not have a latching switch input. It is momentary, and therefore will not turn itself off when the button is released.