I have a rotary encoder connected to my Nano v3.0 and I have written a simple sketch to read the input of the encoder. My goal is to have the 'count' increase/decrease when the encoder is turned clockwise/counterclockwise.
In its current form, I can only get the 'count' to move in one direction, irrespective of the direction I turn the encoder shaft.
I have attached a schematic of the circuit setup, which includes capacitors on the encoder outputs in an attempt to prevent switch debounce.
The sketch is as follows:
//pin 2 of nano has to be used for interrupt
int encoderPinA = 2;
int encoderPinB = 4;
// volatile variables for use in ISR
volatile int count = 100;
volatile int newCount = 100;
void setup() {
pinMode(encoderPinA, INPUT);
pinMode(encoderPinB, INPUT);
//enable pull-up resistors on inputs to prevent fluctuations
digitalWrite(encoderPinA, HIGH);
digitalWrite(encoderPinB, HIGH);
attachInterrupt(digitalPinToInterrupt(encoderPinA), turn_ISR, FALLING);
Serial.begin(9600);
}
void loop () {
IF (newCount != count) {
Serial.print("Count: ");
Serial.println(newCount);
count = newCount;
}
void turn_ISR() {
IF (encoderPinB == LOW) { //cw turn
newCount = count + 1;
} ELSE { //ccw turn
newCount = count - 1;
}
}
What should i do then?? I took the following from the arduino official documentation " Good tasks for using an interrupt may include reading a rotary encoder, or monitoring user input"
Some hints as to the way to resolve would have been useful
olliejspurway:
What should i do then?? I took the following from the arduino official documentation " Good tasks for using an interrupt may include reading a rotary encoder, or monitoring user input"
There's the problem for a start - reading the "Arduino official documentation"!
//pin 2 of nano has to be used for interrupt
int encoderPinA = 2;
int encoderPinB = 4;
// volatile variables for use in ISR
volatile int count = 100;
volatile int newCount = 100;
void setup() {
pinMode(encoderPinA, INPUT);
pinMode(encoderPinB, INPUT);
//enable pull-up resistors on inputs to prevent fluctuations
digitalWrite(encoderPinA, HIGH);
digitalWrite(encoderPinB, HIGH);
attachInterrupt(digitalPinToInterrupt(encoderPinA), turn_ISR, FALLING);
Serial.begin(9600);
}
void loop () {
IF (newCount != count) {
Serial.print("Count: ");
Serial.println(newCount);
count = newCount;
}
void turn_ISR() {
IF (digitalRead(encoderPinB) == LOW) { //cw turn
newCount = count + 1;
} ELSE { //ccw turn
newCount = count - 1;
}
}
Hi,
Have you actually measured the 5v and 0v levels at the output, what is the spec of the encoder.
Some encoders need pull-up resistors on their outputs to give proper output levels.
I see no 5v supply to the rotary encoder.
//enable pull-up resistors on inputs to prevent fluctuations
digitalWrite(encoderPinA, HIGH);
digitalWrite(encoderPinB, HIGH);
That's OK for the output devices of the encoder, what about the rest of the internal circuitry?
As I have said before, I prefer to see pull-ups externally.
What value of pull up does the encoder spec recommend.