Skip to content

Instantly share code, notes, and snippets.

@mstum
Created July 1, 2020 20:53
Show Gist options
  • Save mstum/d8f8611e14f8efb5b40beb3aad8c0acf to your computer and use it in GitHub Desktop.
Save mstum/d8f8611e14f8efb5b40beb3aad8c0acf to your computer and use it in GitHub Desktop.
ATX Power Controller
* ATX Breakout Board is Amazon ASIN B01MY7KK80
Basically:
* Connect PS_ON and GND from the PSU to a Relay's NO connectors. Leave the NC connector just floating.
* Connect the Trigger Pin of the Relay to Pin 9 (RELAY_PIN) of the Arduino
* Connect the Power Button of the case to Pin 12 (PWRBTN_PIN) of the Arduino, and GND
* Connect the Power LED of the case to Pin 10 (PSUPOWER_LED) of the Arduino, and GND, using an appropriate resistor (e.g., 330 Ohm)
* Connect a Piezo Speaker to Pin 11 (SPKR_PIN) of the Arduino, and GND
I'm powering the Arduino from the +5VSB line of the Power Supply, by wiring it to 5V and GND.
Wiring power directly to 5V of the Arduino bypasses the regulator and isn't recommended, but the VIN pin has a 2V drop and
thus wouldn't work off of 5V from the Power Supply.
When the Arduino is powered on, it gives a short beep on the speaker.
When you hold down the power button for a bit (250ms), it toggles the Relay on.
When you let go and then hold down the power button for a bit (250ms), it toggles the Relay off.
If you keep holding the power button, nothing happens, you have to release it to turn it off afterwards.
There is a short melody played when powering on or off to signal what's happening.
#include "pitches.h"
struct Note
{
// See frequencies in pitches.h
int note;
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int duration;
};
Note StartupMelody[] = {
{NOTE_C4, 8},
{NOTE_D4, 4},
};
Note PowerOnMelody[] = {
{NOTE_C4, 8},
{NOTE_D4, 4},
{NOTE_G4, 8},
{NOTE_FS4, 4},
{NOTE_DS4, 4},
{NOTE_C4, 4},
{NOTE_D4, 4},
};
Note PowerOffMelody[] = {
{NOTE_C4, 4},
{NOTE_G3, 8},
{NOTE_G3, 8},
{NOTE_A3, 4},
{NOTE_G3, 4},
{NOTE_PAUSE, 4},
{NOTE_B3, 4},
{NOTE_C4, 4},
};
void PlayMelody(Note *melody, size_t melodyLength, int speakerPin)
{
for (int i = 0; i < melodyLength; i++)
{
Note note = melody[i];
int actualDuration = 1000 / note.duration;
tone(speakerPin, note.note, actualDuration);
delay(actualDuration * 1.30);
noTone(speakerPin);
}
tone(speakerPin, -1, 12);
delay(12);
noTone(speakerPin);
}
void PlayStartupMelody(int speakerPin)
{
size_t melodyLength = sizeof(StartupMelody) / sizeof(StartupMelody[0]);
PlayMelody(StartupMelody, melodyLength, speakerPin);
}
void PlayPowerOnMelody(int speakerPin)
{
size_t melodyLength = sizeof(PowerOnMelody) / sizeof(PowerOnMelody[0]);
PlayMelody(PowerOnMelody, melodyLength, speakerPin);
}
void PlayPowerOffMelody(int speakerPin)
{
size_t melodyLength = sizeof(PowerOffMelody) / sizeof(PowerOffMelody[0]);
PlayMelody(PowerOffMelody, melodyLength, speakerPin);
}
#define NOTE_PAUSE 0
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
// The Relay might trigger on either HIGH or LOW, change this if your Relay is different than mine
int RELAY_ON = HIGH;
int RELAY_OFF = LOW;
// Button to trigger the Relay
int PWRBTN_PIN = 12;
// LED to indicate if the Relay is on or off
int PSUPOWER_LED = 10;
// Signal Pin of the Relay
int RELAY_PIN = 9;
// + pin of the Speaker
int SPKR_PIN = 11;
#include "melodies.h"
// how long (ms) to hold the power button for it to register
long powerButtonDelayTime = 250;
bool lastPowerButtonPressed = false;
long powerButtonTimer = 0;
bool relayState = false;
bool handleButtonPress = true;
void setup() {
pinMode(SPKR_PIN, OUTPUT);
analogWrite(SPKR_PIN, 0);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, RELAY_OFF);
pinMode(PSUPOWER_LED, OUTPUT);
digitalWrite(PSUPOWER_LED, LOW);
pinMode(PWRBTN_PIN, INPUT_PULLUP);
PlayStartupMelody(SPKR_PIN);
}
void loop() {
bool buttonPressed = digitalRead(PWRBTN_PIN) == LOW;
long now = millis();
if (buttonPressed) {
if (handleButtonPress) {
if (lastPowerButtonPressed == false) {
lastPowerButtonPressed = true;
powerButtonTimer = now;
}
if ((now - powerButtonTimer) > powerButtonDelayTime) {
handleButtonPress = false;
togglePower();
}
}
} else {
if (lastPowerButtonPressed == true) {
lastPowerButtonPressed = false;
}
if (!handleButtonPress) {
handleButtonPress = true;
}
}
}
void togglePower() {
if (relayState) {
turnOffPower();
} else {
turnOnPower();
}
}
void turnOnPower() {
relayState = true;
PlayPowerOnMelody(SPKR_PIN);
digitalWrite(RELAY_PIN, RELAY_ON);
digitalWrite(PSUPOWER_LED, HIGH);
}
void turnOffPower() {
relayState = false;
PlayPowerOffMelody(SPKR_PIN);
digitalWrite(RELAY_PIN, RELAY_OFF);
digitalWrite(PSUPOWER_LED, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment