Introduction
This is an 8421 absolute position encoder(Vertical) 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.
Features
- Long Service Life
 - 8421 Code
 - Breakout, easy to install
 
Application
- Industrial Automation
 - Automotive Electronics
 - Multimedia Speaker
 - Instruments and Apparatuses
 - Smart Home and Other Fields
 
Specification
- Current: 150mA, 42V
 - 8421 Encode: 16-gear(0~F)
 - Service Life: 10000 steps
 - Operating Temperature: -40~85℃
 - Module Size: 19x19mm/0.75x0.75”
 - Mounting Aperture: 2.0mm
 - Mounting Hole Pitch: 15x15mm
 
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
Tutorial
Requirements
- 
Hardware
- DFRduino UNO R3 (or similar) x 1
 - 8421_Encoder-Vertical x1
 - Wires
 
 - 
Software
 
Connection Diagram
Sample Code
- Upload the following codes to your mainboard, open the serial monitor, then you will find the output code value.
 
/**************************************************************************/
/*!
 * @file DFR0721_V10_Demo.ino
 * @brief This example read and print the 8421 encoder code.
 * @copyright  Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [PowerLiao]([email protected])
 * @version  V1.0
 * @date  2020-07-09
 * @get from 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.begin(115200);
  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);
  }else{
    clrbit(code8421, 3);
  }
  if (digitalRead(code4Pin) == HIGH){
    setbit(code8421, 2);
  }else{
    clrbit(code8421, 2);
  }
  if (digitalRead(code2Pin) == HIGH){
    setbit(code8421, 1);
  }else{
    clrbit(code8421, 1);
  }
  if (digitalRead(code1Pin) == HIGH){
    setbit(code8421, 0);
  }else{
    clrbit(code8421, 0);
  }
  //Output the code in hexadecimal 
  Serial.print("Now code8421 is:  ");
  Serial.println(code8421, HEX);
  delay(1000);
}
Expected Results
The module print out the current code value every 1 second.
FAQ
For any questions, advice or cool ideas to share, please visit the DFRobot Forum.
