Introduction: Mastering STM32 Black Pill and STM Cube IDE: a Step-by-Step Guide to Display 'Chitkara University' on 16x2 LCD

As an embedded systems enthusiast, I have always been fascinated by the capabilities of the STM32 Black Pill and the STM Cube IDE. In this article, I will provide a step-by-step guide on how to display 'Chitkara University' on a 16x2 LCD using the STM32 Black Pill and the STM Cube IDE.

Supplies

  1. STM32 black pill board
  2. USB –Type C cable
  3. LCD 16x2
  4. Potentiometer- 10K
  5. Connecting jumper cables (M-M, F-M)
  6. Breadboard

Step 1: Introduction to STM32 Black Pill and STM Cube IDE

The STM32 Black Pill is a low-cost development board based on the STM32F103C8T6 microcontroller. It is a popular choice among hobbyists and students due to its small size, low cost, and powerful features. The STM Cube IDE, on the other hand, is an integrated development environment (IDE) designed specifically for STM32 microcontrollers. It provides a comprehensive set of tools for writing, compiling, and uploading code to STM32 microcontrollers.

Step 2: Overview of 16x2 LCD

A 16x2 LCD is a common type of liquid crystal display (LCD) that can display up to 16 characters per line and has two lines. It is widely used in various electronic devices such as calculators, digital clocks, and small appliances. The LCD consists of a glass panel with a layer of liquid crystal material sandwiched between two polarizing filters. The LCD has a backlight that illuminates the display for easy viewing.

Step 3: Pin Configuration of 16x2 LCD

Before we can start displaying text on the 16x2 LCD, we need to understand its pin configuration. The 16x2 LCD has 16 pins, which are used for power, ground, data, and control signals. The following table shows the pin configuration of the 16x2 LCD:

Pin1 (Ground/Source Pin): This is a GND pin of display, used to connect the GND terminal of the microcontroller unit or power source.

Pin2 (VCC/Source Pin): This is the voltage supply pin of the display, used to connect the supply pin of the power source.

Pin3 (V0/VEE/Control Pin): This pin regulates the difference of the display, used to connect a changeable POT that can supply 0 to 5V.

Pin4 (Register Select/Control Pin): This pin toggles among command or data register, used to connect a microcontroller unit pin and obtains either 0 or 1(0 = data mode, and 1 = command mode).

Pin5 (Read/Write/Control Pin): This pin toggles the display among the read or writes operation, and it is connected to a microcontroller unit pin to get either 0 or 1 (0 = Write Operation, and 1 = Read Operation).

Pin 6 (Enable/Control Pin): This pin should be held high to execute Read/Write process, and it is connected to the microcontroller unit & constantly held high.

Pins 7-14 (Data Pins): These pins are used to send data to the display. These pins are connected in two-wire modes like 4-wire mode and 8-wire mode. In 4-wire mode, only four pins are connected to the microcontroller unit like 0 to 3, whereas in 8-wire mode, 8-pins are connected to microcontroller unit like 0 to 7.

Pin15 (+ve pin of the LED): This pin is connected to +5V

Pin 16 (-ve pin of the LED): This pin is connected to GND.

Step 4: Setting Up the STM Cube IDE Environment

To set up the STM Cube IDE environment, we first need to download and install the latest version of the STM Cube IDE from the official website. Once installed, we can open the IDE and create a new project by following these steps:

  1. Click on 'File' > 'New' > 'STM32 Project'
  2. Select the STM32F01CCU6 microcontroller from the list of available devices
  3. Choose a project name and location
  4. Click on 'Finish'

Once the project is created, we can start configuring the STM32 Black Pill.


Step 5: Configuring STM32 Black Pill

To configure the STM32 Black Pill, we need to do the following steps :

  1. Select Pin from A0 to A5 as GPIO_OUTPUT pins.
  2. Now select the mode as RCC and in its connectivity setting select I2C1 option.
  3. Now, we will configure the clock.
  4. Then select the Button called Device Configuration Tool Code Generation in the top taskbar menu.
  5. It will give us the layout for code in which we write our own code.

Once the settings are configured, we can start writing code to display text on the 16x2 LCD.

Step 6: Writing Code to Display 'Chitkara University' on 16x2 LCD

To display 'Chitkara University' on the 16x2 LCD, we need to write code that sends the appropriate data and control signals to the LCD. We can use the following code as a starting point :

