Introduction: 3 Bit Binary Code Using Led and HAL Programming With STM 32

This is a tutorial blog on how to blink 3 LEDs in 8 different combinations. Since 3 in decimal equates to 8 bits in binary I would make it like a counter. The STM32 Black Pill, paired with the STM32CubeIDE, offers an excellent platform for learning embedded systems development. In this tutorial, we'll explore how to implement a sequence where three LEDs blink in various combinations. These combinations will include patterns such as 100, 010, 001, 110, and others, totaling nine distinct combinations. We'll achieve this using HAL (Hardware Abstraction Layer) programming, which simplifies low-level hardware access and configuration on STM32 microcontrollers. By following this tutorial, you'll gain hands-on experience in configuring GPIO pins, controlling LEDs, and creating custom sequences, laying a solid foundation for more complex embedded systems projects. Let's dive in and start blinking those LEDs in exciting patterns!

Supplies

  1. STM32 Black Pill
  2. Bread Board
  3. Male-Male jumper connector cables
  4. LED (3 nos.)
  5. C Type cable

Software:

  • STM32CubeIDE
  • STM32CubeProgrammer


Step 1: Making a New Project File in STM32CubeIDE

GOTO---->> File>New>STM32 Project

Step 2: ADD Configurations to STM 32Board

Search For Component- STM32F401CCU6 click on "Next".

Step 3: Open Main.c File

GOTO Project and find main.c file

Step 4: Configuring Your Board

configure your stm32bord as shown

Step 5: Configure Your Clock

Step 6: Connect LEDs to the Breadboard

  • Place the three LEDs on the breadboard.
  • Connect a 220 Ω resistor to each LED's anode (the longer leg).
  • Connect the cathode of each LED to a common ground line on the breadboard.
  • Connect the anode of each LED (through the resistor) to the corresponding GPIO pin on the STM32 board:


Step 7: Goto While(1) in Main() and Add Your Code

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,0);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,0);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_5,0);

HAL_Delay(500);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,1);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,0);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_5,0);

HAL_Delay(500);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,0);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,1);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_5,0);

HAL_Delay(500);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,1);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,1);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_5,0);

HAL_Delay(500);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,0);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,0);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_5,1);

HAL_Delay(500);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,1);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,0);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_5,1);

HAL_Delay(500);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,0);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,1);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_5,1);

HAL_Delay(500);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,1);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,1);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_5,1);

HAL_Delay(500);

  • Open your project in Project Explorer and follow this path; Core > Src > main.c.
  • In main.c, inside int main() code block go to while(1) to write the code to turn on/ off the LEDs in 8 different combinations.
  • Add delay so you are able to see the output.
  • Save the file and press on debug.


Step 8: Copying Path of .elf File From File Explorer

  • Right click on the STMProject or use Alt + Shift + W shortcut and press "System Explorer"
  • Open the project folder then "Debug" and copy path of .elf file.


Step 9: Open STMCubeProgrammer and Connect to STM Board Via USB

  • Once the file is saved in STM32CubeIDE connect the STM32 board after bootloader.

Check other instructables for more detailed steps.

  • Press on "Open file" and select the .elf from the path copied earlier.
  • Press on "Download" on the right.


  • Go to the "Erasing & programming" from the menu in the left.
  • Paste the File path copied earlier.
  • Check the "Full chip erase" and "Download file" and press on "Start automatic mode".


Step 10: Result

  • Once the chip is erased, the code will be ready to upload on the chip.
  • To upload, disconnect and reconnect the USB-C cable and your LEDs will start blinking.


Step 11: Test Your Circuit

  • Once the program is uploaded, the LEDs should blink in the 8 combinations with a 500 ms delay between each combination.
  • If everything works as expected, you've successfully created a simple LED-blinking tutorial with an STM32 microcontroller.


Step 12: Additional Tips:

  • You can change the delay time to increase or decrease the speed of LED transitions.
  • Experiment with different LED patterns to create unique blinking sequences.
  • Explore other GPIO functionalities, like interrupts, for more complex projects.