Skip to content
You Start with one site
Amounts in Start with one site
Engineering · Embedded Systems & Electronics

Sensor and Actuator Interfacing (STM32)

Connect the sensors, motors, displays and radios of a real product to the STM32 Blue Pill, so every reading is right and none is lost. Ten projects, from a proximity alarm to a station that logs, shows and sends its readings.

Found this useful?
Which device is on which bus of the Blue Pill in the capstone. I2C1 on PB6 and PB7 carries the BME280 at 0x76 and the SSD1306 OLED at 0x3C. SPI1 carries the microSD card, chip select on PA4. USART2 on PA2 and PA3 carries the HC-05 Bluetooth, JSON to the phone. ADC1 reads the potentiometer on PA0, the alarm threshold. GPIO drives the relay on PB12, the buzzer on PB13, green, yellow and red LEDs, and reads two buttons.

A button is pressed once and the program counts five presses. A thermistor shows 29 degrees on a hot day and never more. A GPS receiver sends about 400 characters a second, and a program that reads them in a busy loop loses some. A stepper told to start at full speed skips steps, and nothing reports it: every position after that is wrong.

In this course you connect one device after another the way a product does: the protocol, the wiring, a driver, and a test with a meter, a logic analyzer or a serial terminal. Ten projects on the Blue Pill end in one station that reads a BME280, shows it on an OLED, logs it to an SD card, sends it over Bluetooth, and treats a missing sensor as an alarm.

How each lesson goes
  1. 1Start from a faultA GPS sends about 400 bytes a second. A ring of 512 holds a little more than one second, so the loop must never block longer.
  2. 2Learn the protocolAn I2C write, field by field: start, the address 0x76, the register, the value, stop.
  3. 3Write the driverThe SD card starts slow, then reads or writes one 512 byte block at 4.5 MHz in about 0.9 ms.
  4. 4Test and measureWith 2.2 kΩ pull-ups the clock edge rises in 186 ns, inside the 400 kHz limit.
You will be able to
  1. Wire and drive digital, analog and PWM devices, with the filter, buffer or ramp each one needs
  2. Share I2C and SPI buses, receive serial data without loss, and join two boards on a CAN bus
  3. Join many devices in one firmware that keeps its timing and recovers from faults

New to this? Know the Blue Pill from the STM32 course? Start with lesson 1: set up CubeIDE and build a proximity alarm.

You need A Blue Pill (two for lesson 9), an ST-Link V2, a breadboard and the parts of each lesson: most 1 to 5 USD each, about 40 to 60 USD in all.

The engineering behind it

The exact step where the physical world becomes data: sampling, buses, and the errors each one adds.

Lessons
Lesson 1 · 18 minGPIO and Digital InterfacingA proximity alarm: a relay and a buzzer switch on when an object comes closer than 20 cm, and each press is counted once.CubeIDEGPIODebouncingExternal interrupts Lesson 2 · 14 minADC and Analog Signal ConditioningA monitor that reads a thermistor, a light sensor and a knob on three channels, copied to memory by DMA.Voltage dividersRC filterOp-amp bufferScan mode Lesson 3 · 13 minPWM, Timers, and Motor ControlTwo servos held at exact angles by two knobs, a DC motor that speeds up gently, and tones from the same timers.Timer PWMPrescaler and ARRH-bridgeInput capture Lesson 4 · 13 minI2C Sensors and DisplaysA weather station: a BME280, an OLED and an EEPROM on one two-wire bus, with a log of 512 readings.AddressesPull-upsBus scannerDrivers Lesson 5 · 11 minSPI Storage and DisplaysA logger that draws a live chart on a colour screen and writes every reading to an SD card as CSV.SPI modesChip selectSpeed switchingFatFS Lesson 6 · 13 minUART GPS Bluetooth RS-485A GPS tracker that loses no byte, sends the position to a phone by Bluetooth, and along hundreds of metres by RS-485.DMA with idle lineNMEABluetoothRS-485 Lesson 7 · 12 minRFID NFC IdentificationA door access system with an RC522 reader and a master card, and the point where its security ends.MIFARE ClassicAnti-collisionState machineUID limits Lesson 8 · 14 minStepper Motors and EncodersA positioning stage that finds its zero, ramps its speed, and moves a NEMA 17 to the place an encoder sets.A4988Speed rampEncoder modeHoming Lesson 9 · 15 minDMA Interrupts and CAN BusTwo Blue Pills on a CAN bus: one sends a knob value every 100 ms, the other dims an LED, and both rejoin after a fault.DMAInterrupt prioritiesbxCANFilters Lesson 10 · 15 minMulti-Sensor Data LoggerOne station: sensor, OLED, SD card, Bluetooth, relay and buzzer in one firmware that recovers from faults.Bus planTask timingDisplay pagesFault recovery
More about this course

