This example uses PlatformIO and the Teensy-4.x-Quad-Encoder-Library to implement a hardware quadrature encoder on pins 0 and 1. Remember that the teensy will be destroyed if you use 5V encoder signals. You can only use 3.3V signals!
#include <Arduino.h> #include <QuadEncoder.h> QuadEncoder encoder(1, 0, 1, 0); void setup() { Serial.begin(115200); encoder.setInitConfig(); encoder.init(); } void loop() { int32_t position = encoder.read(); // Compute position in mm and print it constexpr float mm_per_count = 0.0012; float mm = position * mm_per_count; Serial.printf("Position: %+3.4f (count %ld)\r\n", mm, position); // Print every 50ms delay(50); }
[env:teensy41] platform = teensy board = teensy41 framework = arduino monitor_speed = 115200 lib_deps = git+https://github.com/mjs513/Teensy-4.x-Quad-Encoder-Library