Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

2.1.6 Rotary Encoder Module

Introduction

In this project, you will learn about Rotary Encoder. A rotary encoder is an electronic switch with a set of regular pulses in strictly timing sequence. When used with IC, it can achieve increment, decrement, page turning and other operations such as mouse scrolling, menu selection, and so on.

Required Components

In this project, we need the following components.

../_images/Part_two_25.png

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Raphael Kit

337

Raphael Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

GPIO Extension Board

BUY

Breadboard

BUY

Jumper Wires

BUY

Rotary Encoder Module

BUY

chematic Diagram

../_images/image349.png

Experimental Procedures

Step 1: Build the circuit.

../_images/2.1.6_fritzing.png

In this example, we can connect the Rotary Encoder pin directly to the Raspberry Pi using a breadboard and 40-pin Cable, connect the GND of the Rotary Encoder to GND, 「+」to 5V, SW to digital GPIO27, DT to digital GPIO18, and CLK to digital GPIO 17.

Step 2: Go to the folder of the code.

cd ~/raphael-kit/nodejs/

Step 3: Run the code.

sudo node rotary_encoder_module.js

You will see the count on the shell. When you turn the rotary encoder clockwise, the count is increased; when turn it counterclockwise, the count is decreased. If you press the switch on the rotary encoder, the readings will return to zero.

Code

const Gpio = require('pigpio').Gpio;

const clkPin = new Gpio(17, {
  mode: Gpio.INPUT,
  pullUpDown: Gpio.PUD_DOWN,
  edge: Gpio.RISING_EDGE
});
const dtPin = new Gpio(18, {
  mode: Gpio.INPUT,
  pullUpDown: Gpio.PUD_DOWN,
});
const swPin = new Gpio(27, {
  mode: Gpio.INPUT,
  pullUpDown: Gpio.PUD_UP,
  edge: Gpio.FALLING_EDGE
});

var globalCounter = 0;

clkPin.on('interrupt',()=>{
  if(dtPin.digitalRead()==1){
      globalCounter--;
  }
  else{
      globalCounter++;
  }
  console.log(`globalCounter = ${globalCounter}`);
});

swPin.on('interrupt', () => {
  globalCounter = 0;
  console.log(`globalCounter = ${globalCounter}`);
});

Code Explanation

var globalCounter = 0;

clkPin.on('interrupt',()=>{
  if(dtPin.digitalRead()==1){
      globalCounter--;
  }
  else{
      globalCounter++;
  }
  console.log(`globalCounter = ${globalCounter}`);
});

When dtPin goes from low to high, if clkPin is high, the count decreases, otherwise the count increases.

swPin.on('interrupt', () => {
  globalCounter = 0;
  console.log(`globalCounter = ${globalCounter}`);
});

The swPin will output low when the shaft is pressed. Let the globalCounter go to zero at this point

Phenomenon Picture

../_images/2.1.6rotary_ecoder.JPG