Arduino Tutorials – Lesson 27 – Position Encoder sensor using Arduino MEGA2560

Creating position encoder sensor direction change program using Arduino MEGA2560

Required Components

  1. Position encoder sensor -1 no
  2. Arduino board Mega 2560 -1 no
  3. Connecting Wires -4 no

Circuit

Steps

  1. Make sure the components are working properly.
  2. Connect the position encoder sensor to the Arduino Mega 2560.
  3. Connect the Arduino pin 6&7 to Position encoder sensor CLK & DT pin .
  4. Connect the +5v and ground(gnd) connections respectively.
  5. Check the Arduino program.
  6. Check the Circuit Connections.
  7. Run the Arduino program.

Arduino Program

#define outputA 8
#define outputB 9
int counter = 0;
int aState;
int aLastState;

void setup() 
{
  pinMode (outputA,INPUT);
  pinMode (outputB,INPUT);
  Serial.begin (9600);
  aLastState = digitalRead(outputA);
}

void loop() 
{
  aState = digitalRead(outputA); // Reads the "current" state of the outputA
  if (aState != aLastState)
  {
    if (digitalRead(outputB) != aState) {
      counter ++;
    } 
    else {
      counter --;
    }
    Serial.print("Position: ");
    Serial.println(counter);
  }
  aLastState = aState;
}

Usage

  1. Automation
  2. Aerospace
  3. Textiles

Leave a Reply

Your email address will not be published. Required fields are marked *