42. LED toggle using SPI

In this task, we'll set up SPI communication between two microcontrollers, one acting as the master and the other as the slave. The goal is to toggle an LED connected to the slave microcontroller by pressing a push-button connected to the master.

Components Needed

  • Master Microcontroller (e.g., Arduino, STM32, ESP32)
  • Slave Microcontroller (same or compatible family)
  • Push-button switch (for the master)
  • LED (for the slave)
  • Current-limiting resistor (for the LED, typically 220Ω–1kΩ)
  • Breadboard & jumper wires

Hardware Setup

Push-Button (Master Side)

  • Connect one terminal of the push-button to a GPIO pin on the master (configured as input).
  • Use an internal or external pull-up or pull-down resistor to avoid floating signals.
  • Optionally, add a debounce circuit (hardware RC filter) or implement software debouncing.

LED (Slave Side)

  • Connect the LED to a GPIO pin on the slave (configured as output).
  • Add a current-limiting resistor (e.g., 330Ω) in series with the LED and GND.

SPI Connections between master and slave

Master PinSlave Pin
SCK(Clock)SCK
MOSI(Master Out Slave In)MOSI
MISO(Master In Slave Out)MISO 
SC/CS(Slave Select)SS

Note: If you only need one-way communication (Master → Slave), you can omit the MISO line.

What if Voltage Levels Are Different?

  • SPI signals (SCK, MOSI, MISO, CS) are digital logic signals. If the two microcontrollers operate at different logic voltages (e.g., 5V Arduino as Master and 3.3V ESP32 as Slave), a direct connection can damage the lower-voltage device or cause unreliable communication.
  • Example Scenario
    1. Arduino UNO (5V) → ESP32 (3.3V):
      • Arduino's output HIGH = 5V, which exceeds ESP32’s max 3.3V input tolerance, risking permanent damage.
  • Solutions
    1. Level Shifter IC (Preferred)
      • Use bidirectional level shifters for MOSI, MISO, and SCK lines.
    2. Voltage Divider (for MOSI & SCK going to 3.3V device)
      • Example: Two resistors (e.g., 10kΩ & 20kΩ) to step down 5V → 3.3V.
    3. Use 3.3V Logic-Compatible Devices (Simplest)
      • If possible, select both microcontrollers that operate at the same voltage level.

Important: The CS/SS line also needs level shifting if coming from a higher voltage device.

How It Works

  1. The master detects a button press (with debouncing).
  2. It sends a command byte (e.g., 0x01 = ON, 0x00 = OFF) over SPI.
  3. The slave receives the command and toggles the LED state (ON ↔ OFF).
  4. (Optional) The slave can send back a status byte for verification.

Expected Behavior

  • Each press of the button on the master toggles the LED on the slave (ON → OFF → ON → ...).

Implementation  (Pseudocode)

Master (pseudocode)

  • Initialize:
    • Configure the button GPIO as input with pull-up/pull-down.
    • Initialize SPI as Master (set clock polarity/phase, speed, MSB/LSB as required).
  • Loop:
    • Read button state and detect edge (press event with debounce).
    • On valid press, assert CS low.
    • Transmit command byte (e.g., 0x01).
    • Optionally read the response byte.
    • Deselect CS high.

Slave (pseudocode)

  • Initialize:
    • Configure LED GPIO as output (start OFF).
    • Initialize SPI as Slave with matching mode (CPOL/CPHA) and bit order.
  • On SPI receive interrupt or polling:
    • Read received byte.
    • If byte == 0x01, HIGH LED pin.
    • Optionally load a status byte into the SPI data register to be read by the Master.

Best Practices

  • Use the same SPI mode (CPOL/CPHA) on both master and slave.
  • Implement button debouncing (either software or hardware) to prevent false triggers.
  • Ensure a common ground between the two microcontrollers.
  • Keep the SPI clock speed within the slave’s capability.

So, by connecting and configuring the push-button switch, LED, and SPI communication, we can implement the task.

Below are the solutions to the given task using different microcontrollers

  1. STM32 as master and slave
  2. ESP32 as master and slave
  3. Arduino UNO as master and slave

Submit Your Solution

Note: Once submitted, your solution goes public, helping others learn from your approach!