arduino stm32 encoder

Arduino STM32 Encoder Showdown: An In-Depth Comparison

By Robert Walker
AD

Arduino and STM32 are both popular in the world of electronic prototyping, each offering unique advantages. When it comes to encoder integration, both platforms have their distinct strengths. Let's dive into the Arduino STM32 encoder implementation, exploring the factors to consider, necessary components, and step-by-step setup for both platforms.

Before we delve into the technical aspects, understanding encoders is crucial. Encoders are position sensors that convert the angular position or motion of a shaft to a digital signal. They're used in various applications, including robotics, CNC machines, and servo systems, to monitor speed, position, or count events.

Arduino Encoder Setup

Arduino, with its simplicity and extensive library support, is an excellent choice for quick and easy encoder projects. Here, we'll use the popular Quadrature Encoder library.

AD
Arduino Rotary Encoder: Tutorial, Wiring, and Pinout
Arduino Rotary Encoder: Tutorial, Wiring, and Pinout
an electronic device connected to a micro board
an electronic device connected to a micro board
Better Arduino Rotary Encoder Circuit
Better Arduino Rotary Encoder Circuit

The basic Arduino encoder setup consists of an encoder, a pull-up resistor, and an Arduino board. The encoder's channel A and B signals are connected to Arduino's digital pins, while the encoder is powered via the 5V and GND pins.

Library Setup

Start by installing the QuadEncoder library via the Arduino IDE's Library Manager. Once installed, include it in your sketch as follows:

ESP32 - Rotary Encoder
ESP32 - Rotary Encoder
How Rotary Encoder Works and How To Use It with Arduino
How Rotary Encoder Works and How To Use It with Arduino
Arduino Rotary Encoder Menu - educ8s.tv - Watch Learn Build
Arduino Rotary Encoder Menu - educ8s.tv - Watch Learn Build

`#include `

Arduino Code

Here's a simple Arduino encoder setup:

AD
const int encoderPinA = 2;
const int encoderPinB = 3;

QuadEncoder encoder(encoderPinA, encoderPinB,QuadEncoder::EncoderMode::TICKS);

void setup() { Serial.begin(9600); }

void loop() { Serial.println(encoder.getPosition()); delay(100); }

STM32 Encoder Setup

STM32, with its power and flexibility, is ideal for more complex encoder projects. Here, we'll use the built-in TIM (Timer) module to read the encoder signals.

How to Use Industrial Encoders With Arduino
How to Use Industrial Encoders With Arduino
rotary encoder with arduino
rotary encoder with arduino
STM32 with Arduino IDE - iCircuit
STM32 with Arduino IDE - iCircuit
ESP32 - Rotary Encoder Servo Motor | ESP32 Tutorial
ESP32 - Rotary Encoder Servo Motor | ESP32 Tutorial
50 Arduino Sensors Every Maker Should Know
50 Arduino Sensors Every Maker Should Know
Rotary Encoder or Arduino 360 Degree Encoder
Rotary Encoder or Arduino 360 Degree Encoder

The STM32 encoder setup is similar to Arduino, with encoder channel A and B connected to TIM input capture channels, and the encoder powered via the 3.3V and GND pins.

STM32CubeMX Configuration

Open STM32CubeMX and follow these steps to configure the TIM module:

AD
  • Select the required encoder TIM module (e.g., TIM2 or TIM3).
  • Enable the encoder input (sensitive or non-sensitive) by setting the corresponding input to 'Encoder input'.
  • Select the generated interrupt and clock prescaler settings.
dzie

STM32 Encoder Code

Here's an example code snippet demonstrating encoder reading using the configured TIM module in an STM32 project. Ensure you've included the necessary headers and set up your STM32 board correctly:

Menu with voice & rotary encoder
Menu with voice & rotary encoder
Advanced menu system with rotary encoder for Arduino/STM32
Advanced menu system with rotary encoder for Arduino/STM32
Siri + Arduino
Siri + Arduino
How Rotary Encoder Works and How To Use It with Arduino
How Rotary Encoder Works and How To Use It with Arduino
Arduino Powered Rotary Encoders - I Made It at TechShop
Arduino Powered Rotary Encoders - I Made It at TechShop
Arduino Aquaponics: Rotary Encoders
Arduino Aquaponics: Rotary Encoders
Arduino Uno + STM32 Bluepill + STM32 Blackpill CAN Communication
Arduino Uno + STM32 Bluepill + STM32 Blackpill CAN Communication
Arduino Uno + STM32 Bluepill + STM32 Blackpill CAN Communication
Arduino Uno + STM32 Bluepill + STM32 Blackpill CAN Communication
Arduino DIY Electronics for Real-World Application
Arduino DIY Electronics for Real-World Application
an electronic device with two eyes on it's face and wires attached to the board
an electronic device with two eyes on it's face and wires attached to the board
Rotary Encoder Incremental rotary Encoder How to use it with Arduino
Rotary Encoder Incremental rotary Encoder How to use it with Arduino
Reading Absolute Encoder with Arduino
Reading Absolute Encoder with Arduino
Getting Started with STM32 Nucleo in Arduino IDE - LED Blinking
Getting Started with STM32 Nucleo in Arduino IDE - LED Blinking
Using a KY040 Rotary Encoder with Arduino
Using a KY040 Rotary Encoder with Arduino
TIM_HandleTypeDef htim2;
uint32_t encoder_value = 0;

void SystemClock_Config(void);
static void MX_TIM2_Init(void);

int main(void)
{
  HAL_Init();
  SystemClock_Config();
  MX_TIM2_Init();

  while (1)
  {
    encoder_value = TIM_GetCounter(&htim2);
    HAL_Delay(100);
  }
}

void MX_TIM2_Init(void)
{
  TIM_Encoder_InitTypeDef sConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};

  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 0;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 65535;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  sConfig.EncoderMode = TIM_ENCODERMODE_TI12;
  sConfig.InterruptEnable = TIM_INTERDUCTION(source,CalculatorSource1 | TIM_CH1 DetectivePresentTS1);
  if (HAL_TIM_IC_Init(&htim2) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_Encoder_Init(&htim2, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
}

Entering the world of Arduino STM32 encoder implementation opens up numerous possibilities for exciting projects. From robotics to CNC machines, the sky's the limit. So, grab your best hardware, choose your platform, and start building!

AD