How to set GPIO as a input and output

Good Evening guys. I am asking about how to set GPIO as a input and output. My Board is ESP32 Wroom and I am still newbie in this part. So my company ask me to do this one~ I have no idea to do this
So I hope someone else can teach me about this one~ Thanks You So Much

I am not sure why you would want to do this. Can you please explain the reason for doing it ? What is connected to the pin ?

Note that you can read the state of a pin set as an OUTPUT with no special code needed

Did you mean set GPIO both INPUT and OUTPUT at the same time? - it is impossible, I think...

The ESP32 has a variety of general-purpose input/output (GPIO) pins that can be configured as inputs or outputs. To use the GPIO pins as inputs or outputs, you will need to use a library that provides an API for interacting with the ESP32's GPIOs. The most commonly used library for this purpose is the ESP-IDF (Iot Development Framework) library, which is developed and maintained by Espressif (the company that makes the ESP32).

Here is an example of how to configure a GPIO pin as an output using the ESP-IDF library:

#include "driver/gpio.h"

#define LED_GPIO 2

void app_main() {
    // Configure the GPIO as an output
    gpio_pad_select_gpio(LED_GPIO);
    gpio_set_direction(LED_GPIO, GPIO_MODE_OUTPUT);

    // Turn on the LED
    gpio_set_level(LED_GPIO, 1);
}

This code configures the GPIO pin 2 as an output and set it to High, In this case an LED connected to the Pin2 will turn on. You can use similar logic to configure any other pin as output or input.

Here is an example of how to configure a GPIO pin as an input:

#include "driver/gpio.h"

#define BUTTON_GPIO 0

void app_main() {
    // Configure the GPIO as an input
    gpio_pad_select_gpio(BUTTON_GPIO);
    gpio_set_direction(BUTTON_GPIO, GPIO_MODE_INPUT);

    // Read the state of the button
    int button_state = gpio_get_level(BUTTON_GPIO);
    if (button_state == 0) {
        // Button is pressed
    } else {
        // Button is not pressed
    }
}

This code configures the GPIO pin 0 as an input and reads the state of the button connected to the pin, The state of the button can be read by calling the gpio_get_level() function. The function will return 0 if the button is pressed, and 1 if it is not pressed.

In addition to the basic input and output functionality, the ESP-IDF library also provides more advanced features such as configurable pull-up and pull-down resistors, and the ability to generate interrupts when the state of an input pin changes. It's also possible to use other libraries like Arduino library which make things easier with more functions.

using pinMode()

1 Like

thx

Hmmm if only GPIO for input also can right ?

mode: INPUT, OUTPUT, or INPUT_PULLUP. 

If the pin is set as an INPUT using pinMode() then you can only use it to read input signals

Please explain why you are asking these questions. What exactly have you been asked to do by your company ?

You are trying to be fast by posting quick with short text.
As you have experienced now

Trying to be fast in fact slows things down.
Now you are 3 hours later and even the nature of your question is still unclear.

How many rounds of this annoying ping-pong game of
posting quick - asking back
posting quick - asking back
posting quick - asking back
posting quick - asking back
posting quick - asking back
posting quick - asking back
do you wanna play?

it will be much more efficient if you invest time into describing in detail what you really want to do

This is neither WhatsApp-ing nor Snapchat-ing, nor Instagramm-ing
Though if you would really setup a short video that brings everything to the point. This might work as question but I'm pretty sure that producing this high-quality video will take 50 times more than writing the detailed text

Hello f3lix0928

Go to your sketch, select the word "pinMode" and type Ctrl-Shift+F from the keyboard and you will immediately get the Arduino reference related.

Have a nice day and enjoy coding in C++.

If you set the GPIO to be INPUT_PULLUP, it will ouput a weak high signal which might be enough to drive a mosfet gate. But it's still an input, so if you ground it through a pushbutton switch, it will go low. Here's an example of a soft switch power control using that technique. The pushbutton originally turns on the power, but then can be used as an input after that. Then in the end the GPIO can be switched to an output to turn off the power.

1 Like

or ...

INPUT_PULLDOWN

Hi @f3lix0928

Can you tell us the application and why you need to do this, apart from

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

and not answering questions that we ask

LoL who wanna ply u can even skip my question no need said like that.

1 Like

Get it Thx Mate

Useful bro thanks alot

INPUT_PULLDOWN means let the elec go on to the input right?

Arduino application and IOT Project