Using the ANO Directional Navigation and Scroll Wheel Rotary Encoder with Arduino involves wiring it up, installing the necessary libraries, and running the example code. This example also uses a NeoPixel ring. Therefore, you'll need to install the RotaryEncoder and Adafruit NeoPixel libraries.
Wiring
First, solder the rotary encode to the breakout, and wires to the NeoPixel ring. Then, follow the example below to connect both to a Feather M4.
- NeoPixel ring data in to Feather A0
- NeoPixel ring ground to Feather GND
- NeoPixel ring power to Feather 3.3v
- Breakout ENCA to Feather D13
- Breakout ENCB to Feather D12
- Breakout COMA to Feather D11
- Breakout SW1 to Feather D10
- Breakout SW2 to Feather D9
- Breakout SW3 to Feather D6
- Breakout SW4 to Feather D5
- Breakout SW5 to Feather SCL
- Breakout COMB to Feather SDA
The wiring shown here ties COMA/B to digital outputs set LOW in code (instead of connected to GND) to allow for the direct-to-Feather connection.
Library Installation
You can install the RotaryEncoder and NeoPixel libraries for Arduino using the Library Manager in the Arduino IDE.
Click the Manage Libraries ... menu item, search for RotaryEncoder, and select the RotaryEncoder library (by Matthias Hertel):
Then search for NeoPixel and select the Adafruit NeoPixel library:
Load Example
Click Download Project Bundle to download the example to your computer. Open the ANO_Rotary_Encoder_NeoPixel_Arduino_Example.ino file in the Arduino IDE.
// SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries // // SPDX-License-Identifier: MIT #include <Adafruit_NeoPixel.h> #include <RotaryEncoder.h> #define PIN_ENCODER_A 13 #define PIN_ENCODER_B 12 #define COM_A 11 #define COM_B SDA #define BUTTON_UP 5 #define BUTTON_LEFT SCL #define BUTTON_DOWN 9 #define BUTTON_RIGHT 6 #define BUTTON_IN 10 RotaryEncoder encoder(PIN_ENCODER_A, PIN_ENCODER_B, RotaryEncoder::LatchMode::TWO03); // This interrupt will do our encoder reading/checking! void checkPosition() { encoder.tick(); // just call tick() to check the state. } int last_rotary = 0; #define NUMPIXELS 12 Adafruit_NeoPixel pixels(NUMPIXELS, A0, NEO_GRB + NEO_KHZ800); void setup(void) { Serial.begin(115200); while (!Serial); Serial.println("ANO Rotary Encoder Demo"); pinMode(COM_A, OUTPUT); digitalWrite(COM_A, LOW); pinMode(COM_B, OUTPUT); digitalWrite(COM_B, LOW); attachInterrupt(digitalPinToInterrupt(PIN_ENCODER_A), checkPosition, CHANGE); attachInterrupt(digitalPinToInterrupt(PIN_ENCODER_B), checkPosition, CHANGE); pinMode(BUTTON_UP, INPUT_PULLUP); pinMode(BUTTON_DOWN, INPUT_PULLUP); pinMode(BUTTON_LEFT, INPUT_PULLUP); pinMode(BUTTON_RIGHT, INPUT_PULLUP); pinMode(BUTTON_IN, INPUT_PULLUP); pixels.begin(); pixels.setBrightness(30); pixels.show(); } void loop(void) { // read encoder int curr_rotary = encoder.getPosition(); RotaryEncoder::Direction direction = encoder.getDirection(); pixels.clear(); if (curr_rotary != last_rotary) { Serial.print("Encoder value: "); Serial.print(curr_rotary); Serial.print(" direction: "); Serial.println((int)direction); } last_rotary = curr_rotary; pixels.setPixelColor((curr_rotary + (1000*NUMPIXELS)) % NUMPIXELS, pixels.Color(0, 150, 0)); if (! digitalRead(BUTTON_UP)) { pixels.setPixelColor(0, pixels.Color(150, 0, 0)); } if (! digitalRead(BUTTON_LEFT)) { pixels.setPixelColor(NUMPIXELS/4, pixels.Color(150, 0, 0)); } if (! digitalRead(BUTTON_DOWN)) { pixels.setPixelColor(NUMPIXELS/2, pixels.Color(150, 0, 0)); } if (! digitalRead(BUTTON_RIGHT)) { pixels.setPixelColor(NUMPIXELS*3/4, pixels.Color(150, 0, 0)); } if (! digitalRead(BUTTON_IN)) { pixels.fill(pixels.Color(50, 50, 50)); } pixels.show(); delay(20); }
After opening the demo file, upload to your Arduino wired up to the encoder. Once you upload the code, you will see the rotary encoder values when you rotate it being printed when you open the Serial Monitor (Tools->Serial Monitor) at 115200 baud, similar to this:
Rotate the encoder to see the green NeoPixel move around the NeoPixel ring. Press the outer directional buttons to see a red LED light up in a corresponding location on the ring. Finally, click the center button to see the entire ring light up white.
Text editor powered by tinymce.