Arduino Uno stops transmitting once rotary encoder is plugged in

I have a 5-24V rotary encoder I’m trying to get working for a sim racing wheel. Whenever I only have the 2 phase hooked up to D2 & D3 pins the arduino “works” (obv it’s not getting any actually reading but it will send stuff through serial). However if I plug the rotary encoder in to 5v and grd then it just stops transmitting and I don’t understand why. I don’t have anything else connected to the board at all.

To be sure, please post a wiring diagram, and code unless You have crystall balls to send us.

Here is the code

/*     Arduino Rotary Encoder Tutorial
 *      
 *  by Dejan Nedelkovski, www.HowToMechatronics.com
 *  
 */
 
 #define outputA 2
 #define outputB 3
 int counter = 0; 
 int aState;
 int aLastState;  
 void setup() { 
   pinMode (outputA,INPUT);
   pinMode (outputB,INPUT);
   
   Serial.begin (9600);
   // Reads the initial state of the outputA
   aLastState = digitalRead(outputA);   
 } 
 void loop() { 
   aState = digitalRead(outputA); // Reads the "current" state of the outputA
   // If the previous and the current state of the outputA are different, that means a Pulse has occured
   if (aState != aLastState){     
     // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
     if (digitalRead(outputB) != aState) { 
       counter ++;
     } else {
       counter --;
     }
     Serial.print("Position: ");
     Serial.println(counter);
   } 
   aLastState = aState; // Updates the previous state of the outputA with the current state
 }

I’m not sure how to make it look fancy like I’ve seen in other picture but here is the diagram I drew up in paint
Diagram|690x388

Sounds like the encoder is wired incorrectly and it is only ‘transmitting’ when not connected because the inputs are left floating. Check your wiring. I suspect there is a mistake. Of course without knowing what encoder you have and how it is wired I can’t say what the mistake is.

I have this encoder if that helps. By check the wiring do you mean like crack it open and make sure nothing is like shorted or disconnected?

Not even the pull up resistors, as explained in the comments on the Amazon page?
Leo…

If I change the INPUT to INPUT_PULLOUT it still doesn’t work

How does your wiring compare to the description:

Wiring Output:
A phase, B phase, Vcc positive supply, V0, the shield wire.
Connection:
Green = A phase, white = B phase, red = power +, black = V0
Note: AB two -phase outputs must not be connected directly to VCC, otherwise, they will burn the output transistor.
Output
AB two -phase quadrature output rectangular pulse, the circuit output is NPN open collector output type ,the output of this type can and with additonal added pull-up resistor(not included) is directly connected MCU or PLC, such as 51 single or Mitsubishi PLC (PLC input mode should be connected to 0V switch function ).
Note:
If the encoder is not connected to the device can not be directly connected to oscilloscope ( open collector output without pull-up resistor when there is no voltage output) If you want the oscilloscope in AB two-phase output, work with two pull-up resistors(not included).

Okay I think I just don’t understand how digital pull-ups work. I thought they could replace pullup resistors but whenever I put in pull up resistors in it works so thanks!

The internal pullups are like 35K to 50K, very weak.
10K external pullups down to maybe 1K are much better for a reliable signal.

Hi,

If I change the INPUT to INPUT_PULLOUT it still doesn’t work

Think you mean

INPUT_PULLUP

A circuit diagram would have helped.

Tom… :smiley: :+1: :coffee: :australia:

Run this sketch and Tools->SerialPlotter. The upper trace is OutputA and the lower trace is OutputB. They should produce pulses that are 90° out of phase (the edges on one channel should be in the middle of a pulse on the other). If a trace is flat when you turn the shaft, your connection is bad.

#define outputA 2
#define outputB 3

void setup()
{
  Serial.begine(115200);
  delay(200);

  pinMode (outputA, INPUT);
  pinMode (outputB, INPUT);
}

void loop()
{
  Serial.print(digitalRead(outputA) + 2);
  Serial.print(' ');
  Serial.println(digitalRead(outputB));
}
1 Like

Perhaps he was thinking of a means of contraception? :rofl: