0
0
Embedded Cprogramming~5 mins

Why GPIO is the foundation of embedded in Embedded C

Choose your learning style9 modes available
Introduction

GPIO pins let a microcontroller talk to the outside world by turning things on or off and reading signals.

To turn on an LED light when a button is pressed.
To read the state of a sensor like a temperature or motion detector.
To control motors or relays in a robot or machine.
To communicate simple signals between devices.
To detect if a switch is open or closed.
Syntax
Embedded C
void GPIO_SetPinOutput(int pinNumber);
int GPIO_ReadPinInput(int pinNumber);

GPIO pins can be set as input or output.

Output pins send signals; input pins read signals.

Examples
This sets pin 5 so you can turn something on or off.
Embedded C
GPIO_SetPinOutput(5); // Set pin 5 as output
This reads if pin 3 is HIGH or LOW, like checking if a button is pressed.
Embedded C
int buttonState = GPIO_ReadPinInput(3); // Read pin 3 input
Sample Program

This program shows how to set a pin as output and turn it on, then read another pin as input.

Embedded C
#include <stdio.h>

// Simulated GPIO functions for demo
int gpio_pins[10] = {0};

void GPIO_SetPinOutput(int pinNumber) {
    // In real embedded, this sets pin mode to output
    printf("Pin %d set as output\n", pinNumber);
}

void GPIO_WritePin(int pinNumber, int value) {
    gpio_pins[pinNumber] = value;
    printf("Pin %d output set to %d\n", pinNumber, value);
}

int GPIO_ReadPinInput(int pinNumber) {
    // In real embedded, this reads pin state
    return gpio_pins[pinNumber];
}

int main() {
    GPIO_SetPinOutput(1); // Set pin 1 as output
    GPIO_WritePin(1, 1);  // Turn on pin 1 (like turning on LED)

    int sensorValue = GPIO_ReadPinInput(2); // Read pin 2 input
    printf("Sensor value on pin 2 is %d\n", sensorValue);

    return 0;
}
OutputSuccess
Important Notes

GPIO pins are the simplest way for a microcontroller to interact with the real world.

Always check if a pin is input or output before using it.

In real embedded systems, you control hardware registers to manage GPIO pins.

Summary

GPIO pins let microcontrollers send and receive simple signals.

They are the basic building blocks for embedded hardware control.

Understanding GPIO is key to making embedded devices work.