Every number has a reason

A resistor, a buffer or a speed is not chosen because it looks right. Each number comes from the device, the bus or the load.

  • 10 msthe least RC of the button filter: the contacts bounce for 1 to 10 ms, and a processor that reads the pin millions of times a second sees one press as five or tenDigital input
  • 1.8 Vthe limit of the LM358 buffer on a 3.3 V supply; the thermistor divider gives 1.8 V at about 29 °C, so the reading would stop rising thereAnalog input
  • 2.2 kΩone pull-up on each I2C wire at 400 kHz; module pull-ups add in parallel, and below about 1 kΩ the chips can no longer pull the line lowI2C
  • 512 bytesof DMA ring for the GPS: at about 400 bytes a second it holds a little more than one second, so the main loop must never block longerUART
  • 2000 steps/s²the ramp that brings the stepper to its cruise speed of 800 steps a second without leaving the rotor behindMotion

I2C saves pins. SPI saves time.

I2C
Two wires for many chips
  • Each chip has its own address
  • 100 or 400 kHz
  • One wrong address or missing resistor stops every device on the bus

The test: many slow devices and few pins?

SPI
One more wire for each chip
  • A chip select for each device
  • Millions of bits a second: one colour picture of 41,000 bytes would take almost 4 s on I2C at 100 kHz
  • Two devices selected at once give noise or a damaged file

The test: a screen or a card that needs speed?

Why STM32CubeIDE and the Blue Pill

Pins on a pictureGive I2C1 its pins and see each conflict at once.
Clock treeCheck that the bus clock gives the baud rate you need.
HAL code generatedCorrect start-up code from your settings; your time goes to the driver.
Debugger built inBreakpoints and live variables with no GDB command line.
Parts of real productsBME280, OLED, RC522, NEO-6M, A4988, MCP2551: most 1 to 5 USD each.
Production notesWhat changes from breadboard to board: pull-ups, decoupling, ESD and bus speed.

Honest limits. For a custom bootloader or code that must be checked line by line, the bare toolchain of the STM32 course is the better tool. The relay and the buzzer stay under the control of the firmware; the readings can also go on in batches to a system that keeps the history and sends an alert when a value passes a limit.

Before you start

  • The Embedded Programming: STM32 course, or the same experience
  • C, a Blue Pill and an ST-Link V2
  • Reading a schematic, a breadboard and Ohm's law
  • A logic analyzer or oscilloscope helps, but is optional
The full course notes

Knowing how to configure a timer or set up a UART peripheral is one thing. Knowing how to connect an RFID reader, drive a stepper motor to a precise position, or parse GPS coordinates from a serial stream is another. This course bridges that gap. Every lesson pairs a communication protocol with real hardware you can hold in your hand, and every project does something useful when you are done building it. #STM32 #Interfacing #Sensors

Why This Course?

The Embedded Programming: STM32 course taught the internals: clock trees, register access, DMA channels, linker scripts. That foundation matters, but most embedded work involves connecting external devices and making them cooperate. This course focuses entirely on that external world.

Protocol to Device

Each lesson pairs a protocol (GPIO, ADC, PWM, I2C, SPI, UART, CAN) with real sensors and actuators. You do not just read a datasheet; you wire, configure, code, and verify the device on your bench.

Industry-Relevant Hardware

The parts list covers what engineers actually use: BME280, MPU6050, OLED displays, RFID readers, GPS modules, stepper drivers, CAN transceivers. These are the components that show up in product designs, not toy peripherals.

