Python Serial Communication Using UART, SPI and I2C

Python Serial Communication Using UART, SPI and I2C Protocols

Introduction

Python serial communication using UART, SPI and I2C protocols is an important topic for students learning embedded systems, Internet of Things (IoT) development, hardware interfacing, and automation. These communication protocols allow computers, microcontrollers, sensors, and peripheral devices to exchange data efficiently. Python provides libraries that simplify communication, making it useful for testing embedded hardware, collecting sensor information, and developing IoT applications.

Understanding UART, SPI, and I2C helps students connect software programming with real-world electronic systems. Embedded Tech Development Academy (ETDA), a Top Embedded Training Institute in Bangalore, helps students develop practical technical knowledge through programming exercises, hardware interfacing, and project-based learning. Its technical training and assured placement support help learners prepare for embedded engineering opportunities.

Understanding Serial Communication in Python

Serial communication transfers data between electronic devices through communication interfaces. Instead of transmitting an entire data word simultaneously over multiple parallel connections, serial protocols transfer information through serial data lines.

Python supports different communication interfaces through specialized libraries. These interfaces are widely used in embedded systems development, Internet of Things (IoT) gateways, sensor monitoring, and firmware testing.

UART Communication Using Python

UART stands for Universal Asynchronous Receiver Transmitter. It is an asynchronous serial communication interface that transfers data without requiring a separate clock signal between communicating devices.

Important UART Parameters

UART communication typically requires configuring:

  • Baud rate.

  • Data bits.

  • Parity.

  • Stop bits.

  • Flow control.

Both communicating devices must use compatible communication settings to exchange data correctly.

Python UART Programming Using PySerial

PySerial is a popular Python library used for serial communication through UART interfaces.

Install the library using:

pip install pyserial

UART Communication Example

import serial

ser = serial.Serial(
    port="COM3",
    baudrate=9600,
    timeout=1
)

ser.write(b"Hello STM32\n")

data = ser.readline()

print(data.decode(errors="replace"))

ser.close()

This example opens a serial connection, transmits data, reads a response, and closes the connection.

On Linux, the serial port may appear as /dev/ttyUSB0 or /dev/ttyACM0, depending on the connected hardware and driver.

SPI Communication Using Python

SPI stands for Serial Peripheral Interface. It is a synchronous communication protocol commonly used for high-speed data exchange between microcontrollers and peripheral devices.

SPI Communication Signals

SPI typically uses four signals:

  • SCLK for serial clock.

  • MOSI for master-to-slave data.

  • MISO for slave-to-master data.

  • CS for selecting the connected peripheral.

SPI supports full-duplex communication, allowing data transmission and reception simultaneously.

Python SPI Programming Using spidev

On supported Linux systems, the spidev library provides access to SPI devices through the Linux SPI interface.

Install the library using:

pip install spidev

SPI Communication Example

import spidev

spi = spidev.SpiDev()

spi.open(0, 0)

spi.max_speed_hz = 500000
spi.mode = 0

response = spi.xfer2([0x01, 0x02, 0x03])

print(response)

spi.close()

This example opens SPI bus 0, device 0, configures the communication speed and mode, and transfers three bytes.

Actual SPI communication requires suitable hardware, enabled Linux SPI support, and compatible peripheral configuration.

I2C Communication Using Python

I2C stands for Inter-Integrated Circuit. It is a two-wire synchronous communication protocol that supports multiple devices sharing the same bus.

I2C Communication Signals

I2C uses two main signals:

  • SCL: Serial Clock Line.

  • SDA: Serial Data Line.

Both lines generally use open-drain signaling and require suitable pull-up resistors. Each slave device normally has an address used by the master to identify it.

Python I2C Programming Using smbus2

The smbus2 library provides Python access to SMBus-compatible I2C communication on supported Linux platforms.

Install it using:

pip install smbus2

I2C Communication Example

from smbus2 import SMBus

bus = SMBus(1)

device_address = 0x48

bus.write_byte(device_address, 0x01)

value = bus.read_byte(device_address)

print(value)

bus.close()

This example writes one byte to an I2C device and reads a byte in response.

The actual command format depends on the peripheral. Sensors and EEPROMs may require register addresses, specific commands, or additional configuration sequences.

UART vs SPI vs I2C – Key Differences

Understanding protocol differences helps students select the appropriate communication interface.

