16 Gear 0-F Rotary Encoder Switch 8421 Horizontal Wiki - DFRobot


This is an 8421 absolute position encoder with 16-gear code(0-F). The mechanical position determines the uniqueness of each position of the encoder. Compared to a potentiometer, it doesn’t require memory, reference point and constant counting. Whenever you need the position, you can just easily read it. In this case, the anti-interference of the encoder and the reliability of the data are greatly improved. And because of the absolute uniqueness of each position, the anti-interference ability, and no power-off memory function required, this product is widely used in many kinds of industrial systems for device status controlling.




Board Overview

Num Label Description
1 8 8421 BCD code 8 port
2 4 8421 BCD code 4 port
3 2 8421 BCD code 2 port
4 1 8421 BCD code 1 port
5 C Common Port-Connect to VCC
6 X Common Port-Connect to GND

Truth Table

Truth Table



Connection Diagram

Connection Diagram

Sample Code

    @file       DFR0722_V10_Demo.ino
    @author     PowerLiao (DFRobot)
    @version    V1.0
    @date       2020-07-09
    @copyright  Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
    @licence    The MIT License (MIT)
    @breif      This example read and print the 8421 encoder code.

    This demo and related libraries are for [DFR0722]8421 Encoder-Horizontal(V1.0)
    Check out the links below for tutorials and connection diagrams
    Product(CH): http://www.dfrobot.com.cn/
    Product(EN): https://www.dfrobot.com/


#define setbit(data,b) (data|=(1<<b)) //set the b bit of data to 1
#define clrbit(data,b) (data&=~(1<<b)) //set the b bit of data to 0

uint8_t code8421 = 0;
const uint8_t code8Pin = 5;
const uint8_t code4Pin = 4;
const uint8_t code2Pin = 3;
const uint8_t code1Pin = 2;

void setup() {
  Serial.println("8421 Encoder Starts up!");
  pinMode(code8Pin, INPUT);
  pinMode(code4Pin, INPUT);
  pinMode(code2Pin, INPUT);
  pinMode(code1Pin, INPUT);

void loop() {
  //According to the pin level and set the corresponding bit to 0 or 1
  if (digitalRead(code8Pin) == HIGH){
    setbit(code8421, 3);
    clrbit(code8421, 3);

  if (digitalRead(code4Pin) == HIGH){
    setbit(code8421, 2);
    clrbit(code8421, 2);

  if (digitalRead(code2Pin) == HIGH){
    setbit(code8421, 1);
    clrbit(code8421, 1);

  if (digitalRead(code1Pin) == HIGH){
    setbit(code8421, 0);
    clrbit(code8421, 0);

  //Output the code in hexadecimal 
  Serial.print("Now code8421 is:  ");
  Serial.println(code8421, HEX);

Expected Results

The module print out the current code value every 1 second.



For any questions, advice or cool ideas to share, please visit the DFRobot Forum.

More Documents