Getting Started with STM32G0 and STM32CubeIDE: Timer in Encoder Mode

In this guide, we shall take a look at the encoder types and how to use it with STM32G070.

In this guide, we shall cover the following:

  • What is encoder.
  • Connection.
  • Driver development.
  • Results.

1. What is Encoder:

rotary encoder, also called a shaft encoder, is an electro-mechanical device that converts the angular position or motion of a shaft or axle to analog or digital output signals. 

Rotary encoders are used in a wide range of applications that require monitoring or control, or both, of mechanical systems, including industrial controls, robotics, photographic lenses, computer input devices such as optomechanical mice and trackballs, controlled stress rheometers, and rotating radar platforms.

1.2 Encoder types:

  • Mechanical: Also known as conductive encoders. A series of circumferential copper tracks etched onto a PCB is used to encode the information via contact brushes sensing the conductive areas. Mechanical encoders areeconomical but susceptible to mechanical wear. They are common in human interfaces such as digital multimeters
  • Optical: This uses a light shining onto a photodiode through slits in a metal or glass disc. Reflective versions also exist. This is one of the most common technologies. Optical encoders are very sensitive to dust.
  • On-Axis Magnetic: This technology typically uses a specially magnetized 2 pole neodymium magnet attached to the motor shaft. Because it can be fixed to the end of the shaft, it can work with motors that only have 1 shaft extending out of the motor body. The accuracy can vary from a few degrees to under 1 degree. Resolutions can be as low as 1 degree or as high as 0.09 degree (4000 CPR, Count per Revolution). Poorly designed internal interpolation can cause output jitter, but this can be overcome with internal sample averaging.
  • Off-Axis Magnetic: This technology typically employs the use of rubber bonded ferrite magnets attached to a metal hub. This offers flexibility in design and low cost for custom applications. Due to the flexibility in many off axis encoder chips they can be programmed to accept any number of pole widths so the chip can be placed in any position required for the application. Magnetic encoders operate in harsh environments where optical encoders would fail to work. (from wikipedia)

1.3 Encoder Output:

In out case, the encoder shall generate pulses on both DT and CLK lines in a certain manner that can be read by the timer of stm32. In the figure below, is the output from shaft encoder when rotating clockwise. Clk is yellow and DT is Blue.

Clockwise rotation

When the shaft encoder rotate clockwise, CLK like pulled high first then DT line which indicates there is rotation.

When the counter clock rotation occurs, the DT line is pulled high first then the CLK line.

Counter Clockwise rotation

2. Encoder Connection:

The module is shown below:

The connection is as following

Encoder PinSTM32G070
GNDGND
+3V3
DTPA6
CLKPA7

3. Driver Development:

We start off by creating new STM32 Project and name it encoder.

From Pinout and Configuration, select Timer3:

Select the combined channel and select Encoder Mode.

This will automatically select PA6 and PA7 as encoder mode.

In parameters Settings:

Set encoder mode to TI1 and TI2.

Input filler to 10 or increase it according to your application.

Also, we need UART, please refer to this guide here.

Save the code project and it will generate the required.

In main.c

In user begin includes, include the following code header file:

#include "stdio.h"

In user begin PV, declare the following:

uint8_t uart_data[100];
uint16_t encoder_previous;

In user code begin 2:

Start the timer in encoder mode as following:

HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL);

In user code begin 3:

  if(TIM3->CNT != encoder_previous)
  {

	  uint32_t len =sprintf(uart_data,"Encoder Reading %d\r\n",TIM3->CNT);

	  HAL_UART_Transmit(&huart2, uart_data, len, 100);

	  encoder_previous=TIM3->CNT;

  }

Check if the current value of the timer is not equal to the store value, if it is not, print the new value and store the current value.

4. Results:

Upload the code to your board and open serial terminal and set the baud rate to 115200. Rotate the encoder and you should get the following:

Happy coding 🙂

2 Comments

  • Andrey Poletimov Posted March 19, 2024 2:03 pm

    Hi. It seems to me that it is better to clarify the phrase: in user code begin 3: and write that you need to add the code inside the loop while

    • Husamuldeen Posted March 21, 2024 7:58 am

      Hi,
      this is automatically generated by CubeMX to put the user code within these sections in case you want to modify the CubeMX, the new generated code won’t delete the user code.

Add Comment

Your email address will not be published. Required fields are marked *