LED cubes are an excellent way to learn about electronics, microcontroller programming, and creative visual effects. This project involves building a 4x4x4 LED matrix and controlling it with an STM32F401 microcontroller. The 4x4x4 LED cube consists of 64 LEDs arranged in 4 layers, with each layer containing 16 LEDs. Using multiplexing, we light up individual LEDs or create dynamic patterns without requiring 64 GPIO pins.
How the 4x4x4 LED Cube Works
-
Hardware Layout:
- The cube is structured in a grid where each LED is connected to a row and column.
- Each layer has a common cathode, and the columns connect the anodes.
-
Multiplexing Control:
- Only one layer is powered at a time, and the LEDs within that layer are controlled using the column connections.
- By rapidly switching between layers (persistence of vision), we create the illusion that all LEDs are on simultaneously.
-
STM32 Control:
- GPIO pins control the rows and columns, while timer interrupts or DMA handle multiplexing efficiently.
Components Required
- STM32F401 Development Board (e.g., Nucleo F401RE)
- 64 LEDs
- 16 Resistors (for current limiting)
- 4 NPN transistors (for layer switching)
- 4x4x4 LED matrix structure
- Breadboard and wires
Circuit Design
LED Cube Connections
- Anodes (Columns):
Each column is connected to a GPIO pin through a current-limiting resistor. - Cathodes (Layers):
Each layer is connected to the collector of an NPN transistor. The transistor base is controlled by a GPIO pin through a resistor.
STM32 Connections
- Use 16 GPIO pins for columns.
- Use 4 GPIO pins for layers via transistors.
- Configure additional pins for debugging or input (optional).
Code Implementation
STM32CubeIDE Setup
-
GPIO Configuration:
- Set 16 pins as outputs for controlling columns.
- Set 4 pins as outputs for layer selection (with transistors).
-
Timers:
- Use a timer to manage layer switching (e.g., every 2 ms).
-
Interrupts or DMA:
- For efficient LED control, use interrupts or DMA to update the column data.
Example Code
#include "stm32f4xx_hal.h"
#define NUM_LAYERS 4
#define NUM_COLUMNS 16
uint16_t column_data[NUM_LAYERS][NUM_COLUMNS] = {
{0x1, 0x2, 0x4, 0x8, ...}, // Pattern for Layer 0
{0x10, 0x20, 0x40, 0x80, ...}, // Pattern for Layer 1
...
};
volatile uint8_t current_layer = 0;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if (htim->Instance == TIM3) { // Timer 3 handles multiplexing
// Turn off all layers
HAL_GPIO_WritePin(GPIOx, LAYER_PINS, GPIO_PIN_RESET);
// Set column data for the current layer
for (int col = 0; col < NUM_COLUMNS; col++) {
HAL_GPIO_WritePin(GPIOx, COLUMN_PINS[col],
(column_data[current_layer][col]) ? GPIO_PIN_SET : GPIO_PIN_RESET);
}
// Turn on the current layer
HAL_GPIO_WritePin(GPIOx, LAYER_PINS[current_layer], GPIO_PIN_SET);
// Move to the next layer
current_layer = (current_layer + 1) % NUM_LAYERS;
}
}
int main(void) {
HAL_Init();
SystemClock_Config();
// Initialize GPIO and Timer
MX_GPIO_Init();
MX_TIM3_Init();
HAL_TIM_Base_Start_IT(&htim3); // Start timer with interrupt
while (1) {
// Main loop can be used for updating patterns or other logic
}
}
Pattern Generation
Patterns are defined by updating the column_data
array. Examples include:
- Static Patterns: Light up specific LEDs.
- Dynamic Patterns: Rotate LEDs in a wave, spiral, or bounce effect.
- 3D Animations: Create flowing shapes, letters, or symbols.
Tools and Techniques
- STM32CubeMX: Configure GPIO pins and timers easily.
- HAL Library: Simplifies hardware interaction.
- Debugging: Use a serial interface or LEDs to debug patterns.
Videos
1. Prototype Testing Video
Conclusion
Building a 4x4x4 LED cube using STM32F401 is a rewarding project that combines hardware design, microcontroller programming, and creative coding. With multiplexing, we efficiently control all LEDs with minimal GPIO usage, creating mesmerizing visual effects. This project is a stepping stone to creating larger LED cubes or integrating more advanced features like Bluetooth control or music synchronization.
Take the first step, and let your imagination light up!
No comments:
Post a Comment