Arduino MKR WiFi 1010 - Rotary Encoder

Arduino MKR WiFi 1010 - Rotary Encoder

Want to add a rotary encoder to your Arduino MKR WiFi 1010 project for precise control? In this tutorial, you’ll connect a rotary encoder to an Arduino MKR WiFi 1010, upload code to the Arduino MKR WiFi 1010, and track the rotary encoder’s direction and position in the Serial Monitor.

What you’ll learn (Arduino MKR WiFi 1010 + rotary encoder):

  • Connecting a rotary encoder to the Arduino MKR WiFi 1010
  • Programming the Arduino MKR WiFi 1010 to read rotary encoder direction and position
  • Using interrupts on the Arduino MKR WiFi 1010 for better rotary encoder performance
  • Detecting button presses on the rotary encoder with the Arduino MKR WiFi 1010

Real-world uses for Arduino MKR WiFi 1010 and rotary encoder:

  • Volume controls (Arduino MKR WiFi 1010 + rotary encoder for audio)
  • Menu navigation (Arduino MKR WiFi 1010 reads rotary encoder clicks)
  • Motor speed adjustment (Arduino MKR WiFi 1010 + rotary encoder)
  • Learning input devices on Arduino MKR WiFi 1010

You’ll see the Arduino MKR WiFi 1010 print rotary encoder data as you turn the knob clockwise or counterclockwise.

Arduino MKR WiFi 1010 rotary encoder

Hardware Preparation

1×Arduino MKR WiFi 1010
1×Micro USB Cable
1×Rotary Encoder
1×Breadboard
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of Rotary Encoder

A rotary encoder is like a knob that tells your Arduino MKR WiFi 1010 exactly how it’s being turned. Unlike a simple button, it detects direction and how many “clicks” you’ve made.

There are two main types:

  • Incremental encoder: Tracks relative movement (like steps up or down).
  • Absolute encoder: Remembers its position even after power off.

This tutorial focuses on the incremental rotary encoder, which is perfect for most Arduino MKR WiFi 1010 projects.

Rotary Encoder Module Pinout

rotary encoder pinout

The rotary encoder has these pins:

  • CLK (Output A): Sends pulses as you turn the knob.
  • DT (Output B): Helps detect the direction of rotation.
  • SW: The button pin (HIGH when not pressed, LOW when pressed).
  • VCC: Power (3.3V to 5V).
  • GND: Ground.

Rotary Encoder vs Potentiometer

Feature Rotary Encoder Potentiometer
Rotation Unlimited (full circle) Limited (quarter to half circle)
Output Digital pulses Analog voltage
Position tracking Relative (steps) Absolute (exact angle)
Best for Counting turns Precise positioning

How Rotary Encoder Works

rotary encoder output

Inside, a disk with slots creates signals as you turn it. The Arduino MKR WiFi 1010 reads these to figure out direction and steps.

How rotary encoder works

When CLK goes from LOW to HIGH:

  • If DT is HIGH, it’s counterclockwise.
  • If DT is LOW, it’s clockwise.

※ NOTE THAT:

Pin A and Pin B are connected to the CLK and DT pins. However, some manufacturers might use a different order. The code below has been tested with the rotary encoder from DIYables.

Wiring Diagram

The wiring diagram between Arduino MKR WiFi 1010 rotary encoder

This image is created using Fritzing. Click to enlarge image

Wiring Table

Rotary Encoder Arduino MKR WiFi 1010
CLK D3
DT D4
SW D5
VCC 3.3V
GND GND

Arduino MKR WiFi 1010 Code – Rotary Encoder

This code reads the rotary encoder’s direction and position without interrupts. It also detects button presses.

/* * This Arduino MKR WiFi 1010 code was developed by newbiely.com * * This Arduino MKR WiFi 1010 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-rotary-encoder */ #include <ezButton.h> // the library to use for SW pin #define CLK_PIN 3 #define DT_PIN 4 #define SW_PIN 5 #define DIRECTION_CW 0 // clockwise direction #define DIRECTION_CCW 1 // counter-clockwise direction int counter = 0; int direction = DIRECTION_CW; int CLK_state; int prev_CLK_state; ezButton button(SW_PIN); // create ezButton object that attach to pin 5 void setup() { Serial.begin(9600); // configure encoder pins as inputs pinMode(CLK_PIN, INPUT); pinMode(DT_PIN, INPUT); button.setDebounceTime(50); // set debounce time to 50 milliseconds // read the initial state of the rotary encoder's CLK pin prev_CLK_state = digitalRead(CLK_PIN); } void loop() { button.loop(); // MUST call the loop() function first // read the current state of the rotary encoder's CLK pin CLK_state = digitalRead(CLK_PIN); // If the state of CLK is changed, then pulse occurred // React to only the rising edge (from LOW to HIGH) to avoid double count if (CLK_state != prev_CLK_state && CLK_state == HIGH) { // if the DT state is HIGH // the encoder is rotating in counter-clockwise direction => decrease the counter if (digitalRead(DT_PIN) == HIGH) { counter--; direction = DIRECTION_CCW; } else { // the encoder is rotating in clockwise direction => increase the counter counter++; direction = DIRECTION_CW; } Serial.print("DIRECTION: "); if (direction == DIRECTION_CW) Serial.print("Clockwise"); else Serial.print("Counter-clockwise"); Serial.print(" | COUNTER: "); Serial.println(counter); } // save last CLK state prev_CLK_state = CLK_state; if (button.isPressed()) { Serial.println("The button is pressed"); } }

