Embedded software becomes meaningful when it interacts with electrical signals. This chapter covers the local interfaces between a processor and nearby sensors, memory, displays, converters and other components.
The key idea is to separate electrical signaling, bus mechanics and application data.
1. GPIO: the simplest digital interface
GPIO means General-Purpose Input/Output.
A pin can often be configured as:
- digital input;
- digital output;
- alternate peripheral function;
- analog input on capable pins.
Important concepts:
- logic HIGH / LOW;
- input threshold;
- output drive strength;
- pull-up / pull-down resistor;
- push-pull output;
- open-drain/open-collector output;
- edge interrupt;
- debounce.
A software value of 1 does not automatically mean a particular voltage. Logic levels depend on the electrical standard and supply domain.
2. Pull-ups, pull-downs and floating inputs
An un-driven digital input can float and randomly read HIGH or LOW.
A pull-up weakly biases it toward HIGH. A pull-down biases it toward LOW.
Open-drain buses such as I²C depend on pull-up resistors because devices actively pull the line LOW but release it for HIGH.
Choosing pull resistance affects rise time, current and noise sensitivity.
3. ADC and DAC
ADC
An Analog-to-Digital Converter samples an analog voltage and converts it into a digital number.
Important terms:
- resolution, e.g. 10-bit / 12-bit / 16-bit;
- reference voltage;
- sampling rate;
- input range;
- quantization;
- noise;
- offset and gain error.
For an ideal N-bit ADC, the digital range has 2^N codes.
DAC
A Digital-to-Analog Converter generates an analog voltage/current representation from a digital value.
Not every MCU has a true DAC; PWM plus filtering is sometimes used instead.
4. PWM
PWM is a digital waveform whose duty cycle is controlled.
25% duty: __|‾|___|‾|___
50% duty: __|‾‾|__|‾‾|__
75% duty: __|‾‾‾|_|‾‾‾|_Frequency and duty cycle are separate parameters.
Typical uses:
- motor control;
- LED brightness;
- servo pulses;
- switch-mode power control;
- simple waveform generation.
5. UART
UART is an asynchronous serial interface. It normally needs at least TX, RX and a shared reference/ground.
Common parameters:
- baud rate;
- data bits;
- parity;
- stop bits;
- flow control.
A familiar configuration is 115200, 8 data bits, no parity, 1 stop bit: 115200 8N1.
UART itself does not define message meaning. The application may send text, binary packets, AT commands, a proprietary frame or another protocol.
UART vs RS-232 / RS-485
UART describes serial framing at the digital interface. RS-232 and RS-485 define electrical signaling characteristics.
A microcontroller UART pin is therefore not automatically RS-232 compatible. A transceiver may be required.
6. RS-232 and RS-485
RS-232
RS-232 is traditionally point-to-point and uses voltage levels different from normal MCU GPIO/UART levels.
RS-485
RS-485 uses differential signaling and supports longer/noisier links and multi-drop topologies.
Important RS-485 concepts:
- A/B differential pair;
- bus termination;
- biasing/failsafe state;
- half-duplex vs full-duplex implementation;
- driver-enable timing.
Protocols such as Modbus RTU are commonly carried over RS-485, but RS-485 itself does not define Modbus messages.
7. I²C
I²C is a synchronous multi-device bus commonly used for sensors, EEPROMs, RTCs and configuration devices.
Typical signals:
- SDA — data;
- SCL — clock.
Both lines are normally open-drain/open-collector with pull-ups.
Important concepts:
- controller/target roles;
- 7-bit and 10-bit addressing;
- START / STOP conditions;
- ACK / NACK;
- repeated START;
- clock stretching;
- arbitration in multi-controller systems.
A transaction conceptually looks like:
START → address + R/W → ACK → data → ACK/NACK → STOPThe device register map is an application/device contract layered on top of I²C transfers.
8. SPI
SPI is a synchronous serial interface commonly used for displays, flash memory, ADCs, sensors and high-rate peripherals.
Typical signals:
- SCLK — clock;
- MOSI / controller-out-target-in;
- MISO / controller-in-target-out;
- CS/SS — chip select.
SPI is usually full duplex and has no universal addressing layer; the controller selects a target using chip-select lines or external logic.
Important concepts:
- clock frequency;
- CPOL and CPHA;
- SPI mode 0–3;
- bit order;
- chip-select timing;
- transaction framing defined by the peripheral.
Incorrect CPOL/CPHA can produce data that looks shifted or completely invalid.
9. I²C vs SPI vs UART
| Property | UART | I²C | SPI |
|---|---|---|---|
| Clock line | No | Yes | Yes |
| Typical wires | TX/RX | SDA/SCL | SCLK + data + CS |
| Addressing in interface | No | Yes | Usually via CS |
| Duplex | Full duplex possible | Half-duplex-style shared data | Usually full duplex |
| Typical use | Console/module/link | Many low/medium-rate peripherals | Faster local peripherals |
| Pull-ups required | Not inherently | Yes | Not inherently |
There is no universally “best” bus. Board topology, bandwidth, device count, pin budget, distance and power matter.
10. USB
USB is much more than “a pair of wires.” It combines electrical signaling, enumeration, descriptors, transfer types, device classes and host/device roles.
Useful concepts:
- host and device roles;
- enumeration;
- endpoint;
- descriptor;
- control transfer;
- bulk transfer;
- interrupt transfer;
- isochronous transfer;
- device classes such as HID, CDC and mass storage.
USB Type-C describes connector/cable and role/power capabilities; it is not identical to a particular USB data generation.
USB Power Delivery is another protocol family layered on Type-C signaling for power negotiation.
11. Logic levels and level shifting
Two digital devices can both “speak SPI” and still be electrically incompatible.
Examples:
- 1.8 V device connected to 3.3 V-only-tolerant logic;
- 5 V output driving a non-5-V-tolerant MCU;
- missing common ground;
- open-drain bus without pull-ups.
Level shifters/transceivers are used when voltage domains or electrical standards differ.
12. Signal integrity and distance
A digital waveform is an analog electrical signal underneath.
At higher speeds or longer traces/cables, behavior depends on:
- capacitance;
- inductance;
- impedance;
- edge rate;
- termination;
- reflections;
- EMI;
- ground quality;
- connector/cable characteristics.
A protocol analyzer may report corrupt frames, but the root cause can be electrical.
13. Reading a datasheet interface section
For any peripheral, extract:
- voltage and absolute maximum ratings;
- pin functions;
- required pull resistors;
- supported bus modes/speeds;
- timing parameters;
- initialization sequence;
- register map or command framing;
- reset/default state;
- error/status flags.
Do not write software from a one-line product description. The electrical and timing tables are part of the interface contract.
14. Useful tools
Multimeter
Measures voltage, resistance, continuity and current (with correct setup).
Oscilloscope
Shows analog waveform vs time. Useful for rise time, glitches, ringing, PWM, clock quality and timing.
Logic analyzer
Captures digital lines and can decode UART, I²C, SPI and other protocols.
USB protocol analyzer
Provides deeper visibility into USB transactions than an ordinary serial terminal.
A logic analyzer tells you what bits were observed; an oscilloscope helps explain why those bits became wrong electrically.
15. Practice
Exercise 1
A 3.3 V MCU must communicate with a 5 V UART module. Explain what you must verify before connecting them.
Exercise 2
An I²C sensor never ACKs. Build an investigation starting from power, pull-ups and address before blaming the driver.
Exercise 3
An SPI flash returns shifted data. Which clock-mode and timing parameters would you compare with the datasheet?
Exercise 4
Explain why Modbus RTU, RS-485 and UART should not be used as synonyms.
Quick testing lens
For local interfaces, high-value failure cases include:
- wrong voltage/pull-up configuration;
- min/max supported clock or baud rates;
- boundary register values and reserved bits;
- missing ACK/NACK or timeout;
- device reset during transaction;
- partial/corrupted frame;
- bus contention or multiple devices;
- cable/trace/load variations;
- recovery after peripheral lock-up.