0

I'm trying to build an Arduino project that counts pulses sent by an encoder.

I've bought an Omron E6B2-CWZ5B-2 2000P/R and face the issue of it sending pulses non-stop, even when I'm not touching it. The code I use is from this video.

I thought that the encoder itself was the problem and bought two encoders like in the video above (LPD3806-400BM-G5-24C), but I faced the exact same problem.

When I connect the encoder to the Arduino 5 V and GND ports, the encoder doesn't seem to work at all (in the video it does without any resistors, etc.). I had to use a different power source, which was a laboratory power supply, which I set to around 6 V.

I connected the A output to digital pin 2 and the B output to digital pin 3 of my Arduino Uno. The problem is - the values just start going up and down at an insane speed without me touching the encoder. If I only connect output A, they go like I'm spinning it hard left, if I connect only the B output, they go like I'm spinning it hard right. If I connect both of them, it's just a massacre of data, like I'm spinning it left and right insanely fast.

The other thing I noticed - if I power up my Laboratory power supply to 12 V and more it seems that the encoder sends even more data per sec.

I only managed to get it working properly once and can not repeat it now. I did the following:

I powered the LPD3806 with 5.5 V externally (lab power supply), took the ground shield of encoder and held it really tight to Arduino Uno USB port. Data spam stopped and the encoder worked properly when I'm twisting the shaft. But I can't repeat this thing now.

It's clear the problem is with grounding, but I wonder how I can fix it to use it in an okay way. And I still wonder how the author of the video above got results with no use of the ground shield at all.

So the question is - how do I fix it and where did I go wrong?

Here are some schematics. I don't know how to do them properly, so I did them with Wokwi. So to read it keep in mind that:

  • I marked A and B output cables of my LPD3806 encoder as CLK and DT on the rotary encoder from wokwi.com

  • The VCC and GND symbols at the very top of some schemas here are + and - of my laboratory power supply

  • I marked my ground shield as SW on the third schema to show where I held it to get it working.

Here's how I tried to power it from the Arduino itself. It just didn't start. No signal; nothing.

I tried to power it from the Arduino itself.

So I powered it from the lab power supply. Now the encoder started, but it was just spamming numbers. When I increased the voltage, the number-spamming got faster.

So I powered it from lab power supply

And here's how I got it working once by pulling the ground shield tight to the Arduino USB port (keep in mind in my schema I marked ground shield as SW on the encoder).

And here's how I got it working

Here's the code I use:

volatile unsigned int temp, counter = 0; // This variable will increase or decrease depending on the rotation of encoder
        
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 encoder activates ai0(). AttachInterrupt 0 is digital pin nr 2 on most Arduinos.

  attachInterrupt(0, ai0, RISING);
       
  // B rising pulse from encoder activates ai1(). AttachInterrupt 1 is digital pin nr 3 on most Arduinos.

  attachInterrupt(1, ai1, RISING);
}
       
void loop() {
  // Send the value of counter
  if (counter != temp) {
    Serial.println (counter);
    temp = counter;
  }
}
       
void ai0() {
  // ai0 is activated if digital pin 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 digital pin nr 3 is going from LOW to HIGH
  // Check with pin 2 to determine the direction
  if (digitalRead(2)==LOW) {
    counter--;
  } else {
   counter++;
  }
}

Looking forward for your answers and thanks in advance!

4
  • 1
    please add the wiring diagram
    – jsotola
    Commented Feb 4 at 22:26
  • 2
    From the description that sounds like a floating pin, though you already have the internal pullups activated. You wrote, that you use an external power supply for the encoder. Did you connect the grounds of that supply and the Arduino? Have you tried adding stronger external pullup resistors (like 1 to 4.7kOhm)? The seller site, that I found with this encoder claims, that you can connect it directly to Arduino 5V pin. Though it might, that your voltage is right at the lower end of the valid power voltage range of the encoder (4.8V). USB voltage can easily get as low as 4.5V
    – chrisl
    Commented Feb 4 at 23:23
  • @chrisl I did try it with external pull-up resistors - no help. But I moved forward a little bit! I did everything like before - I powered LPD3806 with 5.5v externally and got it working properly once. How I did it - I took the ground shield of encoder and held it really tight to Arduino Uno USB port. Data stopped and encoder only worked properly when I'm twisting the shaft. But I can't repeat this thing now. It's clear the problem is with grounding, but I wonder how can I fix it to use it in an okay way. And I still wonder how the author of the video got results without ground shield at all.
    – Do I Care
    Commented Feb 5 at 13:41
  • 1
    You absolutely need to connect all grounds. Your wiring diagram is missing the connection between external power supply ground and Arduino ground
    – chrisl
    Commented Feb 5 at 17:12

1 Answer 1

0

From the datasheet: the E6B2-CWZ5B 2000P/R 0.5M encoder has an output circuit which is described as PNP open collector. That is, the output swings from whatever voltage you are running the encoder at (12 V to 24 V) to high impedance:

encoder output

You probably want to wire it to your (5 V?) Arduino in this manner. The resistor divider is balanced for ~12 V:

connection schematic

Your loop() could be better structured to avoid non-atomic reads because it appears that you are using an 8-bit Arduino and an int is 16-bit. There is, therefore, a risk that an ISR will corrupt the variable counter while you are attempting to read it in the loop():

void loop() {
  // Send the value of counter
  noInterrupts() ;
  int counter_copy = counter ;
  interrupts() ;
  if (counter_copy != temp) {
    Serial.println (counter_copy);
    temp = counter_copy;
  }
}
5
  • Thanks for your answer! But the thing is - I tried it with Omron before, powering it with 12v adapter. Now I'm trying it with LPD3806-400BM-G5-24C, which is 5-24V. As the video above shows, it can be powered with Arduino but it doesn't seem to work that way. What I did now - I powered it with 5.5v externally and got it working properly once. How I did it - I took the ground shield and held it really tight to Arduino Uno USB port. Data stopped and only worked when I'm twisting the shaft. But I can't repeat this thing now. It's clear the problem is with grounding, but I wonder how can I fix it.
    – Do I Care
    Commented Feb 5 at 13:37
  • @DoICare . OK. The second device you have LPD3806-400BM-G5-24C has apparently an open collector (NPN) output so that is wired in a different way to the first so connecting it to an Arduino pin (per phase) and using the internal pullup is correct. The encoder can be driven still at a higher voltage that the the Arduino. However, it is also important that there is a common ground between the Arduino and the encoder. Anyway, show your wiring diagram. This is the nearest I have found to a data sheet with a quick search: domoticx.com/sensor-lpd3806-optical-rotary-encoder
    – 6v6gt
    Commented Feb 5 at 14:00
  • I've just looked at the video. The red wire from the encoder can only be connected to the Arduino if you are powering the encoder at 5v from the Arduino only. Otherwise the encoder red wire should go to an external power supply and again all grounds should be common.
    – 6v6gt
    Commented Feb 5 at 14:09
  • Thanks for your answer once again! I've updated my question with some schemas, would be great if you check it out. I'm pretty new to electronics and I struggle understanding how to set common ground. Again - when I tried to do this with ground shield held tight to Uno USB port, it did work but only for a while
    – Do I Care
    Commented Feb 5 at 15:00
  • Even when you use a lab power supply, all the grounds must be connected together. That is, the power supply ground, the arduino ground and the encoder ground (and 0v and shield if the encoder has these). So your last 2 pictures of the Uno setup are clearly wrong because there is no shared ground. Can you find a better data sheet for that LPD3806-400BM-G5-24C encoder?
    – 6v6gt
    Commented Feb 5 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.