CubeMX Visual Configuration

STM32CubeIDE with its built-in CubeMX lets you visually assign pins, configure clock trees, and generate HAL initialization code. For an interfacing course, this is the right tool: you see which pins support I2C1 or SPI2, resolve conflicts graphically, and focus your time on the device driver logic rather than register bit-twiddling.

From Single Device to System

Lessons build toward a capstone that combines multiple sensors, an SD card, an OLED display, Bluetooth streaming, and alarm outputs into one integrated system. You learn not just how to talk to each device, but how to manage multiple peripherals sharing the same bus.

Why STM32CubeIDE Instead of arm-gcc + OpenOCD?

The Embedded Programming: STM32 course used the bare toolchain (arm-none-eabi-gcc, OpenOCD, make) because the goal was to understand what happens at the register level: startup code, linker scripts, vector tables, and raw peripheral access. That knowledge is valuable and this course assumes you have it.

For interfacing, the priorities shift. You are configuring I2C buses, SPI chip selects, UART baud rates, ADC channels, DMA streams, and CAN bit timing, sometimes several of these on the same project. Doing all of that through raw register writes is slow and error-prone when the goal is to get a sensor talking. CubeIDE with CubeMX gives you:

  • Visual pin assignment: drag I2C1_SCL to PB6, see conflicts highlighted in real time
  • Clock tree visualization: verify that your APB1 clock gives the UART baud rate you need
  • HAL code generation: correct initialization sequences generated from your configuration
  • Integrated debugger: click-to-set breakpoints, live variable watch, no GDB command line needed
  • Two installs, one workflow: STM32CubeIDE (compiler, debugger, editor) and STM32CubeMX (pin configurator, code generator) work together. Starting with CubeIDE 2.0, CubeMX is a separate application

When to use the bare toolchain instead: Custom bootloaders, safety-critical firmware that must be audited line by line, extremely tight flash constraints where HAL overhead matters, or CI/CD pipelines where a headless Makefile build is simpler. If you need to strip a binary down to 4 KB or certify every line of code for automotive/medical, the bare toolchain gives you that control. For everything else, CubeIDE is faster and less error-prone.

Both toolchains produce the same ARM binary. CubeIDE generates a Makefile project you can export and build from the command line if you ever need to.

Course Structure

Each lesson follows a consistent cycle:

  1. The Interfacing Challenge A real device that needs to communicate with the STM32. Not a toy example, but a sensor, actuator, or module you would find in a product design.

  2. Protocol and Wiring How the protocol works (timing, addressing, data framing), the wiring diagram with pin assignments, and the CubeMX configuration for the peripheral.

  3. Build the Driver Write the HAL-based driver code, read/write to the device, handle errors. Every code block compiles and runs on the Blue Pill.

  4. Test and Verify Flash, connect, measure. Use a multimeter, logic analyzer, or serial terminal to confirm the device responds correctly.

  5. Production Notes What changes when you move from a breadboard to a PCB: pull-up values, decoupling, ESD protection, bus speed limits, and power considerations.

Lessons

Lesson 1: GPIO and Digital Interfacing

GPIO and Digital Interfacing. Set up STM32CubeIDE, configure GPIO inputs and outputs with the HAL, and interface digital devices. Implement hardware debouncing, use interrupts for responsive input handling, and drive a relay module. Build: Ultrasonic proximity alarm with relay trigger. Parts: HC-SR04, relay module, buzzer, push button.

Lesson 2: ADC and Analog Signal Conditioning

ADC and Analog Signal Conditioning. Configure the 12-bit ADC with single, continuous, and scan modes. Add signal conditioning circuits (voltage dividers, RC filters, op-amp buffers) to interface analog sensors cleanly. Build: Multi-channel environmental monitor with LED bar graph. Parts: Thermistor (NTC 10K), LDR, potentiometer, op-amp (LM358).

Lesson 3: PWM, Timers, and Motor Control

