Latching Off-On pushbutton for portable electronics PCBs

Thread Starter

kowshik1729

Joined May 10, 2020
99
As the button is pressed to disable latch Q2 the power remains ON because U10 remains on as long as the button is pressed.
Is that what's happening?
Yes but that wasn't my problem. When I press the button ESP32 is getting 3.3V but the code inside it is not running.
 

sghioto

Joined Dec 31, 2017
8,634
Yes but that wasn't my problem. When I press the button ESP32 is getting 3.3V but the code inside it is not running.
Then the code is the problem.
My routine would be when the Disp button is first pressed and powers up the mcu the first line of code would enable the Q2 latch.
Then goes to a sub routine loop that waits for the button release before proceeding.
Now gpio18 is polled waiting for the next button press which goes to another sub routine loop releasing latch Q2 and when the button is released power will disconnect
 

Thread Starter

kowshik1729

Joined May 10, 2020
99
Then Q2 is not latched.
Yes, the reason Q2 is not latched because the code of esp32 is not running. Because at the very starting of my esp32 code I have written the code to turn the gpio connected to the base of transistor Q2 to turn on. I highly believe the esp32 core is resetting itself due to the current draw. But I don't know how to resolve it hahaha
 

Thread Starter

kowshik1729

Joined May 10, 2020
99
It's not any current draw unless you have something wired incorrectly.
The problem can be simple, the gpio connected to button is configured as INPUT with an internal pull up. But there's a diode present just before the IO, so I'm not sure what's causing the issue.

Btw the circuit I have there is from this video, nothing was changed.

 

Thread Starter

kowshik1729

Joined May 10, 2020
99
Can you do a test code to confirm?
Guys, a huge apologies from my side. It is INDEED a problem with my code, I was testing a firmware which has lot of other stuff like wifi related and some where it's using this exact GPIO pin.

I have written a clean code as below

Button test code:
#include "freertos/FreeRTOS.h"

#include "freertos/task.h"

#include "esp_system.h"

#include "driver/gpio.h"

#include "esp_log.h"

#define BUTTON_GPIO 18

#define POWER_LATCH 40

#define DISP_PWR_CTRL 16

static const char *TAG = "GPIO_TOGGLE";

void app_main() {

    // Configure the power latch GPIO as output

    gpio_config_t power_latch = {

        .pin_bit_mask = (1ULL << POWER_LATCH),

        .mode = GPIO_MODE_OUTPUT,

        .pull_up_en = GPIO_PULLUP_DISABLE,

        .pull_down_en = GPIO_PULLDOWN_DISABLE,

        .intr_type = GPIO_INTR_DISABLE

    };

    ESP_ERROR_CHECK(gpio_config(&power_latch));

    gpio_set_level(POWER_LATCH, 1);

    // Configure the button GPIO as input

    gpio_config_t button_gpio_config = {

        .pin_bit_mask = (1ULL << BUTTON_GPIO),

        .mode = GPIO_MODE_INPUT,

        .pull_up_en = GPIO_PULLUP_ENABLE,

        .pull_down_en = GPIO_PULLDOWN_DISABLE,

        .intr_type = GPIO_INTR_DISABLE

    };

    ESP_ERROR_CHECK(gpio_config(&button_gpio_config));

    // Configure the LED GPIO as output

    gpio_config_t led_gpio_config = {

        .pin_bit_mask = (1ULL << DISP_PWR_CTRL),

        .mode = GPIO_MODE_OUTPUT,

        .pull_up_en = GPIO_PULLUP_DISABLE,

        .pull_down_en = GPIO_PULLDOWN_DISABLE,

        .intr_type = GPIO_INTR_DISABLE

    };

    ESP_ERROR_CHECK(gpio_config(&led_gpio_config));

    int led_state = 0;

    int last_button_state = 1; // Assume button is not pressed initially

    while (1) {

        // Read the button state

        int button_state = gpio_get_level(BUTTON_GPIO);

        // Check if the button state has changed

        if (button_state == 0 && last_button_state == 1) {

            // Toggle the LED state

            led_state = !led_state;

            gpio_set_level(DISP_PWR_CTRL, led_state);

            ESP_LOGI(TAG, "Button pressed, LED toggled to %d", led_state);

            // Debounce delay

            vTaskDelay(pdMS_TO_TICKS(200)); // 200 ms delay

        }

        // Update the last button state

        last_button_state = button_state;

        // Small delay to avoid busy-waiting

        vTaskDelay(pdMS_TO_TICKS(10));

    }

}
and now everything works fine, sorry for the trouble and confusion caused, you guys are amazing with the help.

1734950703754.png
 
Last edited:

MisterBill2

Joined Jan 23, 2018
27,563
The switch requirements changed in post #26
OK, and quite correct. Of course, there do exist circuits to get around that issue, often called "auto-start circuits".
But in most industrial systems, not much is supposed to start up when power switches on. Safety rules.
 
Top