Introduction: Rotary Encoder With STM32 Nucleo Board

About: We are team of Embedded Developers and love to share knowledge.

This is Tutorial for Getting position of Rotary Encoder ,which is a incremental type of encoder.Encoders are of two types generally:- one is incremental other is absolute.this code can be used for STM32L476 and STM32L0 microcontrollers.But if you have your own lcd library or code for LCD this will work for any STM32 microcontroller.

I am using STM32L476 nucleo board for this tutorial.

You can control motors like STEPPER motor or servo motor by writing a code according to movement of encoder.I have written such a code already.Please keep watching my tutorials for further.

Step 1: Install STM32CUBEMX and Keil With Packages for STM32L476.

Step 2: Make Electronics Interfacing for Your Project

Electronics components needed for this project is :-

1)16x2 alphanumeric LCD 2)STM32L476 nucleo board. 3)Bread board 4)Jumper wires. 5)One laptop with windows installed(6)Rotary Encoder. Connection of LCD and STM32L476 board are mentioned below:-

STM32L476 - LCD

GND - PIN1

5V - PIN2

NA - 2.2K resistor connected to GND

PB10 - RS

PB11 - RW

PB2 - EN

PB12 - D4

PB13 - D5

PB14 - D6

PB15 - D7

5V - PIN15

GND - PIN16

Connection of Rotary Encoder and STM32 are below

Rotary Encoder-STM BOARD

Power pin-3.3 V

GND-GND

CLK-PC1

DT-PC0

Step 3: Selecting Microcontroller in STM32CUBEMX

Open cubemx and select nucleo64 board with microcontroller as STM32L476

Step 4: Make Necessary Selections in STM32cubemx According to Images Shown in This Tutorial

Use above steps to make necessary selections in STM32Cubemx,and select maximum clock for that microcontroller you are using(STM32L476 I am using in this tutorial)

Step 5: Generate Code for UVision Keil .

Step 6: Write Code for LCD in Main.c File.Use This Step for Only for STM32L4 and STM32L0 Microcontrollers.For Other Microcontrollers Use Your Own Code.

Open main.c file from the projects ,menu of Keil and write code for LCD initialization before the while loop of main.Refer the figure attached.

Step 7: Write the Code in While Loop Inside Main.c File. Refer the File Attached

Step 8: Write Code in STM32L4xx_it.c File in Keil

Write code in STM32L4xx_it.c file in Keil.see code in file attached.

Step 9: Add Variables in Both Files.

Add variables in both files.See file attached.

Step 10: From Project Menu in Uvision Keil Go to Submenu Application/Users.

From Project menu in uvision Keil go to submenu Application/Users.Attach the lcd_hd44780_stml4xx.c file(Right click on the submenu and go to browse option and attach the file after copying three lcd files to the source folder of keil.)

Step 11: Compile Your Code.

Compile the code and Debug if any errors come.

Step 12: Program the Board With Microcontroller.

Program the board with microcontroller.Will get output like in this video.