PWM, Timers, and Motor Control. Generate PWM signals with hardware timers for servo positioning, DC motor speed control, and audio tone generation. Use input capture to measure external signals. Build: Servo pan-tilt mount with DC motor speed control. Parts: 2x SG90 servo, DC motor, L298N H-bridge, potentiometers, passive buzzer.

Lesson 4: I2C Protocol: Sensors and Displays

I2C Protocol: Sensors and Displays. Master I2C communication with multiple devices on a shared bus. Read environmental data, drive an OLED display, and store readings in EEPROM. Handle bus scanning, addressing, and error recovery. Build: Weather station with OLED display and EEPROM logging. Parts: BME280, SSD1306 OLED (128x64), AT24C256 EEPROM.

Lesson 5: SPI Protocol: Storage and Displays

SPI Protocol: Storage and Displays. Configure SPI with proper clock polarity, phase, and chip select management. Interface a color TFT display and an SD card on the same SPI bus using FatFS for file operations. Build: Portable data logger with color TFT display. Parts: ST7735 TFT (1.8”), microSD card module, microSD card.

Lesson 6: UART Devices: GPS, Bluetooth, and RS-485

UART Devices: GPS, Bluetooth, and RS-485. Parse NMEA sentences from a GPS module, relay data over Bluetooth to a phone, and communicate over long distances with RS-485 differential signaling. Use DMA for efficient UART reception. Build: GPS tracker with Bluetooth phone relay. Parts: NEO-6M GPS module, HC-05 Bluetooth module, MAX485 transceiver.

Lesson 7: RFID, NFC, and Identification Systems

RFID, NFC, and Identification Systems. Interface the RC522 RFID reader over SPI, read and write MIFARE cards, build an access control state machine with visual and audio feedback. Store authorized card UIDs in EEPROM. Build: RFID door access control system. Parts: RC522 RFID module, MIFARE cards/tags, SSD1306 OLED, buzzer, LED.

Lesson 8: Stepper Motors and Encoder Feedback

Stepper Motors and Encoder Feedback. Drive stepper motors with acceleration profiles using the A4988 driver. Read rotary encoder position with timer encoder mode. Implement closed-loop positioning with limit switches for homing. Build: Precision linear positioning stage. Parts: NEMA 17 stepper, A4988 driver, rotary encoder, 2x limit switches, 12V supply.

Lesson 9: DMA, Interrupts, and CAN Bus

DMA, Interrupts, and CAN Bus. Use DMA for zero-copy ADC streaming and SPI transfers. Configure the STM32’s built-in bxCAN peripheral for automotive-grade communication. Build a two-node CAN network with message filtering and error handling. Build: CAN bus sensor network (two STM32 nodes). Parts: 2x Blue Pill, 2x MCP2551 CAN transceiver, 120 ohm resistors.

Lesson 10: Capstone: Multi-Sensor Data Logger

Multi-Sensor Data Logger. Combine sensors, storage, display, and wireless from previous lessons into one integrated system. Manage multiple peripherals sharing I2C and SPI buses, handle timing conflicts, implement power management, and build a complete data acquisition system. Build: Environmental monitoring station with SD logging, OLED display, Bluetooth streaming, and alarm outputs. Parts: All reused from prior lessons.

Parts Kit

Most parts cost between 1 and 5 USD each. Many are reusable across lessons.

