Arduino Tutorials – Lesson 41 – Position Encoder sensor using Raspberry Pi Pico

To get the reading of position encoder sensor direction change using Raspberry Pi Pico

Required Components

  1. Position encoder sensor_1 no
  2. Raspberry Pi Pico_1 no
  3. Connecting Wires_4 set

Circuit

Steps

  1. Make sure the components are working properly.
  2. Connect the position encoder sensor CLK pin to the Raspberry Pi Pico GP8.
  3. Connect the position encoder sensor DT pin to the Raspberry Pi Pico GP9.
  4. Connect the +5v and ground(gnd) connections respectively.
  5. Check the Arduino program.
  6. Check the Electrical Circuit.
  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;
}