🪴 GoDeep Search
← Electronics

Microcontrollers & GPIO

What a microcontroller is, how its digital and analog I/O work, PWM, and how it reads sensors and drives outputs.

12 cards · 8 quiz questions · 7 min read

If sensors and actuators are a robot’s senses and muscles, the microcontroller is its nervous system and brain rolled into one tiny chip. Almost every modern gadget, from a microwave to a quadcopter’s flight controller, is run by a microcontroller reading inputs and driving outputs. Understanding what it is and how its pins work is the gateway to embedded electronics.

What is a microcontroller?

A microcontroller (MCU) is a complete small computer on a single chip. It integrates three things that a desktop PC keeps separate: a CPU to execute instructions, memory (flash for the program plus RAM for working data), and peripherals such as I/O pins, timers and converters.

The contrast with a desktop CPU is instructive. A PC processor needs external RAM, storage and a chipset, and runs a full operating system with many programs. An MCU packs everything onto one low-power chip, runs a single dedicated program, and is built to interface directly with electronics. It trades raw performance for integration, efficiency and the ability to talk to the physical world.

Popular examples span a wide range. Arduino is a beginner-friendly ecosystem, typically built around ATmega-class chips with a simple programming environment. STM32 is a family of more capable ARM Cortex-M microcontrollers used in higher-performance and professional designs, including many drone flight controllers.

GPIO: the connection to the world

The MCU’s main link to external hardware is its GPIO, short for General Purpose Input/Output. These are pins that software can configure either as an input, to read a signal, or as an output, to drive one. Configuring and toggling these pins is the essence of embedded programming.

Digital I/O

Most pins are digital, meaning they deal in just two states:

  • HIGH — logic 1, a voltage near the supply (commonly 3.3 V or 5 V).
  • LOW — logic 0, a voltage near 0 V.

As an output, a digital pin switches things on and off. As an input, it reads a button or another logic signal. To read a push button reliably, the button is paired with a pull-up or pull-down resistor so the pin sees a defined level when the button is open, and software often applies debouncing to ignore the brief contact bounce when the button is pressed.

Analog input

The real world is rarely just on or off. To read a continuously varying voltage, an MCU uses an ADC (analog-to-digital converter). It samples the input voltage and reports it as a number; a 10-bit ADC, for instance, returns 0 to 1023 across its input range. This is how a microcontroller reads a potentiometer, a light sensor, or a thermistor, turning a smooth physical quantity into a value the program can act on.

PWM: faking an analog output

Outputting a true variable voltage is harder, so MCUs use a clever trick called PWM (Pulse Width Modulation). The pin stays fully digital, switching on and off very rapidly, but the program varies the fraction of each cycle it stays on, the duty cycle.

A 25 percent duty cycle is HIGH a quarter of the time. For an LED that looks like roughly a quarter brightness; for a motor, roughly a quarter speed.

By averaging out, PWM approximates an analog level without a true analog output. It is the standard way to dim LEDs, set motor speed, and command servos (which read the pulse width as an angle).

Driving real loads safely

A GPIO pin is delicate. It can only source or sink a small current, often just tens of milliamps. That shapes how you connect things:

  • An LED is driven from a HIGH output through a series current-limiting resistor, which holds the current to a safe level (commonly around 10 to 20 mA) so neither the LED nor the pin is damaged.
  • A motor draws far more current than a pin can supply. So you never connect a motor directly. Instead a transistor, MOSFET or driver IC switches the large current, and the GPIO pin merely controls that switch.

Talking to peripherals

Wiring every sensor to its own bundle of GPIO pins would quickly run out of pins. Instead, MCUs use serial buses: UART (simple two-wire serial), I2C (two wires, addressable, many devices share one bus) and SPI (faster, a few more wires). Most sensors, displays and memory chips attach over one of these, exchanging data with the MCU over just a handful of wires.

Put together, a microcontroller is a self-contained controller: it reads the world through digital inputs and ADCs, decides in software, and acts through digital outputs, PWM and bus-connected peripherals, always mindful of how little current its pins can safely handle. Master GPIO, analog input and PWM, and you can make a chip sense and command almost any circuit.

Sources

  • Arduino — Arduino documentation website Official reference for GPIO, digital/analog I/O, PWM and serial buses.
  • Paul Scherz & Simon Monk — Practical Electronics for Inventors book Covers microcontroller interfacing, drivers and current limiting.
  • EETech Media — All About Circuits website Tutorials on digital logic, ADCs and microcontroller I/O.