STM32 Blue Pill SPI MAX7219 LED Matrix: Wiring, Code and Simulator

To build this STM32 Blue Pill SPI MAX7219 LED Matrix project you need an STM32 Blue Pill (STM32F103C8) and a MAX7219 8x8 LED matrix. The SPI library on SPI1 (SCK PA5, MOSI PA7, CS PA4) sends two-byte commands to a MAX7219 and switches an 8x8 matrix between a smile and a heart. Make the 5 connections in the wiring table, upload the Arduino C++ code, and open the logic analyzer on PA5, PA7 and PA4 to watch each SPI write, or draw your own 8x8 picture in the SMILE array.

Open this circuit in the free STM32 Blue Pill (STM32F103C8) simulator

Parts list

QtyPartWhat it does
1STM32 Blue Pill (STM32F103C8)the microcontroller board
1MAX7219 8x8 LED matrixAn 8×8 red matrix. Shift 16 bits (register, data) into DIN on CLK with CS LOW, then raise CS. Rows are registers 1–8; write 0x0C=1 to switch it on.

Wiring

#FromTo
1STM32 Blue Pill (STM32F103C8) PA7MAX7219 8x8 LED matrix DIN
2STM32 Blue Pill (STM32F103C8) PA5MAX7219 8x8 LED matrix CLK
3STM32 Blue Pill (STM32F103C8) PA4MAX7219 8x8 LED matrix CS
4STM32 Blue Pill (STM32F103C8) 5VMAX7219 8x8 LED matrix VCC
5STM32 Blue Pill (STM32F103C8) GNDMAX7219 8x8 LED matrix GND

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the STM32 Blue Pill (STM32F103C8). No install or account is needed.
  2. Add the parts. Add a MAX7219 8x8 LED matrix from the parts palette.
  3. Wire 1. Connect STM32 Blue Pill (STM32F103C8) PA7 to MAX7219 8x8 LED matrix DIN.
  4. Wire 2. Connect STM32 Blue Pill (STM32F103C8) PA5 to MAX7219 8x8 LED matrix CLK.
  5. Wire 3. Connect STM32 Blue Pill (STM32F103C8) PA4 to MAX7219 8x8 LED matrix CS.
  6. Wire 4. Connect STM32 Blue Pill (STM32F103C8) 5V to MAX7219 8x8 LED matrix VCC.
  7. Wire 5. Connect STM32 Blue Pill (STM32F103C8) GND to MAX7219 8x8 LED matrix GND.
  8. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  9. Run it. Press Run. Open the logic analyzer on PA5, PA7 and PA4 to watch each SPI write, or draw your own 8x8 picture in the SMILE array.

Arduino C++ code

// Hardware SPI on SPI1 (SCK PA5, MOSI PA7, CS PA4) drives a MAX7219 8x8 LED
// matrix: each write is two bytes, a register (1-8 = rows) and its value.
#include <SPI.h>

const int CS = PA4;

void max7219(uint8_t reg, uint8_t value) {
  digitalWrite(CS, LOW);
  SPI.transfer(reg);
  SPI.transfer(value);
  digitalWrite(CS, HIGH);
}

const uint8_t SMILE[8] = {0x3C, 0x42, 0xA5, 0x81, 0xA5, 0x99, 0x42, 0x3C};
const uint8_t HEART[8] = {0x00, 0x66, 0xFF, 0xFF, 0xFF, 0x7E, 0x3C, 0x18};

void show(const uint8_t *face) {
  for (int row = 0; row < 8; row++) max7219(row + 1, face[row]);
}

void setup() {
  Serial.begin(115200);
  pinMode(CS, OUTPUT);
  digitalWrite(CS, HIGH);
  SPI.begin();
  SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
  max7219(0x0F, 0);  // display test off
  max7219(0x09, 0);  // no BCD decoding: raw rows
  max7219(0x0B, 7);  // scan all 8 rows
  max7219(0x0A, 8);  // medium brightness
  max7219(0x0C, 1);  // wake up
  Serial.println("MAX7219 ready");
}

void loop() {
  show(SMILE); Serial.println("smile"); delay(1000);
  show(HEART); Serial.println("heart"); delay(1000);
}

What to try

Open the logic analyzer on PA5, PA7 and PA4 to watch each SPI write, or draw your own 8x8 picture in the SMILE array.

Frequently asked questions

How do I wire the SPI MAX7219 LED Matrix project on an STM32 Blue Pill (STM32F103C8)?

Make 5 connections: STM32 Blue Pill (STM32F103C8) PA7 to MAX7219 8x8 LED matrix DIN; STM32 Blue Pill (STM32F103C8) PA5 to MAX7219 8x8 LED matrix CLK; STM32 Blue Pill (STM32F103C8) PA4 to MAX7219 8x8 LED matrix CS; STM32 Blue Pill (STM32F103C8) 5V to MAX7219 8x8 LED matrix VCC; STM32 Blue Pill (STM32F103C8) GND to MAX7219 8x8 LED matrix GND.

What parts do I need for this STM32 Blue Pill project?

An STM32 Blue Pill (STM32F103C8) and a MAX7219 8x8 LED matrix.

Can I simulate this STM32 Blue Pill project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is built with the STM32duino core and runs on an STM32F103 emulated instruction by instruction: its ARM Cortex-M3 core, NVIC and peripherals (GPIO, EXTI, USART, timers, ADC and I²C).

What does the code do?

The SPI library on SPI1 (SCK PA5, MOSI PA7, CS PA4) sends two-byte commands to a MAX7219 and switches an 8x8 matrix between a smile and a heart.

More STM32 Blue Pill (STM32F103C8) projects