.. 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 [|link_sf_facebook|] and join today!
.. _2.1.6_c:
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.
.. image:: ../img/Part_two_25.png
It's definitely convenient to buy a whole kit, here's the link:
.. list-table::
:widths: 20 20 20
:header-rows: 1
* - Name
- ITEMS IN THIS KIT
- LINK
* - Raphael Kit
- 337
- |link_Raphael_kit|
You can also buy them separately from the links below.
.. list-table::
:widths: 30 20
:header-rows: 1
* - COMPONENT INTRODUCTION
- PURCHASE LINK
* - :ref:`cpn_gpio_board`
- |link_gpio_board_buy|
* - :ref:`cpn_breadboard`
- |link_breadboard_buy|
* - :ref:`cpn_wires`
- |link_wires_buy|
* - :ref:`cpn_rotary_encoder`
- |link_rotary_encoder_buy|
**Schematic Diagram**
------------------------
.. image:: ../img/image349.png
:align: center
Experimental Procedures
-----------------------
**Step 1:** Build the circuit.
.. image:: ../img/2.1.6_fritzing.png
:align: center
**Step 2:** Open the code file.
.. raw:: html
.. code-block::
cd ~/raphael-kit/c/2.1.6/
**Step 3:** Compile the code.
.. raw:: html
.. code-block::
gcc 2.1.6_RotaryEncoder.c -lwiringPi
**Step 4:** Run.
.. raw:: html
.. code-block::
sudo ./a.out
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.
.. note::
If it does not work after running, or there is an error prompt: \"wiringPi.h: No such file or directory\", please refer to :ref:`install_wiringpi`.
**Code**
.. code-block:: c
#include
#include
#include
#include
#include
#define clkPin 0
#define dtPin 1
#define swPin 2
static volatile int globalCounter = 0 ;
unsigned char flag;
unsigned char Last_dtPin_Status;
unsigned char Current_dtPin_Status;
void btnISR(void)
{
globalCounter = 0;
}
void rotaryDeal(void)
{
Last_dtPin_Status = digitalRead(dtPin);
while(!digitalRead(clkPin)){
Current_dtPin_Status = digitalRead(dtPin);
flag = 1;
}
if(flag == 1){
flag = 0;
if((Last_dtPin_Status == 0)&&(Current_dtPin_Status == 1)){
globalCounter --;
}
if((Last_dtPin_Status == 1)&&(Current_dtPin_Status == 0)){
globalCounter ++;
}
}
}
int main(void)
{
if(wiringPiSetup() < 0){
fprintf(stderr, "Unable to setup wiringPi:%s\n",strerror(errno));
return 1;
}
pinMode(swPin, INPUT);
pinMode(clkPin, INPUT);
pinMode(dtPin, INPUT);
pullUpDnControl(swPin, PUD_UP);
if(wiringPiISR(swPin, INT_EDGE_FALLING, &btnISR) < 0){
fprintf(stderr, "Unable to init ISR\n",strerror(errno));
return 1;
}
int tmp = 0;
while(1){
rotaryDeal();
if (tmp != globalCounter){
printf("%d\n", globalCounter);
tmp = globalCounter;
}
}
return 0;
}
**Code Analysis**
* Read dtPin value when clkPin is low.
* When clkPin is high, if dtPin goes from low to high, the count decreases, otherwise the count increases.
* swPin will output low when the shaft is pressed.
From this, the program flow is shown below:
.. image:: ../img/2.1.6_flow.png
:align: center
**Phenomenon Picture**
-------------------------
.. image:: ../img/2.1.6rotary_ecoder.JPG
:align: center