Activity Forums Questions & Troubleshooting Encoder on MINI

  • Encoder on MINI

  • Johnnycatt

    Member
    April 25, 2021 at 11:05 am

    I have been using Arduinos for about 7 years and consider myself to be a “good” programmer, but a “bad” electrician.

    I am replacing an Arduino NANO with a Controllino MINI in a system that measures logs as they pass by a photo-eye. When the eye senses a log, the MINI/NANO starts counting pulses from an encoder and translates the pulses into inches.

    I am using a LPD3806 encoder attached at A0 and A1 (encoder and MINI attached to same 12v and ground) It is here: http://domoticx.com/sensor-lpd3806-optical-rotary-encoder/

    I get NO reading from the encoder on the MINI.

    I have read what small information I could find on Controllino and encoders and have found that I cannot use “pinMode(A0, INPUT_PULLUP)” like I did on the Arduino NANO. Also, I have read is that I need an EXTERNAL “Pull-Up” resistor on the A and B legs of the the encoder. I don’t know how to do this. If it is my only option, can you show me to a good tutorial and a good “pull-up resistor board” to buy?


    I did find the KY-040 rotary encoder (https://www.epitran.it/ebayDrive/datasheet/25.pdf) worked fine with the Controllino MINI using just the simple “pinMode(A0, INPUT)” – this may be due to the documentation saying the KY-040 has a built-in “pull-up resistor.”

    Question #1:

    Obviously, for my measuring device, I cannot use the KY-040 and I want to use the LPD3806. The LPD3806 works perfectly with an Arduino using INPUT-PULLUP. My question is, can I attach the LPD3806 to the MINI’s Pinheader (for example, using. D2 and D3) on the MINI and then use “pinMode(2, INPUT_PULLUP)” code?

    Question #2:

    the LPD3806 is called a NPN encoder. If I switched to a PNP encoder, would that work directly with the A0 and A1 screw terminals using “pinMode(A0, INPUT)”?

  • Johnnycatt

    Member
    May 1, 2021 at 10:17 am

    For those who may need this answered in the future.

    After doing a bit of digging, I discovered this thread:

    https://github.com/CONTROLLINO-PLC/CONTROLLINO_Library/issues/26

    Which eventually led to this solution:

    https://github.com/CONTROLLINO-PLC/CONTROLLINO_Library/pull/32/commits/502b37939f477f97c52b6a42424611255b7f9f9a

    In short, I simply needed two “pull-up resistors.” To solve my problem and get the LPD3806 encoder working, I added a resistor (I used a 6.8kOhm) to both input pins (Input0 and Input1) in the same input screw terminals that the Encoder leads were attached. I connected the resistors to the 12v power supply.

    In the Code, I changed pinMode(1, INPUT_PULLUP); and pinMode(2, INPUT_PULLUP); to pinMode(1, INPUT); and pinMode(2, INPUT);

    WORKS GREAT! This is the code Used to read the encoder (I did not use the CONROLLINO.h library for this project):

    //***************************BEGIN CODE******************************

    unsigned long A = 3;

    unsigned long B = 2;

    unsigned long currentStateA;

    unsigned long lastStateA;

    unsigned long pulse;

    unsigned long previousPulse;

    void setup () {

    pinMode(A,INPUT); // not INPUT_PULLUP since we are attaching external Pullup reistors

    pinMode(B,INPUT); //not INPUT_PULLUP since we are attaching external Pullup reistors

    lastStateA = digitalRead(A);

    Serial.begin(9600);

    }

    void loop() {

    currentStateA = digitalRead(A);

    if (currentStateA != lastStateA && currentStateA == 1){

    if (digitalRead(B) != currentStateA) {

    pulse –;

    }

    else {

    pulse ++;

    }

    }

    lastStateA = currentStateA;

    //delay(1) you may need to add a very short delay here to de-bounce signal depending on // your speed (RPM)

    if (pulse != previousPulse) {

    Serial.println(pulse);

    }

    previousPulse = pulse;

    }

    //*******************************END CODE*****************************

    Controllino is a great product, I wish it had switches to disable the internal pull-down resistors to allow for “INPUT_PULLUP’ code to be used, but hopefully this answer helps someone in the future.

    • This reply was modified 3 years, 8 months ago by  Johnnycatt.
  • LeoOfVenice

    Member
    May 2, 2021 at 4:12 pm

    Hello
    I’m going to make the same connection with the same Rotary Encoder.

    I have already done a test with arduino UNO board where channel A connected is to pin 2 and channel B connected to pin 3, to use interrupts, everything works fine here is the setup for connection on UNO BOARD.

    …..

    void setup() {
    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);
    }

    …..

    Now, with CONTROLLINO I want to make same function and I’m happy to read your post with your suggestions but I am little confused (is possible to have a picture of the connections?):

    i am looking at your schetch:

    unsigned long A = 3; // ( is this interrupt pin ? )
    unsigned long B = 2;


    pinMode (A, INPUT); // not INPUT_PULLUP since we are attaching external Pullup reistors
    pinMode (B, INPUT); // not INPUT_PULLUP since we are attaching external Pullup reistors
    St.

    Are you using pin header or screw terminal?

    I suppose screw terminal, beacuse you connect PULL-UP resistors to 12V ,but If you are using screw terminals, why don’t you initialize them as A3 or A2 in the schetch, and you define input as 3 an 2 as PIN HEADER?

    <div>

    And instead..

    If you are using pin headers why do you use the PULL-UP resistor connected with 12 V and not with 5V ?

    (It would be interesting to be able to insert images in these posts.)

    Thank in advance for your help

    </div>

  • Johnnycatt

    Member
    May 4, 2021 at 9:46 am

    Leo,

    I am using the Screw Terminals A0 and A1.

    I am NOT using the <controllino.h> library, but just the basic “arduino coding.” I have made a mistake and uploaded my Arduino UNO code I was using for practice, NOT my Controllino Code (I will try to edit the original sketch).

    The Controllino code should read:

    unsigned long A = A0;

    unsigned long B = A1;


    NOTE: “unsigned long” is not needed here, but I originally used “int” for all my variables in the sketch and with a 600 pulse-per-revolution enocoder, I ran out of numbers very fast and replaced ALL my “int” with “unsigned long.”

    As for the Pull-up resistors, I soldered two 6.8kOhm resistors (in parallel) to the end of a wire. One resistor was put into A0 and the other in A1. The other end of the wire was connected to my 12V supply.


    AS for the encoder, Channel A from the encoder is ALSO in Screw Pin A0. Channel B is in Screw Pin A1 with it’s resistor. It is not a pretty solution, but it works perfectly.

    • This reply was modified 3 years, 8 months ago by  Johnnycatt.
    • This reply was modified 3 years, 8 months ago by  Johnnycatt.
    • This reply was modified 3 years, 8 months ago by  Johnnycatt.
    • LeoOfVenice

      Member
      May 4, 2021 at 12:55 pm

      Thanks
      @Johnnycatt now it is clear…

      This is my first project with a CONTROLLINO MINI

      I’m following this forum for a long time to see how
      connect this kind of encoder …
      and finally saw your post. !

      It is a test machine to compress a spring and test speed in extension.

      In this project there is a pneumatic actuator, used to compress the spring,
      controlled by one relay port of controllino

      There is a magnetic sensor on actuator to report
      if the spring is compressed and start the measurement

      The encoder is used to calculate the speed of spring in extension

      At the end of spring stroke there is another
      proximity sensor to detect if the spring has ended its movement.

      All data are send on a NEXTION screen where buildng a GRAPH of speed.

  • Johnnycatt

    Member
    May 4, 2021 at 11:01 pm

    Sounds great!

    I used mine to count the revolutions of a Sprocket so I’d know where items were on a conveyor.

    On the Conveyor, we are measuring the length of Telephone poles, then sorting them into bins by length — so far it’s doing great!

  • pbro

    Member
    June 8, 2021 at 10:42 pm

    I am using the Mini for a similar situation; in my case the rotary encoder pulses let us know where cherries are as they move along the sorting line. The encoder is an Autonics E50S8-200-3-t-24, with totem pole output. The B phase is wired to Int0, the Z phase to Int1 and the A phase goes to pin A3 (PC3). The internal pullup resistors are enabled. The input signals are very clean.

    However, the output signals on pins D4 and D5 (PB0 and PB1) are not clean. The output pins float when the output value is 0. When the output is 1, the signals are a decent high. I have to include a pulldown resistor (1K) and with the pulldown, the output signals are quite clean. I don’t understand why I need an external pulldown for the ATmega328P pins. If anyone has a suggestion, it would be great.

  • pbro

    Member
    June 8, 2021 at 11:03 pm

    The following is a quote from page 59 of the Atmel ATmega328P manual.

    “If PORTxn is written logic one when the pin is configured as an output pin, the port pin is driven high (one). If PORTxn is written logic zero when the pin is configured as an output pin, the port pin is driven low (zero).”

    If this is true for the Controllino also, the output pin should not float if a zero is written to the pin. Of course, the usual error is with the programmer, so maybe I am missing a configuration step.

  • pbro

    Member
    June 8, 2021 at 11:15 pm

    <div>And another quote, this time from Wikipedia, concerning totem pole outputs:
    </div><div>

    “some push–pull outputs have a third state in which both transistors are switched off. In this state, the output is said to be floating (or, to use a proprietary term, tri-stated).”

    So, it sounds like the the Controllino Mini might have tri-stated output, which isn’t a bad idea in any case.

    </div>

    • Lukas

      Member
      June 15, 2021 at 10:32 am

      Hi pbro,

      you are right. CONTROLLINO screw terminal outputs are “High Side Switches”. So they are just connecting the supply voltage to the output, but not the ground.

      Please note that only CONTROLLINO MEGA has 12x Half-H bridge output with connects also the ground.

      Lukas

Viewing 1 - 8 of 8 replies

Log in to reply.

Original Post
0 of 0 posts June 2018
Now