Using a Rotary Encoder as an input (with 7 segment display output)

This is a quick tutorial on how to use a rotary encoder as an input.
I will also show the circuit needed to operate a 7 segment display through a MAX7219 LED driver.


  • a Rotary Encoder (I used this one)
  • a MAX7219 LED driving IC (available here)
  • an Arduino (available here)
  • your choice of mounting method (I used a breadboard and jumper wires)

Here is the schematic generated using Fritzing.
The main reason it is messy is because the pin layout of the MAX7219 doesn’t translate to the pinout on the 7 segment display.


As you can see, the rotary encoder uses two pins on the Arduino (plus an extra pin for the push button functionality that the model I used has).

The MAX7219 uses 3 pins on the Arduino.

This is how the circuit could be implemented using strip-board; making it a lot neater than breadboard and wires.

rotary enc_layout

Here’s a photo of mine. Its a mess because it is implemented with jumper wires.


Once the circuit is implemented, upload this sketch to the Arduino and it all should work.
(reference for the rotary encoder code)
(reference for the MAX7219 LEDcontrol library)

//We always have to include the library
#include "LedControl.h"

/* Rotary encoder read example */
#define ENC_A 14
#define ENC_B 15

#define ENC_PUSH 4

 Now we need a LedControl to work with.
 ***** These pin numbers will probably not work with your hardware *****
 pin 12 is connected to the DataIn
 pin 11 is connected to the CLK
 pin 10 is connected to LOAD
 We have only a single MAX72XX.
LedControl lc=LedControl(12,11,10,1);

void setup()
  /* Setup encoder pins as inputs */
  pinMode(ENC_A, INPUT);
  digitalWrite(ENC_A, HIGH);
  pinMode(ENC_B, INPUT);
  digitalWrite(ENC_B, HIGH);
  pinMode(ENC_PUSH, INPUT);
  digitalWrite(ENC_PUSH, HIGH);

//  Serial.begin (115200);
//  Serial.println("Start");
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
  /* Set the brightness to a medium values */
  /* and clear the display */

//  lc.setDigit(0,0,0,false);
//  lc.clearDisplay(0);

void loop()
 static uint8_t counter = 4

 ;      //this variable will be changed by encoder input
 int8_t tmpdata;
  tmpdata = read_encoder();
  if( tmpdata ) {
//    Serial.print("Counter value: ");
//    Serial.println(counter/4, DEC);
  counter += tmpdata;
  if (!digitalRead(ENC_PUSH)){
    counter = 0;

/* returns change in encoder state (-1,0,1) */
int8_t read_encoder()
  static int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
  static uint8_t old_AB = 0;
  old_AB <<= 2;                   //remember previous state
  old_AB |= ( ENC_PORT & 0x03 );  //add current state
  return ( enc_states[( old_AB & 0x0f )]);


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s