# I Have a problem with arduino and rotary encoder

**URL:** <https://forum.arduino.cc/t/i-have-a-problem-with-arduino-and-rotary-encoder/1032125>\
**Category:** Motors, Mechanics, Power and CNC\
**Created:** [September 15, 2022, 3:22pm UTC](https://forum.arduino.cc/t/i-have-a-problem-with-arduino-and-rotary-encoder/1032125 "2022-09-15T15:22:54Z")\
**Posts on this page:** 17\
**Page:** 2

<div class="post-metadata">

**Author:** ![axvu](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@axvu](https://forum.arduino.cc/u/axvu)\
**Post date:** [September 15, 2022, 5:59pm UTC](https://forum.arduino.cc/t/i-have-a-problem-with-arduino-and-rotary-encoder/1032125/22 "2022-09-15T17:59:38Z")

</div>

18 n 19 are RX1 and TX1

---

<div class="post-metadata">

**Author:** ![PatMax](https://avatars.discourse-cdn.com/v4/letter/p/9fc29f/32.png) [@PatMax](https://forum.arduino.cc/u/PatMax)\
**Post date:** [September 15, 2022, 6:00pm UTC](https://forum.arduino.cc/t/i-have-a-problem-with-arduino-and-rotary-encoder/1032125/23 "2022-09-15T18:00:21Z")

</div>

Yes, and also Ext Int 2 and Ext Int 3

---

<div class="post-metadata">

**Author:** ![PatMax](https://avatars.discourse-cdn.com/v4/letter/p/9fc29f/32.png) [@PatMax](https://forum.arduino.cc/u/PatMax)\
**Post date:** [September 15, 2022, 6:04pm UTC](https://forum.arduino.cc/t/i-have-a-problem-with-arduino-and-rotary-encoder/1032125/24 "2022-09-15T18:04:00Z")

</div>

Hi madmark2150,  
yes, it is an up/down counting. I don't know what axvu want to do, but ha can begin with this.  
Sure my direction logic is good ! 😉 you cannot find a better one, except if you use a teensy and its electronic decoding quadrature signal

---

<div class="post-metadata">

**Author:** ![PatMax](https://avatars.discourse-cdn.com/v4/letter/p/9fc29f/32.png) [@PatMax](https://forum.arduino.cc/u/PatMax)\
**Post date:** [September 15, 2022, 8:13pm UTC](https://forum.arduino.cc/t/i-have-a-problem-with-arduino-and-rotary-encoder/1032125/25 "2022-09-15T20:13:56Z")

</div>

Mega's documentation was not clear to me. I correct what I said: you have to connect BLK and WH on pins 2 and 3 of the Mega (to be compatible with the code I gave above, and the interrupt vectors 2 and 3). Can a Mega user confirm? )  
A resistor of 1K max on each of the BLK and WH lines is a security for the encoder, in case of bad connection.  
(BLK wire....1K resistor....InputPin)

You must see 0V on one pin, and 5V on other pin.

---

<div class="post-metadata">

**Author:** ![johnerrington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnerrington/32/340106_2.png) [@johnerrington](https://forum.arduino.cc/u/johnerrington)\
**Post date:** [September 16, 2022, 6:23am UTC](https://forum.arduino.cc/t/i-have-a-problem-with-arduino-and-rotary-encoder/1032125/26 "2022-09-16T06:23:38Z")

</div>

The model you quote has pnp output - like this

(thanks for the link @axvu )

![image](https://europe1.discourse-cdn.com/arduino/original/4X/e/3/6/e366e7e05690c388a481afd8c0bad72d82dd8e34.png)

so the A, B, Z connections are o/c or connected to your 24V supply, and can provide 35mA. That would damage our arduino if connected directly.

as @PatMax says, you need to look at which of A & B rises first to know whethre to increment or decrement your counter.

![image](https://europe1.discourse-cdn.com/arduino/original/4X/9/f/9/9f92410b9f416c776db3098516901c9e9ede5f2e.png)

I'd suggest you connect a LED and series resistor (say 4k7) between ground and each of the A & B wires to check the encoder is performing correctly.

---

<div class="post-metadata">

**Author:** ![PatMax](https://avatars.discourse-cdn.com/v4/letter/p/9fc29f/32.png) [@PatMax](https://forum.arduino.cc/u/PatMax)\
**Post date:** [September 16, 2022, 8:57am UTC](https://forum.arduino.cc/t/i-have-a-problem-with-arduino-and-rotary-encoder/1032125/27 "2022-09-16T08:57:02Z")

</div>

Aaaaaah...I'm sorry, I didn't see that the encoder output transistors are PNP ! Thanks johnerrington...

First you have to 1) test the good working of your encoder, and 2) know if the Mega didn't suffer from the direct connection on the 24V...

1. connect to the 2 encoder BLK and WH outputs a Led and a 4k7 resistance as indicated by johnerrington, and check that the 2 leds turn on and off in opposition when you turn the encoder axis

2. make sure that the Mega can be programmed, and add a message in the setup to see the serail-monitor react to the program launch (to see if the Mega is working)

3. if it still doesn't work, try 2 other interrupt inputs (and modify the code in accordance), maybe D2 and D3 suffered ?

The voltage reduction at the Mega input:  
With a 24V (+- 15%) encoder supply you need to put 2 divider bridges as indicated above by zhomeslice :

- Vin on the Black wire of the encoder (BLK)

- Z1 = 4K7

- Z2 = 1K

- Vout on the input pin of the Mega  
(a diode in parallel on Z2 if possible)

- The same for the White wire WH

(you can leave the 2 test LEDs and put the divider bridge in parallel, you will have an additional animation when activating the encoder)

---

<div class="post-metadata">

**Author:** ![PatMax](https://avatars.discourse-cdn.com/v4/letter/p/9fc29f/32.png) [@PatMax](https://forum.arduino.cc/u/PatMax)\
**Post date:** [September 16, 2022, 10:10am UTC](https://forum.arduino.cc/t/i-have-a-problem-with-arduino-and-rotary-encoder/1032125/28 "2022-09-16T10:10:09Z")

</div>

The management of 8-bit ports between Uno and Mega is more complicated than I thought; I have to review my program...

---

<div class="post-metadata">

**Author:** ![PatMax](https://avatars.discourse-cdn.com/v4/letter/p/9fc29f/32.png) [@PatMax](https://forum.arduino.cc/u/PatMax)\
**Post date:** [September 16, 2022, 10:27am UTC](https://forum.arduino.cc/t/i-have-a-problem-with-arduino-and-rotary-encoder/1032125/29 "2022-09-16T10:27:57Z")

</div>

hope this is the good one ?

```cpp
// Arduino MEGA *optical* encoder A & B on pin 2 & 3 

  #define INPUT_ENCOD_A 2 // ***MEGA PIN2 (***=adapt with input pin)
  #define INPUT_ENCOD_B 3 // *** MEGA PIN3
  #define SignalA B00010000 // *** MEGA PIN2 = PORT_E bit4 = B00010000
  #define SignalB B00100000 // *** MEGA PIN3 = PORT_E bit5 = B00100000
  #define SignalAB B00110000 // *** both signals
  volatile int ISRencodPos; // encoder position
  int encodLastPos; // previous position
  byte LastPort8 = SignalA; // previous A/B state 
  

void setup(void) {
  pinMode(INPUT_ENCOD_A, INPUT_PULLUP); //
  pinMode(INPUT_ENCOD_B, INPUT_PULLUP); //
  attachInterrupt(digitalPinToInterrupt(INPUT_ENCOD_A), ExtInt, CHANGE);
  attachInterrupt(digitalPinToInterrupt(INPUT_ENCOD_B), ExtInt, CHANGE);
  Serial.begin(115200); // fast fast fast !
  Serial.println(F("axvu's encoder is working well!")); // 
}

void ExtInt() { /// OPTICAL ENCODER ext interrupt pin X, Y
  byte Port8 = PINE & SignalAB; // *** PIND or PINC or PINE...
  if ((Port8 ^ LastPort8) & SignalA) ISRencodPos++; // Rotation -> {ISRencodPos++; Sense = 1;}
  if ((Port8 ^ LastPort8) & SignalB) ISRencodPos--; // Rotation <- {ISRencodPos--; Sense = 0;}
  if ((Port8) && (Port8 != SignalAB)) Port8 ^= SignalAB; // (swap A-B)
   LastPort8 = Port8; // mieux vaut faire court
}

void loop(void) { /// MAIN LOOP
  noInterrupts(); //
    int encodPosition = ISRencodPos; //
  interrupts(); //
                                                              //
  if (encodLastPos != encodPosition) { // when the encoder change,
    encodLastPos = encodPosition; //
    Serial.println(encodPosition); // print encoder position
  } //
} //

```

---

<div class="post-metadata">

**Author:** ![axvu](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@axvu](https://forum.arduino.cc/u/axvu)\
**Post date:** [September 16, 2022, 6:26pm UTC](https://forum.arduino.cc/t/i-have-a-problem-with-arduino-and-rotary-encoder/1032125/30 "2022-09-16T18:26:47Z")

</div>

Hello, how are you, yesterday I couldn't comment anymore, it turns out that my account is new and I have a response limit.

I'm glad to read the possible mistakes I'm making by not making the voltage divider, unfortunately I won't work until Monday, only on that day will I be able to test everything you're telling me, I hope this really works, I'll keep you up to date!

---

<div class="post-metadata">

**Author:** ![PatMax](https://avatars.discourse-cdn.com/v4/letter/p/9fc29f/32.png) [@PatMax](https://forum.arduino.cc/u/PatMax)\
**Post date:** [September 16, 2022, 6:33pm UTC](https://forum.arduino.cc/t/i-have-a-problem-with-arduino-and-rotary-encoder/1032125/31 "2022-09-16T18:33:43Z")

</div>

hello, we are fine, have good days until then !

---

<div class="post-metadata">

**Author:** ![PatMax](https://avatars.discourse-cdn.com/v4/letter/p/9fc29f/32.png) [@PatMax](https://forum.arduino.cc/u/PatMax)\
**Post date:** [September 18, 2022, 5:38pm UTC](https://forum.arduino.cc/t/i-have-a-problem-with-arduino-and-rotary-encoder/1032125/32 "2022-09-18T17:38:10Z")

</div>

My first Wokwi project! I didn't have to read the doc, except for the resistor rotation; it's easy to create! I don't know if the link will stay the same, if I have to make program changes.

axvu, click on the link, and then on the 'start' button at the top of the window: here is a Mega and an encoder simulation!

> **[rotary\_encoder\_simulator.ino - Wokwi Arduino and ESP32 Simulator](https://wokwi.com/projects/343001355413094995)**
>
> Run IoT and embedded projects in your browser: ESP32, Arduino, Pi Pico, and more. No installation required!

---

<div class="post-metadata">

**Author:** ![axvu](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@axvu](https://forum.arduino.cc/u/axvu)\
**Post date:** [September 19, 2022, 2:14pm UTC](https://forum.arduino.cc/t/i-have-a-problem-with-arduino-and-rotary-encoder/1032125/33 "2022-09-19T14:14:12Z")

</div>

Hey hello again, Im reviewing the program and simulation, it seems to work fine. My encoder is 24v, do I still need to make the voltage reduction? or do I only put the 1k resistor?

---

<div class="post-metadata">

**Author:** ![axvu](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@axvu](https://forum.arduino.cc/u/axvu)\
**Post date:** [September 19, 2022, 2:34pm UTC](https://forum.arduino.cc/t/i-have-a-problem-with-arduino-and-rotary-encoder/1032125/34 "2022-09-19T14:34:30Z")

</div>

I have placed a 1k resistor to each encoder output (WH n' BLK) and a LED on each one, both LED's light up when I spin left or right, is that normal?

---

<div class="post-metadata">

**Author:** ![PatMax](https://avatars.discourse-cdn.com/v4/letter/p/9fc29f/32.png) [@PatMax](https://forum.arduino.cc/u/PatMax)\
**Post date:** [September 19, 2022, 3:00pm UTC](https://forum.arduino.cc/t/i-have-a-problem-with-arduino-and-rotary-encoder/1032125/35 "2022-09-19T15:00:01Z")

</div>

Hi axvu !  
you still need to make the voltage reduction, it is absolutely necessary when connected to the mega (input 2 and 3) !  
but you don't need the 1K resistor (as on the diagram in wokwi) in this case.  
I repeat : you need a 4K7 and an 1K as described above by zhomeslice  
BLK.....4K7.....1K.....0V(ground or gnd)  
between 4K7 and 1K, put a wire to pin 3  
be sure the 1K is connected to the GND (0V)  
ditto for WH

It is normal that both leds seems to light up together when you spin left or right, but without rotation, there must always be 1 led on and the other off.  
1 K is a bit weak, put 4K7 on BLK instead  
BLK...4K7...led...0V (or BLK...led...4K7...0V)  
ditto for WH

---

<div class="post-metadata">

**Author:** ![axvu](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@axvu](https://forum.arduino.cc/u/axvu)\
**Post date:** [September 19, 2022, 3:41pm UTC](https://forum.arduino.cc/t/i-have-a-problem-with-arduino-and-rotary-encoder/1032125/36 "2022-09-19T15:41:58Z")

</div>

I can´t get it to work with your cod. I'm attaching a link that I created for you: [- YouTube](https://youtu.be/3AiL5Q5FfOk)  
I have managed to make it work with another code, apparently, the readings are similar even if the encoder axis spin fast or slow:

```cpp
volatile unsigned int temp, counter = 0; //This variable will increase or decrease depending on the rotation of encoder

int Pos = 0;

const float pi = 3.14;
const float R = 0.752;
const float N = 360;
float distance = 0;
void setup() {
  Serial.begin (9600);

  pinMode(2, INPUT_PULLUP); // internal pullup input pin 2 
  
  pinMode(3, INPUT_PULLUP); // internalเป็น pullup input pin 3
//Setting up interrupt
  //A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
  attachInterrupt(0, ai0, RISING);
   
  //B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on moust Arduino.
  attachInterrupt(1, ai1, RISING);
  }
   
  void loop() {
  // Send the value of counter
  if( counter != temp ){
  Serial.println (counter);
  //Serial.print (distance);
  //Serial.println ("cm");
  distance = ((2*pi*R)/N) * counter;
  temp = counter;
  }
  }
   
  void ai0() {
  // ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
  // Check pin 3 to determine the direction
  if(digitalRead(3)==LOW) {
  counter++;
  }else{
  counter--;
  }
  }
   
  void ai1() {
  // ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
  // Check with pin 2 to determine the direction
  if(digitalRead(2)==LOW) {
  counter--;
  }else{
  counter++;
  }
  }

```

---

<div class="post-metadata">

**Author:** ![PatMax](https://avatars.discourse-cdn.com/v4/letter/p/9fc29f/32.png) [@PatMax](https://forum.arduino.cc/u/PatMax)\
**Post date:** [September 19, 2022, 3:50pm UTC](https://forum.arduino.cc/t/i-have-a-problem-with-arduino-and-rotary-encoder/1032125/37 "2022-09-19T15:50:28Z")

</div>

The link works, but a message tells me that the video is private and I can't see it...

you managed to make the encoder work on your mega, with this last program? with the 2 reduction resistances? great!  
you can turn the axis quickly, without losing steps?

I don't understand why my program doesn't work on your Mega; if it works on the Wokwi simulator, it should work everywhere!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 18, 2023, 3:51pm UTC](https://forum.arduino.cc/t/i-have-a-problem-with-arduino-and-rotary-encoder/1032125/38 "2023-03-18T15:51:08Z")

</div>

Closed on March 18, 2023, 3:51pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.

[Previous page](https://forum.arduino.cc/t/i-have-a-problem-with-arduino-and-rotary-encoder/1032125.md?page=1)
