Skip to content
NoisyVolt
NoisyVolt

music electronics make code

  • Build
  • Code
  • Play
  • Watch
  • Listen
  • Sound
NoisyVolt

music electronics make code

Encoder Test

andy, May 7, 2026May 7, 2026

Basic code to test a rotary encoder connection on Teensy

Wiring

Test code:

// Rotary Encoder Test — Teensy 4.0
// 5-pin mechanical encoder (no module board)
// Wiring: A->Pin2, B->Pin3, C->GND, SW1->Pin4, SW2->GND

#include <Encoder.h>

Encoder myEnc(2, 3);

const int SW_PIN = 4;
long lastPosition = 0;
bool lastButtonState = HIGH;

void setup() {
  Serial.begin(115200);
  while (!Serial && millis() < 3000);

  // Enable internal pull-ups on all three signal pins
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(SW_PIN, INPUT_PULLUP);

  Serial.println("Rotary Encoder Test — Teensy 4.0");
  Serial.println("Rotate the knob and press the button.");
  Serial.println("-----------------------------------");
}

void loop() {
  // --- Read encoder position ---
  long newPosition = myEnc.read();

  if (newPosition != lastPosition) {
    int delta = newPosition - lastPosition;
    Serial.print("Position: ");
    Serial.print(newPosition / 4);
    Serial.print("  (raw: ");
    Serial.print(newPosition);
    Serial.print(")  Direction: ");
    Serial.println(delta > 0 ? "CW  ▶" : "CCW ◀");
    lastPosition = newPosition;
  }

  // --- Read button ---
  bool buttonState = digitalRead(SW_PIN);
  if (buttonState == LOW && lastButtonState == HIGH) {
    Serial.println(">>> Button pressed! Resetting position to 0.");
    myEnc.write(0);
    lastPosition = 0;
    delay(50);
  }
  lastButtonState = buttonState;
}

Build Code teensy

Post navigation

Previous post
Next post

Recent Posts

  • MIDI Loop Player
  • Sample Player on rPi
  • Display Test
  • Encoder Test
  • Audio Test

Archives

  • May 2026
  • April 2026
  • March 2026
Listen Watch Read Reference
©2026 NoisyVolt | WordPress Theme by SuperbThemes

Powered by
►
Necessary cookies enable essential site features like secure log-ins and consent preference adjustments. They do not store personal data.
None
►
Functional cookies support features like content sharing on social media, collecting feedback, and enabling third-party tools.
None
►
Analytical cookies track visitor interactions, providing insights on metrics like visitor count, bounce rate, and traffic sources.
None
►
Advertisement cookies deliver personalized ads based on your previous visits and analyze the effectiveness of ad campaigns.
None
►
Unclassified cookies are cookies that we are in the process of classifying, together with the providers of individual cookies.
None
Powered by