Lower rotary encoder range

Hello. Here is my code I took from examples.
I am using arduino uno with rotary encoder lpd3806
360 degrees has 2400 pulses if I remember correctly.

Here is my code:

#include <Arduino.h>
#include <RotaryEncoder.h>
#define PIN_IN1 12
#define PIN_IN2 13
#define ROTARYSTEPS 5
#define ROTARYMIN 0
#define ROTARYMAX 360

RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::TWO03);

int lastPos = -1;

void setup()
{
  Serial.begin(115200);
  while (! Serial);

}
void loop()
{
  encoder.tick();


  int newPos = encoder.getPosition() * ROTARYSTEPS ;


  if (newPos < ROTARYMIN) {
    encoder.setPosition(ROTARYMIN / ROTARYSTEPS);
    newPos = ROTARYMIN;

  } else if (newPos > ROTARYMAX) {
    encoder.setPosition(ROTARYMAX / ROTARYSTEPS);
    newPos = ROTARYMAX;
  } // if

  if (lastPos != newPos) {
    Serial.print(newPos);
    Serial.println();
    lastPos = newPos;

  }
}


atm I turn the encoder maybe 1/6 turns and its already showing me 360
I need to make it to show 360 after I've turned the encoder 360

And yes the code needs to stop at 0 and at 360.

I am trying to make antenna rotator with logic: after antenna turns 360 from starting point, it stops so no wires break. only way back is to turn motors other way closer to 0.

But first I need to raise the range of the encoder somehow.
I've tried lastPos = newPos / 800; etc..... without luck.

If I take the ROTARYMAX value to the sky like 36000000
then ONE full turn is 6000 on serial monitor.

What kind of encoder do you use? Mechanical contacts may need debouncing.

Perhaps an absolute encoder may be a better choice for your application, giving the absolute direction of the antenna immediately.

Hi,
Can you post a schematic diagram?
Please include power supplies, component names and pin labels.

That encoder has NPN OPEN Collector outputs, have you got pullup resistors fitted?

What is the EXACT PART NUMBER of your encoder?
The exact number is very important.

Thanks.. Tom.. :smiley: :+1: :coffee::australia:

the antenna turns 2 turns per minute so its slow enough :smiley:

rotary encoder lpd3806-600BM-g5-24c

4 wires, black and red so gnd and 5v.
then 2 wires are connected to uno's pin 12 and 13.
No resistors etc...

this is same encoder than in my last project and there I got it working easy.

#include <Encoder.h>
Encoder windvane(A2, A3);
void setup() {
  Serial.begin(9600);
}
int old_pos = windvane.read();
const char  wd[][4] = {{" N "}, {"NNE"}, {" NE"}, {"ENE"}, {" E "}, {"ESE"}, {" SE"}, {"SSE"}, {" S "}, {"SSW"}, {" SW"}, {"WSW"}, {" W "}, {"WNW"}, {" NW"}, {"NNW"}};
void loop() {
  int i = windvane.read();;
  i = windvane.read();
  if (i != old_pos) {
    Serial.println(wd[abs(i % 2400)/280]);
    old_pos = windvane.read();
  }
}

this is my last project code. almost same thing.
Serial.println(wd[abs(i % 2400)/280]);
takes the value and does 2400/280 and I get perfect one turn = 360degree

Now I have to somehow make same 2400/280 here but I have no idea where I have to add it

There seem to be many different versions, and the device SHOULD be marked with the resolution; but from your code number its 600 pulses per rev.

https://www.amazon.co.uk/Rotary-Encoder-Module-LPD3806-Arduino/dp/B077K5K2XN

Hi,

Detail:

  • incremental rotary encoder, AB two phases
  • 600 pulses per revolution
  • DC 5-24V power supply
  • Maximum mechanical speed of 6000 rev / min.
  • The response frequency: 0-20KHz

From..

Have you tried adding the pullup resistors, 4K7 should be enough.
You have a 600ppr encoder by the looks of the specs.

Tom... :smiley: :+1: :coffee: :australia:

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