To get the reading of position encoder sensor direction change using Raspberry Pi Pico
Required Components
- Position encoder sensor_1 no
- Raspberry Pi Pico_1 no
- Connecting Wires_4 set
Circuit
Steps
- Make sure the components are working properly.
- Connect the position encoder sensor CLK pin to the Raspberry Pi Pico GP8.
- Connect the position encoder sensor DT pin to the Raspberry Pi Pico GP9.
- Connect the +5v and ground(gnd) connections respectively.
- Check the Arduino program.
- Check the Electrical Circuit.
- 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;
}