How to use pin 4 and 5 for rotary encoder in Arduino Ono

Hi friends,
I want to use other pins except pins 2 and 3 on my Arduino for connecting an encoder. But when I rotate the encoder, it doesn't work properly and shows strange numbers on the serial monitor. One of my friends suggested the following code, but it didn't work either. I appreciate it if you could guide me on what to do.

Best regards
Neda

#include <Encoder.h>

#define ENCODER_A 4
#define ENCODER_B 5

#define MAX_SPEED 255

volatile long count = 0;

Encoder myEncoder1(ENCODER_A, ENCODER_B);

void encoderISR() {
  if (digitalRead(ENCODER_A) == digitalRead(ENCODER_B)) {
    count++;
  } else {
    count--;
  }
}

void setup() {
  Serial.begin(9600);

  attachInterrupt(digitalPinToInterrupt(ENCODER_A), encoderISR, CHANGE);
  attachInterrupt(digitalPinToInterrupt(ENCODER_B), encoderISR, CHANGE);
}

void loop() {
  long position1 = count; 
  Serial.println(position1); 

 
}

Welcome to the forum

The Uno hardware interrupts are on pin 2 and 3. You cannot use other pins of your choice for hardware interrupts

Why not use pins 2 and 3 ?

With other pins you can use PCINT. Look at other rotary encoder topics in the forum for details.

Blockquote

because I want to connect 2 encoders to Arduino one of them will use Pin 2 and 3 and what should I do for the other one?

using a timer-interrupt for polling the encoders

or using an IO-expander

best regards Stefan

I wouldn't listen to that friend any more on the topic of coding.

Besides @StefanL38 's suggestions, you have at least two other options:

  • Pick a different processor board than the Ono (sic). That's what I'd do.

  • For the encoder not connected to Pins 2/3 use the polling option provided by the Encoder library.

in this link he used Expander and I don't want to use Expander

and

and in this link I couldn't understand the codes I'm a novice in Arduino would you please help me?

Best regards
Neda

If you ask specific questions about what you do not understand I will explain.
Nothing else.
Post specific questions

There are a number options in the IDE library manager to make pin change interrupts easier to deal with.

For example - GitHub - NicoHood/PinChangeInterrupt: A simple & compact PinChangeInterrupt library for Arduino

Somehow easier but then the logic how to evaluate rotary-econders is still missing

please refer below link (New topic)

How I can use second encoder in my code? - Using Arduino / Programming Questions - Arduino Forum

would you give me some example please?

https://www.pjrc.com/teensy/td_libs_Encoder.html
https://github.com/ghawkgu/NewEncoder-DualMode

Hello every one
I used an encoder, a motor driver, and a motor in the following code, which starts moving the motor by rotating the encoder.

Now I want to use another encoder as well. How can I define the second encoder in this code, considering that I cannot use pins 2 and 3 because encoder number one is connected to them?

I would appreciate it if you could guide me.


#include <Encoder.h>
#define RPWM 5
#define LPWM 6
#define REN 8
#define LEN 9

#define MAX_SPEED 255 

Encoder myEncoder(2, 3); 

int out1;

void setup() {
  Serial.begin(9600);
  pinMode(RPWM, OUTPUT);
  pinMode(LPWM, OUTPUT);
  pinMode(LEN, OUTPUT);
  pinMode(REN, OUTPUT);
  digitalWrite(REN, HIGH);
  digitalWrite(LEN, HIGH);
}

void loop() {
  long position = myEncoder.read(); 
  Serial.println(position);
  
  int speed = min(MAX_SPEED, abs(position)); 
  if (position > 0) { 
    analogWrite(RPWM, MAX_SPEED);
    analogWrite(LPWM, 0);
  } else if (position < 0) { 
    analogWrite(LPWM, MAX_SPEED);
    analogWrite(RPWM, 0);
  } else { 
    analogWrite(LPWM, 0);
    analogWrite(RPWM, 0);
  }
    }
  

The Encoder library will use 2, 1 or no interrupts. So you could use pin 2 and 4 (for instance) for 1 encoder and 3 and 7 (for instance) for the second encoder. That may be fast enough and still allow 2 encoders. If you don't need a lot of speed from the second encoder (human interface), use 2 non-interrupt pins and save the interrupts for the encoder that requires speed.

From the encoder library docs:

Encoders have 2 signals, which must be connected to 2 pins. There are three options.

  1. Best Performance: Both signals connect to interrupt pins.
  2. Good Performance: First signal connects to an interrupt pin, second to a non-interrupt pin.
  3. Low Performance: Both signals connect to non-interrupt pins

Here is an example-code that uses the

library

// define the input pins
#define pinA 19
#define pinB 20
#define pinP 21

// create an encoder object initialized with their pins
Encoder encoder( pinA, pinB, pinP );

// setup code
void setup() 
{
  // start the encoder time interrupts
  EncoderInterrupt.begin( &encoder );
}

// loop code
void loop() {
  // read the debounced value of the encoder button
  bool pb = encoder.button();

  // get the encoder variation since our last check, it can be positive or negative, or zero if the encoder didn't move
  // only call this once per loop cicle, or at any time you want to know any incremental change
  int delta = encoder.delta();

  // add the delta value to the variable you are controlling
  myEncoderControlledVariable += delta;
}

whatever questions you have about this demo-code just ask them.
Just make sure that your questions are specific to the code

best regards Stefan

Thank you so much! I've been reading a lot of articles and testing various codes for the past two days, but I couldn't find a solution at all. You have been a great help to me.

Another question I had is, what should I do if I want to use more than two encoders? I would appreciate it if you could also guide me on this matter.

The explanations on the main-page of this library

says

More

  • A template code for several encoders is available as a comment in the Encoder.h header file.

I have no direct experience having used only 2 at once. If they are slow speed and can be used with regular digital you can have as many encoders as pins and time to read them.

If you need more interrupts, for fast encoders, see if there are encoder libraries that use pin-change interrupts or get a board with more hardware interrupts (Mega has 6).

thanks a bunch :blush: