On/Off Power supply

Hello,

I need a circuit that can be turned ON and OFF depending on the case.

  1. Everything is OFF. I connect a 12V to the optocoupler circuit, and turn the power ON.
  2. When needed, I will disconnect the 12V rail from the optocoupler circuit
  3. The MCU is powered ON as it gets the power, and continues to set power ON for itself and other things
  4. Meanwhile, if I disconnect the 12V from the optocoupler, the MCU will allow the power supply to be ON
  5. After the condition is met, the MCU will cut OFF the power

Here is the schematic:

So, here you can see the btnIn1 where I will connect the 12V when needed. On onOffCut is connected digital pin from the MCU.

The question is, am I working in the right direction? Do I need to change the resistor values? Maybe some other approach?

The basic thing is, the setup should start working when I press the button with 12V, until the MCU says it is enough. The button might be a switch, so it could be a longer ON period on the optocoupler side.

This is not the final design. It can be changed if there are better solutions, suggestions, etc

The 5V pull-up on the ON/OFF pin should be 12V, as if the regulator is OFF, there will be no 5V, LOL.

That is true but now you have 12V on a MCU pin. Not a good idea.

What is this MCU?
Are the I/Os 5V or 3.3V?

You may consider this:

2 Likes

Nice way to burn an MCU.

I will use the ATtiny1614, or similar.

This schematic looks very good.

The regulator is OFF from R1 to the 12V. When I press the button, I will connect 12V to the btnIn, and thus, over the PC817, I will supply the GND to the ON/OFF pin, this way powering up the setup. Meanwhile, the MCU will boot up, write HIGH on the onOffCut pin, and move on to powering up the setup, no matter the button. When the condition is met, the MCU will write LOW, this way powering OFF the setup. The R3 will hold LOW the base of the transistor.

Did I understand this correctly?

Yes, perfectly.

1 Like

Thank you very much.

1 Like

You are welcome

The board arrived and is assembled. This is the schematic:

As you can see, there are some things I haven’t mentioned, such as the potentiometer and LEDs. More about them later.

The idea is that when the button is pressed, the board gets 12V on btnIn1 and two optocouplers. One powers up the board, and the other one tells the MCU if the button is released. When I press the button, the board will turn ON the relay as long as it is pressed. After the button is released, the board should stay powered for a while by the MCU.

Here is the sketch for the basic logic:

int pot   = 0;
int rly   = 1;
int led1  = 2;
int led2  = 3;
int led3  = 4;
int led4  = 5;
int in1   = 6;
int onOff = 7;
int inState;
void setup() {
  pinMode(onOff, OUTPUT);
  digitalWrite(onOff, HIGH);
  pinMode(rly, OUTPUT);
  digitalWrite(rly, LOW);
  pinMode(in1, INPUT);
}
void loop() {
  inState = digitalRead(in1);
  if (inState == 0){
    delay(2000);
    digitalWrite(onOff, LOW);
    }
}

So, when I press the button, the onOff line goes ON to power the board. The rly line goes ON to power the relay. The MCU listens to the in1 pin, and when it is LOW (the button is released), it waits 2 seconds, and turns the power OFF. Delay will be replaced with millis(). The potentiometer logic will be added later. But the problem here is a strange behavior of the board.

When I press the button (connect 12V with btnIn1) and release it after a second, it works as expected. It turns ON, waits 2 secs, and turns OFF. When I press the button a little bit longer, it gets stuck. The MCU stays ON, both the relay and the power. It doesn’t shut down.

What did I do wrong? The schematic, or the code? I don’t have the UART communication with the PC. The MCU ATtiny1614 is programmed by the JTAG2UPDI programmer.

There are cases when I hold the button for a few seconds more and release it, it shuts down that very second.

Just changed delay() with millis(), and get the same.

if (inState == 0){
    if ((millis() - lastTime) > timerDelay){
      digitalWrite(onOff, LOW);
      lastTime = millis();
      }
    }

Hi, @dekip
Thanks for your PCB circuit, but can you know draw a schematic with everything connected and the peripherals, such as switches/buttons included.
At the moment you have H1 and U6 etc, what are they connected to?
You need to draw a proper signal flow schematic to make troubleshooting convenient.

Can you also post images of your PCB board?
So we can see your component layout.

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

See my comments

void setup() {
  pinMode(onOff, OUTPUT);
  digitalWrite(onOff, HIGH);
  pinMode(rly, OUTPUT);
  digitalWrite(rly, LOW);
  pinMode(in1, INPUT);
  delay (500); // Wait for regulator to start up
}
void loop() {
  inState = digitalRead(in1);
  if (inState == 0){
    delay(2000);
    digitalWrite(onOff, LOW);
	while(1); // stay here forever, don't loop
    }
}

What BOD level are you using?

H1 is obviously a programming header.

U4, U6, and U9 are relay pins. There is nothing on them right now.

Gotta do some more tests, but it looks like it will work.

However, it looks like when the onOff pin goes OFF, there is some current left. As if this C4 cap holds for a split second enough for the MCU to boot up. Once the PWR LED stayed ON for a split second, but the MCU was dead. This LED shouldn’t work either, as it uses 5V just as the MCU.

PS.

Tried this while(1) trick with millis(), but didn’t manage to make it work. The delay() is ok, but it will not be 2 secs, but a few minutes. So if after 1 minute the button is pressed, the MCU will not notice it to go out of the if() loop.

PS2.

The BOD is left as it is. I haven’t changed a thing. I don’t use a bootloader for this MCU.

Are you using attinycore?
If yes then you have options for the BOD level.

As if this C4 cap holds for a split second enough for the MCU to boot up.

Not sure what happens to the buck output when the enable goes high, it may have a spike that causes the MCU to reboot. Adding a 1K resistor across C4 may help
You may also have to add another delay at the beginning of setup() to allow the cap to discharge in case it does reboot.

I have added a 500ms delay at the beginning of the setup. It did the trick. But maybe it is a long time, I will try with smaller values. This means I will go without the resistor across the C4.

Yes, I am using the megaTinyCore package. I forgot to mention that. Sorry.

Now, it works partially as it should. When I press the button for a sec, it works. When I press the button for a longer time, it works. When I press the button, release it, and press it again, it does not work. In this case, I press the button, and the relay is ON. I release it and press it again, the relay is still ON. But right after I release it the second time, it goes OFF. It does not reenter the if loop, as it has been held by the while(), I believe.

It should work as some kind of a delayed switch. With the button, I should control it. The button could sometimes be pressed for a short time, sometimes for a longer time, and sometimes it could be pressed several times within the delay. Only when there is no pressed button within the delay, it should go OFF.

I hope I made it clear.

Should have mentioned that in your original post, I might have suggested a different approach.
What BOD level have you selected?