PartQuantityFirst UsedNotes
Blue Pill (STM32F103C8T6, WeAct recommended)2Lesson 1Second unit needed for CAN bus (Lesson 9). Reuse from STM32 course
ST-Link V2 clone1Lesson 1SWD programmer/debugger. Reuse from STM32 course
Breadboard (full-size)2Lesson 1One per CAN node in Lesson 9
Jumper wires (M-M, M-F)40+Lesson 1Assorted lengths
HC-SR04 ultrasonic sensor1Lesson 1Distance measurement
Relay module (5V, 1-channel)1Lesson 1Digital output control
Passive buzzer1Lesson 1Audio feedback, PWM-driven tones
Push buttons2Lesson 1Tactile switches
NTC 10K thermistor1Lesson 2Temperature sensing
LDR (photoresistor)1Lesson 2Light level sensing
Potentiometer (10K)2Lesson 2Analog input, servo control
LM358 op-amp1Lesson 2Signal conditioning buffer
SG90 micro servo2Lesson 3Pan-tilt positioning
DC motor (3-6V)1Lesson 3Speed and direction control
L298N motor driver1Lesson 3H-bridge for DC motor
BME280 sensor module1Lesson 4Temperature, humidity, pressure (I2C)
SSD1306 OLED (128x64, I2C)1Lesson 4Display, reused in Lessons 7 and 10
AT24C256 EEPROM module1Lesson 4I2C data storage
ST7735 TFT display (1.8”)1Lesson 5Color SPI display
microSD card module1Lesson 5SPI SD card interface
microSD card (any size)1Lesson 5Data storage
NEO-6M GPS module1Lesson 6UART GPS with antenna
HC-05 Bluetooth module1Lesson 6UART Bluetooth SPP
MAX485 module2Lesson 6RS-485 transceiver, one per end
RC522 RFID module1Lesson 7SPI RFID reader
MIFARE Classic cards/tags2+Lesson 713.56 MHz RFID tags
NEMA 17 stepper motor1Lesson 8Bipolar stepper
A4988 stepper driver1Lesson 8Microstepping driver
Rotary encoder (with push button)1Lesson 8Position feedback
Limit switches (microswitch)2Lesson 8Homing reference
12V power supply (1A+)1Lesson 8Stepper motor power
MCP2551 CAN transceiver2Lesson 9CAN bus physical layer
120 ohm resistor2Lesson 9CAN bus termination
LEDs (assorted)5+VariousStatus indicators
Resistors (330, 1K, 4.7K, 10K)AssortedVariousPull-ups, current limiting

Estimated total cost: 40 to 60 USD (less if you have parts from prior courses)

Skill Progression

SkillLessons 1-3Lessons 4-6Lessons 7-8Lessons 9-10
ProtocolGPIO, ADC, PWMI2C, SPI, UARTSPI (RFID), Encoder modeDMA, CAN bus, multi-bus
DevicesUltrasonic, relay, servo, DC motorBME280, OLED, TFT, SD, GPS, BluetoothRFID reader, stepper, encoderCAN transceiver, full sensor suite
TechniqueDebouncing, filtering, PWM generationBus scanning, FatFS, NMEA parsingState machines, acceleration profilesZero-copy DMA, message filtering
ToolingCubeIDE setup, HAL basics, CubeMX configMulti-device bus sharing, clock treeEEPROM storage, closed-loop controlTwo-node networks, system integration

Prerequisites

STM32 experience required

You should be comfortable with:

  • C programming and basic embedded concepts
  • The STM32 Blue Pill board and ST-Link V2 programmer
  • At minimum, the Embedded Programming: STM32 course (or equivalent experience)
  • Basic electronics: reading schematics, using a breadboard, Ohm’s law

Helpful but not required:

  • Logic analyzer or oscilloscope (useful for debugging protocols)
  • Multimeter (for voltage checks and continuity testing)
  • The PCB Design with KiCad course (if you want to move projects to PCBs)

Getting Started

  1. Install STM32CubeIDE and STM32CubeMX. Download both from st.com/stm32cubeide and st.com/stm32cubemx. Both are free and run on Linux, macOS, and Windows. Starting with CubeIDE 2.0, CubeMX is a separate application: you configure peripherals and generate code in CubeMX, then build and debug in CubeIDE. Lesson 1 walks through the full setup.

  2. Gather Lesson 1 parts. You need the Blue Pill, ST-Link V2 clone, HC-SR04 ultrasonic sensor, relay module, buzzer, push button, and a breadboard. If you completed the STM32 embedded course, you already have the board and programmer.

  3. Start with Lesson 1. It walks through CubeIDE project creation, explains why we use CubeIDE for this course (and when the bare toolchain is still the right choice), and builds a complete proximity alarm system.

  4. Work through lessons in order. Later lessons reuse components and code patterns from earlier ones. The capstone in Lesson 10 ties everything together.

© 2021-2026 SiliconWit®. All rights reserved.

10 lessons, in order.

SiliconWit makes these courses. We also run siliconwit.io, a monitoring service.