Rotary Encoder - Measure Shaft Rotation

  1. Introduction:

This experiment focuses on measuring the rotation of a shaft using a rotary encoder connected to a Raspberry Pi Pico microcontroller. A rotary encoder converts rotational movement into digital signals, which can be used to determine both the direction of rotation (clockwise or anticlockwise) and the number of rotations. The experiment aims to detect the direction of rotation and count the total rotations in a system.



  1. Scope:

The project covers:

  • Using the Raspberry Pi Pico and rotary encoder to detect rotational direction.

  • Measuring and displaying the number of rotations on the console.

  • Writing the application code in Embedded C and MicroPython.

This system is useful in applications such as robotics, control systems, and mechanical measurements where accurate rotational feedback is necessary.





  1. Prerequisite:

3.1. Hardware:

  • Pico microcontroller

  • Rotary encoder

  • Breadboard

  • Jumper wires

  • USB cable (to connect the computer)

3 .2. Software:

  • Thonny Software(For micro python)

  • MicroPython firmware installed on Raspberry Pi Pico

  • ubuntu Terminal(For embedded C)

  • CMake,Pico SDK,GNU Arm Embedded Toolchain,WSL.



  1. Connection Details:

CLK (Clock) pin of the rotary encoder to GPIO 16 of the Raspberry Pi Pico.

DT (Data) pin of the rotary encoder to GPIO 17 of the Raspberry Pi Pico.

SW (Switch) pin is not used in this experiment (button detection is not included).

GND and VCC of the rotary encoder connected to GND and 3.3V on the Raspberry Pi Pico.

UART Serial Console: Use a micro-USB cable to connect the Pico to the PC, then open PuTTY and select the correct COM port and baud rate (115200).



  1. Working Principle:

A rotary encoder works by generating two square wave signals from the CLK and DT pins. The phase relationship between these signals determines the direction of rotation:

  • If CLK leads DT, the shaft is rotating clockwise.

  • If DT leads CLK, the shaft is rotating anticlockwise.

The software counts these signals and calculates the total rotation by analyzing the phase difference between CLK and DT.

Working of Rotary Encoder

Imagine you have a knob that you can turn in either direction. A rotary encoder is like a clever device that can detect and measure the movement and rotation of that knob. It can tell you how much the knob has been turned and in which direction.

A rotary encoder typically consists of a rotating shaft and two parts called the "A" and "B" channels. As you turn the knob, these channels produce a series of electrical signals (when connected to a common ground) that the encoder can understand. It uses these signals to keep track of the knob's position and movement.

The great thing about a rotary encoder is that it can provide precise and accurate feedback about how much the knob has been turned. It can detect even the smallest rotations, whether you're turning the knob slowly or quickly. It can also detect when it is pushed like a button in the center.

Rotary encoders are commonly used in various applications. For example, they can be found in volume control knobs on audio equipment, as well as in navigation systems, industrial machinery, and robotics. They provide a convenient and reliable way to input rotational movements into electronic systems.

Rotary Encoder Pinout

Here's the explanation of each pin:

SW (Switch): This pin corresponds to the integrated push-button switch on the rotary encoder. When the button is pressed, this pin typically outputs a logical LOW or GND signal, indicating a button press event.

VCC (Positive Supply): This pin is connected to the positive supply voltage of the circuit, often referred to as Vcc. It provides the power supply for the rotary encoder.

GND (Ground): This pin is connected to the ground or 0V reference of the circuit. It provides the common ground connection for the rotary encoder.

DT (Data) or Channel B: This pin provides additional signals to help determine the direction of rotation. By analyzing the order and timing of the signals from this pin and the CLK pin, the direction of rotation can be determined.

CLK (Clock) or Channel A: This pin outputs pulses or signals that indicate the rotation of the encoder. Each pulse corresponds to a specific amount of rotation in either clockwise or counterclockwise direction.



  1. Application code:

  • MicroPython:

from machine import Pin

from time import sleep


# Define GPIO pins for the rotary encoder

clk_pin = Pin(24, Pin.IN)

dt_pin = Pin(25, Pin.IN)


rotation_count = 0

last_clk_state = clk_pin.value()


def detect_rotation():

global rotation_count, last_clk_state

clk_state = clk_pin.value()

dt_state = dt_pin.value()


# Detect change in CLK state

if clk_state != last_clk_state:

if dt_state != clk_state:

# Clockwise rotation

rotation_count += 1

print("Clockwise:", rotation_count)

else:

# Anticlockwise rotation

rotation_count -= 1

print("Anticlockwise:", rotation_count)


last_clk_state = clk_state


while True:

detect_rotation()

sleep(0.005) # Debounce delay


  • Embedded C:

#include "pico/stdlib.h"


// Define GPIO pins for the rotary encoder

#define CLK_PIN 24

#define DT_PIN 25


int rotation_count = 0;

int last_clk_state;


void setup() {

gpio_init(CLK_PIN);

gpio_init(DT_PIN);


gpio_set_dir(CLK_PIN, GPIO_IN);

gpio_set_dir(DT_PIN, GPIO_IN);


last_clk_state = gpio_get(CLK_PIN);

}


void loop() {

int clk_state = gpio_get(CLK_PIN);

int dt_state = gpio_get(DT_PIN);


if (clk_state != last_clk_state) {

if (dt_state != clk_state) {

// Clockwise rotation

rotation_count++;

printf("Clockwise: %d\n", rotation_count);

} else {

// Anticlockwise rotation

rotation_count--;

printf("Anticlockwise: %d\n", rotation_count);

}

}


last_clk_state = clk_state;

sleep_ms(5); // Debounce delay

}


int main() {

stdio_init_all();

setup();

while (1) {

loop();

}

return 0;

}



  1. Hardware Image:



  1. Output:

When the shaft of the rotary encoder is turned:

  • The console will display "Clockwise" with an increasing count when turned clockwise.

  • It will display "Anticlockwise" with a decreasing count when turned anticlockwise.





  1. Video Demonstration:





  1. Appendix:

  • Common CMakeLists:

cmake_minimum_required(VERSION 3.12)


# Pull in SDK (must be before project)

include(pico_sdk_import.cmake)


project(pico_experiments C CXX ASM)


set(CMAKE_C_STANDARD 11)

set(CMAKE_CXX_STANDARD 17)


if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")

    message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")

endif()


# Initialize the SDK

pico_sdk_init()


add_compile_options(-Wall

        -Wno-format          # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int

        -Wno-unused-function # we have some for the docs that arent called

        )

if (CMAKE_C_COMPILER_ID STREQUAL "GNU")

    add_compile_options(-Wno-maybe-uninitialized)

endif()


# Hardware-specific examples in subdirectories:

add_subdirectory(expt_10_LCD)

  • CMakeLists:

cmake_minimum_required(VERSION 3.13)

include(pico_sdk_import.cmake)


project(rotary_encoder)


pico_sdk_init()


add_executable(rotary_encoder

main.c

)


target_link_libraries(rotary_encoder pico_stdlib)

pico_add_extra_outputs(rotary_encoder)

  1. References:



Class Schedules:
# Topic Date & time Action

Curriculum

0% Completed (0/1)