< async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"">

Thursday, December 24, 2015

Adding Rotary encoder to arduino projects- quick start

DDS with Rotary encoder and ili9341tft

Rotary encoder is a handy add-on for most of the arduino projects which will serve as a measurement instruments, signal generator, direct digital synthesis (dds) vfo and several other projects.  Adding an encoder to your project is pretty easy. In this post i will take in to it quickly. This tutorial is based on a simple and inexpensive encoder with a click button. The click on the encoder is useful for menu selection etc and the rotation is useful for changing the values. The main purpose of this post is to help the addition of an encoder to my dds cum vswr analyser project -  A simple standalone antenna analyzer based on arduino and ad9850 with ili9341tft

Connecting rotary encoder to arduino projects

The encoder has three pins on one side  and two on the other side. The two pins are similar to a click button and can be used as a simple switch. When we push the shaft of the encoder, it closes the switch. Rotation of the encoder triggers the pins on the other side. There are three pins and the central one is connected to ground and the other two pins goes to D2 and D3 on the arduino. If we use the interrupts on the arduino/atmega328, it is important to connect this to hardware interrupt pins on the arduino board. In other models of arduino board see this link to identify the valid interrupt pins which are usable.

BoardDigital Pins Usable For Interrupts
Uno, Nano, Mini, other 328-based2, 3
Mega, Mega2560, MegaADK2, 3, 18, 19, 20, 21
Micro, Leonardo, other 32u4-based0, 1, 2, 3, 7
Zeroall digital pins, except 4
Dueall digital pins

When we turn the encoder, it triggers the interrupt and the code blocks inside the ISR(PCINT2_vect)
function will be triggerd. See the example code to learn more

A library make things easier and can be downloaded here Rotary Library (download and extract to library folder - instruction )

Click button

As mensioned earlier it is a simple push button. To add more possibilities like single click , double click etc we use a library called onebutton (more info) and Download (install instruction)

Attached below is a code which uses an ili9341 tft with rotary. You can comment out all lines starting with tft and can test the library and encoder with serial output. Setting up and wiring the tft is same as in this post - Quickly test an ILI9341 TFT display with an arduino [quick test]

Source Code

#include <stdint.h>  
#include <TFTv2.h>  
#include <SPI.h>  
#include <OneButton.h>  
#include <Rotary.h>  
 Rotary r = Rotary(3, 2);  
 OneButton button(A0,true);  
 void setup()  
   //rotory interrupt  
   PCICR |= (1 << PCIE2);  
   PCMSK2 |= (1 << PCINT18) | (1 << PCINT19);  
   // click on the encoder  
   Serial.println("Testing Rotary with ili9341 tft");  
 void loop()  
   // keep watching the push button:  
   // You can implement other code in here or just wait a while  
 void doubleclick() {  
   Tft.fillScreen(0, 320, 0, 220,BLUE) ;  
 void singleclick() {  
   Tft.fillScreen(0, 320, 0, 220,BLUE) ;  
 void longclick() {  
   Tft.fillScreen(0, 320, 0, 220,BLUE) ;  
 ISR(PCINT2_vect) {  
   unsigned char result = r.process();  
   if (result == DIR_NONE) {  
     // do nothing  
   else if (result == DIR_CW) {  
     Tft.fillScreen(0, 320, 0, 220,BLUE);  
   else if (result == DIR_CCW) {  
     Tft.fillScreen(0, 320, 0, 220,BLUE);  


  1. This comment has been removed by the author.

    1. It is a part of avr-libc. See https://www.arduino.cc/en/Tutorial/UsingAVRlibraries

    2. This comment has been removed by the author.

    3. Make sure to choose the correct board in the arduino ide. For e.g if using arduino uno, choose it in the board settings. Try updating the arduino software.

    4. This comment has been removed by the author.

  2. Hi ! Is it going to work anyway if I cut the two pins just to use the three pins side ?
    (On rotary encoder PEC11R-4215F-N0012 to transform it into PEC11R-4315F-N0012) ?