Libraries \ Encoder

Reference for Wiring version 1.0 Build 0100+ If you have a previous version, use the reference included with your software. If see any errors or have any comments, let us know.





#include <Encoder.h> 
int val; 
Encoder myEncoder; 
void setup() { 
  // ataches the encoder to pins EI2 and pin 8
  // EI2 is pin 18 on Wiring S
  // EI2 is pin 2 on Wiring v1
  myEncoder.attach(EI2, 8); 
  myEncoder.write(0); // set the encoder position to 0
void loop() { 
  // Reads the position or angle of the encoder variable 
  val =;          // read position
  Serial.print("Encoder is at: "); // print the position
  delay(100); // wait 100ms for next reading
Description The Wiring encoder library is based on external interruptions which can only happen on these specific pins. The phaseB cable can be connected to any other digital I/O pin of the Wiring I/O board. The attach(phaseAPin, phaseBPin) method attaches an Encoder variable to the pins where the encoder is connected.
encoder.attach(pinA, pinB)
pinA Encoder phase A pin. On Wiring v1 boards the external interrupts capable pins are: 0, 1, 2, 3, 36, 37, 38 and 39 named EI0, EI1, .. EI7 respectively. On Wiring S board the external interrupts capable pins are: 2, 3 and 18 named EI0, EI1 and EI2 respectively.
pinB Encoder phase B pin, it can be any Wiring digital pin
encoder The Encoder variable.
Returns None
Usage Application
Updated on July 07, 2011 11:09:54pm PDT

Creative Commons License