This encoder is matched with 2wd miniQ Robot chassis. AB two-phase pulse wave,measured by two infrared reflective sensor.These two sensor pulse waveform is close to a phase difference of 90 degrees, through the AB two-phase lead and lag of the waveform judged wheel forward rotation or the reverse. You can install it on the motor bracket or mini robot chassis to do the PID and position control.

Note: Please short current R4 when working in 3.3V


Connection Diagram

Mini robot encoder diagram


Sample Code

const byte encoder0pinA = 2;//A pin -> the interrupt pin 0
const byte encoder0pinB = 4;//B pin -> the digital pin 4
byte encoder0PinALast;
int duration;//the number of the pulses
boolean Direction;//the rotation direction

void setup()
  Serial.begin(57600);//Initialize the serial port
  EncoderInit();//Initialize the module

void loop()
  duration = 0;

void EncoderInit()
  Direction = true;//default -> Forward
  attachInterrupt(0, wheelSpeed, CHANGE);

void wheelSpeed()
  int Lstate = digitalRead(encoder0pinA);
  if((encoder0PinALast == LOW) && Lstate==HIGH)
    int val = digitalRead(encoder0pinB);
    if(val == LOW && Direction)
      Direction = false; //Reverse
    else if(val == HIGH && !Direction)
      Direction = true;  //Forward
  encoder0PinALast = Lstate;

  if(!Direction)  duration  ;
  else  duration--;


Q&A Some general Arduino Problems/FAQ/Tips
Q I have a question about the encoder. In the process of adjusting the screws, I have turned a screw out of range and don't know which way to turn it back. Is there one direction i should turn it to 'start from zero'?
A You can turn it in clockwise or reversely to adjust the resistance value, range 0-30KΩ. And the valid adjustable angle is 260°±20°. To make it easier to understand, it means if you rotate it in 360°, it will keep the same resistance value.
A For any questions, advice or cool ideas to share, please visit the DFRobot Forum.

More Documents

DFshopping_car1.png Get it from MiniQ Robot chassis Encoder or DFRobot Distributor.

Turn to the Top