Write The Following Code In main.c File -

#define SLAVE_ADDRESS_LCD 0x4E

#include "stdlib.h"

write this code after I2C_HandleTypeDef hi2c1;

char str[40];


void lcd_send_data (char data)

  {

  char data_u, data_l;

  uint8_t data_t[4];

  data_u = (data&0xf0);

  data_l = ((data<<4)&0xf0);

  data_t[0] = data_u|0x0D; //en=1, rs=1

  data_t[1] = data_u|0x09; //en=0, rs=1

  data_t[2] = data_l|0x0D; //en=1, rs=1

  data_t[3] = data_l|0x09; //en=0, rs=1

  HAL_I2C_Master_Transmit (&hi2c1, SLAVE_ADDRESS_LCD,(uint8_t *) data_t, 4, 100);

  }


void lcd_send_cmd (char cmd)

  {

   char data_u, data_l;

   uint8_t data_t[4];

   data_u = (cmd&0xf0);

   data_l = ((cmd<<4)&0xf0);

   data_t[0] = data_u|0x0C; //en=1, rs=0

   data_t[1] = data_u|0x08; //en=0, rs=0

   data_t[2] = data_l|0x0C; //en=1, rs=0

   data_t[3] = data_l|0x08; //en=0, rs=0

   HAL_I2C_Master_Transmit (&hi2c1, SLAVE_ADDRESS_LCD,(uint8_t *) data_t, 4, 100);

  }


void lcd_init (void)

 {

  // 4 bit initialisation

  HAL_Delay(50); // wait for >40ms

  lcd_send_cmd (0x30);

  HAL_Delay(5); // wait for >4.1ms

  lcd_send_cmd (0x30);

  HAL_Delay(1); // wait for >100us

  lcd_send_cmd (0x30);

  HAL_Delay(10);

  lcd_send_cmd (0x20); // 4bit mode

  HAL_Delay(10);


  // dislay initialisation

  lcd_send_cmd (0x28); // Function set --> DL=0 (4 bit mode), N = 1 (2 line display) F = 0 (5x8 characters)

  HAL_Delay(1);

  lcd_send_cmd (0x08); //Display on/off control --> D=0,C=0, B=0 ---> display off

  HAL_Delay(1);

  lcd_send_cmd (0x01); // clear display

  HAL_Delay(1);

  HAL_Delay(1);

  lcd_send_cmd (0x06); //Entry mode set --> I/D = 1 (increment cursor) & S = 0 (no shift)

  HAL_Delay(1);

  lcd_send_cmd (0x0C); //Di splay on/off control --> D = 1, C and B = 0. (Cursor and blink, last two bits)

 }


void lcd_send_string (char *str)

  {

  while (*str) lcd_send_data (*str++);

  }

  void setCursor(int a, int b)

  {

  switch (b)

      {

        case 0:

          b |= 0x80;

          break;

        case 1:

          b |= 0xC0;

          break;

      }

      lcd_send_cmd (b);

      lcd_send_cmd (a);

   }

Write this code in while (1) Loop :

 lcd_init();

  setCursor(0,0);

  lcd_send_string("CHITKARA");

  HAL_Delay(1000);

  setCursor(0,1);

  lcd_send_string("UNIVERSITY");

  HAL_Delay(1000);



Step 7: Compiling and Uploading the Code to STM32 Black Pill

Once we have written the code, we can compile it by clicking on 'Project' > 'Build All'. This will generate a binary file that we can upload to the STM32 Black Pill using a USB cable and a flash programmer such as ST-Link.

Step 8: Troubleshooting Common Issues

If the code does not work as expected, we can troubleshoot common issues by checking the connections, verifying the pin configuration, and debugging the code using the STM Cube IDE.

Step 9: Conclusion

In this article, we have provided a step-by-step guide on how to display 'Chitkara University' on a 16x2 LCD using the STM32 Black Pill and the STM Cube IDE. We have discussed the pin configuration of the 16x2 LCD, the STM Cube IDE environment, and the code required to display text on the LCD. With this knowledge, readers can explore the capabilities of the STM32 Black Pill and the STM Cube IDE and create their own embedded systems projects.


CTA: Start your STM32 Black Pill and STM Cube IDE journey today and explore the world of embedded systems.