Rotary Encoder With STM32 Nucleo Board

by Avinash KumarP in Circuits > Microcontrollers

14084 Views, 8 Favorites, 0 Comments

Rotary Encoder With STM32 Nucleo Board

9.png

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.

Install STM32CUBEMX and Keil With Packages for STM32L476.

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

Selecting Microcontroller in STM32CUBEMX

Open cubemx and select nucleo64 board with microcontroller as STM32L476

Make Necessary Selections in STM32cubemx According to Images Shown in This Tutorial

1.png
2.png
3.png

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)

Generate Code for UVision Keil .

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.

4.png

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.

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

5.png

Write Code in STM32L4xx_it.c File in Keil

6.png

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

Add Variables in Both Files.

7.png
8.png

Add variables in both files.See file attached.

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.)

Compile Your Code.

Compile the code and Debug if any errors come.

Program the Board With Microcontroller.

Rotary Encoder with STM32 Nucleo board

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