Creating position encoder sensor direction change program using Arduino MEGA2560
Required Components
- Position encoder sensor -1 no
- Arduino board Mega 2560 -1 no
- Connecting Wires -4 no
Circuit
Steps
- Make sure the components are working properly.
- Connect the position encoder sensor to the Arduino Mega 2560.
- Connect the Arduino pin 6&7 to Position encoder sensor CLK & DT pin .
- Connect the +5v and ground(gnd) connections respectively.
- Check the Arduino program.
- Check the Circuit Connections.
- 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
- Automation
- Aerospace
- Textiles