We use the ezButton library to simplify button handling.

Detailed Instructions

New to Arduino MKR WiFi 1010? Complete our Getting Started with Arduino MKR WiFi 1010 tutorial first to set up your development environment.

  1. Connect the rotary encoder to the Arduino MKR WiFi 1010 as shown in the wiring diagram.
  2. Plug your Arduino MKR WiFi 1010 into your computer’s USB port.
  3. Open the Arduino IDE.
  4. Select the Arduino MKR WiFi 1010 board and correct COM port.
  5. Install the ezButton library from the Library Manager.
  6. Copy the code and paste it into a new sketch.
  7. Click Upload to compile and upload to the Arduino MKR WiFi 1010.
  8. Turn the knob right, then left.
  9. Press the knob.
  10. Check the Serial Monitor for output.
COM6
Send
Rotary Encoder:: direction: CLOCKWISE - count: 1 Rotary Encoder:: direction: CLOCKWISE - count: 2 Rotary Encoder:: direction: CLOCKWISE - count: 3 Rotary Encoder:: direction: CLOCKWISE - count: 4 Rotary Encoder:: direction: CLOCKWISE - count: 5 Rotary Encoder:: direction: ANTICLOCKWISE - count: 4 Rotary Encoder:: direction: ANTICLOCKWISE - count: 3 Rotary Encoder:: direction: ANTICLOCKWISE - count: 2 Rotary Encoder:: direction: ANTICLOCKWISE - count: 1 Rotary Encoder:: direction: ANTICLOCKWISE - count: 0 The button is pressed
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Arduino MKR WiFi 1010 Code – Rotary Encoder with Interrupt

For better performance, use interrupts to avoid missing steps. This code uses an interrupt on the Arduino MKR WiFi 1010.

/* * This Arduino MKR WiFi 1010 code was developed by newbiely.com * * This Arduino MKR WiFi 1010 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-rotary-encoder */ #include <ezButton.h> // the library to use for SW pin #define CLK_PIN 3 #define DT_PIN 4 #define SW_PIN 5 #define DIRECTION_CW 0 // clockwise direction #define DIRECTION_CCW 1 // counter-clockwise direction volatile int counter = 0; volatile int direction = DIRECTION_CW; volatile unsigned long last_time; // for debouncing int prev_counter; ezButton button(SW_PIN); // create ezButton object that attach to pin 5 void setup() { Serial.begin(9600); // configure encoder pins as inputs pinMode(CLK_PIN, INPUT); pinMode(DT_PIN, INPUT); button.setDebounceTime(50); // set debounce time to 50 milliseconds // use interrupt for CLK pin is enough // call ISR_encoderChange() when CLK pin changes from LOW to HIGH attachInterrupt(digitalPinToInterrupt(CLK_PIN), ISR_encoderChange, RISING); } void loop() { button.loop(); // MUST call the loop() function first if (prev_counter != counter) { Serial.print("DIRECTION: "); if (direction == DIRECTION_CW) Serial.print("Clockwise"); else Serial.print("Counter-clockwise"); Serial.print(" | COUNTER: "); Serial.println(counter); prev_counter = counter; } if (button.isPressed()) { Serial.println("The button is pressed"); } // TO DO: your other work here } void ISR_encoderChange() { if ((millis() - last_time) < 50) // debounce time is 50ms return; if (digitalRead(DT_PIN) == HIGH) { // the encoder is rotating in counter-clockwise direction => decrease the counter counter--; direction = DIRECTION_CCW; } else { // the encoder is rotating in clockwise direction => increase the counter counter++; direction = DIRECTION_CW; } last_time = millis(); }

※ NOTE THAT:

  • Some guides on other sites might tell you to use two interrupts for one encoder, but you only need one.
  • Always add the word volatile for global variables used in the interrupt. If you skip this, you might get strange errors.
  • Keep the code inside the interrupt as simple as possible. Do not use Serial.print() or Serial.println() in the interrupt.

Arduino MKR WiFi 1010 Rotary Encoder Application

Using a Rotary Encoder, you can use it for many different tasks, including these:

  • Arduino MKR WiFi 1010 – The rotary knob changes the servo motor's position
  • Arduino MKR WiFi 1010 – The rotary knob adjusts the LED brightness
  • Arduino MKR WiFi 1010 – The rotary knob controls the stepper motor's speed

Troubleshooting

  • No output in Serial Monitor: Confirm COM port and baud rate match the code.
  • Missed steps: Switch to the interrupt version of the code.
  • Button not working: Check wiring and pull-up resistor.
  • Erratic readings: Use shorter wires and ensure stable power.

Challenge Yourself - Creative Customizations

Once your Arduino MKR WiFi 1010 reads the rotary encoder reliably, try these ideas:

Quick Variations

  • Add min/max limits to the counter.
  • Change the step size (e.g., count by 2 instead of 1).

Advanced Features to Try

  1. Control an LED’s brightness with the rotary encoder.
  2. Use the encoder to navigate a menu on an LCD display.
  3. Combine with a servo motor for position control.
  4. Add multiple encoders for multi-axis control.

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!