Spark fun Rotary Encoder and MEGA Arduino

I have tried commenting out code and still not working, verified that the encoder is wired up correctly. 2 days later, I need help. if I could get a pointer to a libe that works with the Spark fun RGB encoder that would be great

// -----
// LimitedRotator.ino - Example for the RotaryEncoder library.
// This class is implemented for use with the Arduino environment.
// Copyright (c) by Matthias Hertel, htt
// This work is licensed u
// More information on: htt Arduino Projects
// -----
// 26.03.2017 created by Matthias Hertel
// 06.02.2021 conditions and settings added for ESP8266
// -----

// This example checks the state of the rotary encoder in the loop() function.
// The current position is printed on output when changed.
// In addition to the SimplePollRotator example here the range of the rotator is limited to the range 0 - 16 and only incremental steps of 2 are realized.
// To implement this limit the boundaries are checked and eventually the current position is adjusted.
// The internal (physical) position of the rotary encoder library remains by stepping with the increment 1
// so the the logical position is calculated by applying the ROTARYSTEPS factor.

// Hardware setup:
// Attach a rotary encoder with output pins to A2 and A3.
// The common contact should be attached to ground.

#include <Arduino.h>
#include <RotaryEncoder.h>

// Example for Arduino UNO with input signals on pin 2 and 3
#define PIN_IN1 2
#define PIN_IN2 3

#elif defined(ESP8266)
// Example for ESP8266 NodeMCU with input signals on pin D5 and D6
#define PIN_IN1 D5
#define PIN_IN2 D6


#define ROTARYMIN 0
#define ROTARYMAX 16

// Setup a RotaryEncoder with 4 steps per latch for the 2 signal input pins:
// RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::FOUR3);

// Setup a RotaryEncoder with 2 steps per latch for the 2 signal input pins:
RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::TWO03);

// Last known rotary position.
int lastPos = -1;

void setup()
while (! Serial);
Serial.println("LimitedRotator example for the RotaryEncoder library.");
encoder.setPosition(10 / ROTARYSTEPS); // start with the value of 10.
} // setup()

// Read the current position of the encoder and print out when changed.
void loop()

// get the current physical position and calc the logical position
int newPos = encoder.getPosition() * ROTARYSTEPS;

if (newPos < ROTARYMIN) {
encoder.setPosition(ROTARYMIN / ROTARYSTEPS);

} else if (newPos > ROTARYMAX) {
encoder.setPosition(ROTARYMAX / ROTARYSTEPS);
} // if

if (lastPos != newPos) {
lastPos = newPos;
} // if
} // loop ()

// The End

You should RE-edit your posting to show your code by using code-tags

Click the pencil-icon on your post.
delete all the lines of code.

There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

the NewEncoder-library from user gfvalvo works very reliable

general hints:

  • You should take away any work from your potential users that you can do on your own. This means provide a download-link of the datasheet of all devices that you are using.

  • write the exact type of microcontroller you are using.

  • describe the behaviour of your code

best regards Stefan

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.