10 GPIO functions in Embedded Programming

Introduction : GPIO is the short form for General Purpose Input/Output. It is type of programmable pin found on microcontrollers and processors that can be configured as either input or output. When used as input, GPIO pin reads digital signals such as button presses or sensor outputs. When used as output, GPIO can send digital signals to control devices like LEDs, relays or buzzers.

GPIOs are fundamental for interfacing a microcontroller with the external world. It helps to enable basic functions such as turning components on or off, receiving user input or communicating with other hardware. Their versatility and simplicity make GPIOs essential building blocks in embedded systems and electronics projects.

10 GPIO applications

Following are ten applications of GPIO in embedded domain specially using ESP32 microcontroller.

1. Controlling LEDs :

GPIO pins are controlled to high or low values to turn LEDs in ON or OFF states. Examples : Status indication, blinking patterns or testing parts or loops or if/else conditions of software codes.

digitalWrite(2, HIGH); // Turn ON LED on GPIO2

2. Reading Push Buttons : The gpio pin is connected with push button output. When it gets pressed, gpio pin will get high or low level. This level is consecutively read by the microcontroller or microprocessor program. Use case : User input, mode selection, device power toggle.

int buttonState = digitalRead(4); // Read button on GPIO4

3. Interfacing with Relays : GPIO pin can be interfaced with relay. GPIO pin can be used to control relay in order to switch AC appliances. Use case : Home Automation, Industrial control.

digitalWrite(5, HIGH); // Activate relay connected to GPIO5

4. Driving Buzzer : When connected with buzzer or beeper, it generates ON/OFF pulses to make sound. Use cases : Alarms, notifications, feedback signals etc.

tone(15, 1000); // Play 1kHz tone on GPIO15

5. Reading Sensors : It can be used to read logic levels from sensors such as PIR motion sensors, IR sensors, door magnetic sensors etc. Use case : Security systems, automation

int motion = digitalRead(13); // Motion sensor on GPIO13

6. Software based PWM : GPIO pin can be used as output. Here it can simulate PWM using ledcWrite() for dimming LEDs or speed control of DC motors. Use case : LED brightness control, fan speed control etc.

ledcAttachPin(18, 0);  // PWM on GPIO18
ledcWrite(0, 128);     // 50% duty cycle

7. Generating Interrupts : In hardwired interrupt, when it is interfaced with some external hardware, it can detect rising/falling edge which triggers ISR code part. Use case : Real time events, doorbells, capacitive touch inputs.

attachInterrupt(digitalPinToInterrupt(14), isrFunction, FALLING);

8. I2C/SPI Bus Implementation : GPIO can be used to implement SPI or I2C bus protocols provided they are not supported on specific hardware. In this case, specific pins are programmed to be used as I2C or SPI. Use case : Interface with sensors, displays (OLED, LCD), memory chips, etc.

Wire.begin(21, 22); // SDA, SCL

9. Capacitive Touch Sensing : Dedicated touch sensitive GPIOs are supported by some microcontrollers such as ESP32. Use case : Touch switches, sliders, keypads without mechanical components.

int touchVal = touchRead(T0); // Read touch pad on GPIO4 (T0)

10. Bit-Banging Protocols :

Manually toggle GPIOs to implement communication protocols like 1-Wire, WS2812 LEDs, etc. Use case: Custom protocol handling or timing-sensitive LED strips.

Adafruit_NeoPixel strip(1, 12, NEO_GRB + NEO_KHZ800); // GPIO12

Conclusion : As studied, in embedded programming, GPIO functions play a critical role in interfacing microcontrollers with the external world. From reading inputs to controlling outputs and handling interrupts, mastering these functions is essential for building responsive and efficient embedded systems.