FeatureUARTSPII2C
ClockAsynchronousSynchronousSynchronous
Main signalsTX, RXSCLK, MOSI, MISO, CSSDA, SCL
SpeedModerateGenerally highModerate
DuplexFull-duplex capableFull-duplexHalf-duplex
AddressingNo built-in addressingChip-select basedDevice address
ApplicationsDebugging, serial terminalsDisplays, flash memorySensors, EEPROMs

UART is suitable for simple point-to-point communication, SPI is useful for high-speed peripheral communication, and I2C is convenient for connecting multiple low-speed devices.

Practical Python Communication Projects

Students can strengthen their communication programming skills through practical projects.

UART-Based Sensor Monitoring

Connect a microcontroller to a computer using UART and transmit sensor readings periodically. Python can receive the data, display measurements, and save them for analysis.

SPI-Based Sensor Data Acquisition

Use Python on a supported Linux development board to communicate with an SPI sensor. Read digital measurements and process them using Python libraries.

I2C-Based Environmental Monitoring

Connect temperature, humidity, or pressure sensors through I2C. Python can read sensor registers, calculate measurements, and display information on a dashboard.

These projects demonstrate how Python programming and embedded hardware communication work together in practical Internet of Things (IoT) applications.

Common Python Serial Communication Errors

Communication failures frequently occur because of incorrect configuration or hardware connections.

UART errors may result from incorrect baud rates, wrong serial ports, or incompatible data settings. SPI problems may occur because of incorrect clock modes, chip-select configuration, or unsupported peripheral connections.

I2C communication errors commonly involve incorrect device addresses, missing pull-up resistors, or bus configuration problems.

Students should verify wiring, protocol settings, device compatibility, and communication signals before debugging application code. Tools such as logic analyzers and oscilloscopes can help identify timing and communication problems.

Python Communication in Embedded Systems and IoT

Python communication libraries are useful for developing embedded test applications, Internet of Things (IoT) gateways, and hardware monitoring tools.

Python can communicate with microcontrollers through UART, access peripherals through SPI, and collect sensor data through I2C. These capabilities support automation, hardware validation, data logging, and rapid application development.

Students interested in embedded systems should understand both Python-based communication and low-level microcontroller programming because these skills complement each other in firmware development and hardware testing.

Learn Python and Embedded Communication at ETDA

Python serial communication provides valuable practical knowledge for students interested in embedded systems, Internet of Things (IoT), automation, and firmware development.

At Embedded Tech Development Academy (ETDA), students can strengthen their programming fundamentals through technical learning, practical exercises, and embedded project development.

Choosing a Top Embedded Training Institute in Bangalore helps learners understand how Python, Embedded C, microcontrollers, communication protocols, and debugging techniques are applied in real engineering projects.

Embedded Tech Development Academy (ETDA) focuses on practical skill development and provides assured placement support to help students prepare for technical employment opportunities.

Students searching for a Top Embedded Training Institute in Bangalore can explore Embedded Tech Development Academy (ETDA) to develop communication programming skills and build confidence in embedded hardware interfacing.

Frequently Asked Questions

What is Python serial communication?

Python serial communication allows programs to exchange data with hardware devices using interfaces such as UART, SPI, and I2C through suitable libraries.

PySerial is widely used for UART communication through serial ports on supported operating systems.

Yes. Python can communicate with SPI devices using libraries such as spidev on supported Linux platforms.

Libraries such as smbus2 provide access to I2C and SMBus-compatible interfaces on supported Linux systems.

SPI generally uses separate data lines and chip-select signals for faster communication, while I2C uses two shared lines and device addressing.

UART, SPI, and I2C programming helps students develop hardware interfacing, embedded testing, sensor communication, and Internet of Things (IoT) application development skills.

Conclusion

Python serial communication using UART, SPI, and I2C protocols is an essential skill for students learning embedded systems and Internet of Things (IoT) development. Understanding protocol fundamentals, Python libraries, hardware configuration, and practical communication examples helps learners develop reliable applications.

UART is useful for serial debugging and point-to-point communication, SPI supports high-speed peripheral interfacing, and I2C simplifies communication with multiple addressed devices.

For students planning an embedded engineering career, Embedded Tech Development Academy (ETDA) provides practical learning opportunities to strengthen programming knowledge and hardware interfacing skills. Exploring a Top Embedded Training Institute in Bangalore like Embedded Tech Development Academy (ETDA) can help learners build industry-oriented capabilities through hands-on projects, structured training, and assured placement support.

Author: ETDA Trainers
Experience: 10+ Years of Industry Experience in Embedded Systems, IoT, and Embedded C Programming