Khov
Posts: 24
Joined: Wed Jun 14, 2017 11:16 am
Location: France

Encoder random value

Fri Jun 08, 2018 5:20 am

Hi,

I'm beginner with Rapsberry PI ZERO W, I try a simple thing : counting the number of steps when I turn the incremental encoder PEC11R-4220F-N0012 (datasheet: https://www.mouser.fr/datasheet/2/54/EC11R-777457.pdf)
My problem is : Whatever I may rotate or not the encoder, the PI count anyway.

I cannot figure out what isgoing wrong. I show you how I have wired : Imagehttps://ibb.co/kmBy3T
This is the result I get on my terminal : Imagehttps://ibb.co/mrhGq8

I give you the code of 'Paul Versteeg' that I use.

Code: Select all

import RPi.GPIO as GPIO
from time import sleep


# Global constants & variables
# Constants
__author__ = 'Paul Versteeg'

counter = 10  # starting point for the running directional counter

# GPIO Ports
Enc_A = 23  # Encoder input A: input GPIO 23 (active high)
Enc_B = 24  # Encoder input B: input GPIO 24 (active high)


def init():
    '''
    Initializes a number of settings and prepares the environment
    before we start the main program.
    '''
    print "Rotary Encoder Test Program"

    GPIO.setwarnings(True)

    # Use the Raspberry Pi BCM pins
    GPIO.setmode(GPIO.BCM)

    # define the Encoder switch inputs
    GPIO.setup(Enc_A, GPIO.IN) # pull-ups are too weak, they introduce noise
    GPIO.setup(Enc_B, GPIO.IN)

    # setup an event detection thread for the A encoder switch
    GPIO.add_event_detect(Enc_A, GPIO.RISING, callback=rotation_decode, bouncetime=2) # bouncetime in mSec
    #
    return


def rotation_decode(Enc_A):
    '''
    This function decodes the direction of a rotary encoder and in- or
    decrements a counter.

    The code works from the "early detection" principle that when turning the
    encoder clockwise, the A-switch gets activated before the B-switch.
    When the encoder is rotated anti-clockwise, the B-switch gets activated
    before the A-switch. The timing is depending on the mechanical design of
    the switch, and the rotational speed of the knob.

    This function gets activated when the A-switch goes high. The code then
    looks at the level of the B-switch. If the B switch is (still) low, then
    the direction must be clockwise. If the B input is (still) high, the
    direction must be anti-clockwise.

    All other conditions (both high, both low or A=0 and B=1) are filtered out.

    To complete the click-cycle, after the direction has been determined, the
    code waits for the full cycle (from indent to indent) to finish.

    '''

    global counter

    sleep(0.002) # extra 2 mSec de-bounce time

    # read both of the switches
    Switch_A = GPIO.input(Enc_A)
    Switch_B = GPIO.input(Enc_B)

    if (Switch_A == 1) and (Switch_B == 0) : # A then B ->
        counter += 1
        print "direction -> ", counter
        # at this point, B may still need to go high, wait for it
        while Switch_B == 0:
            Switch_B = GPIO.input(Enc_B)
        # now wait for B to drop to end the click cycle
        while Switch_B == 1:
            Switch_B = GPIO.input(Enc_B)
        return

    elif (Switch_A == 1) and (Switch_B == 1): # B then A <-
        counter -= 1
        print "direction <- ", counter
         # A is already high, wait for A to drop to end the click cycle
        while Switch_A == 1:
            Switch_A = GPIO.input(Enc_A)
        return

    else: # discard all other combinations
        return



def main():
    '''
    The main routine.

    '''

    try:

        init()
        while True :
            #
            # wait for an encoder click
            sleep(1)

    except KeyboardInterrupt: # Ctrl-C to terminate the program
        GPIO.cleanup()


if __name__ == '__main__':
    main()

User avatar
joan
Posts: 15284
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Encoder random value

Fri Jun 08, 2018 7:14 am

I see no evidence of internal or external pull-ups on GPIO 23/24 so the lines are effectively floating except when pulled to ground.

Add some pull-ups to 3V3 on GPIO 23/24.

Also see http://abyz.me.uk/rpi/pigpio/examples.h ... encoder_py

User avatar
Z80 Refugee
Posts: 358
Joined: Sun Feb 09, 2014 1:53 pm

Re: Encoder random value

Fri Jun 08, 2018 7:36 am

The datasheet for that device, as linked in the first post, shows a "suggested filter circuit". Use that, but connect to the 3.3V GPIO pin instead of 5V as shown. "Terminal A" and "Terminal B" are your inputs to the GPIO, "Terminal C" is the 0V connection. The circuit shows the required pull-ups, and also a filter to combat switch contact bounce. A series resistor is necessary for GPIO protection (in case you accidentally make the pin an output), and the resistor shown for the filter will do that job.

Whenever a datasheet shows a recommended or suggested application circuit, use it unless you have a very good reason not to.

For more information about interfacing to the GPIO, see section "Hardware Interfacing" here: viewtopic.php?f=34&t=207597

E9B6C712-4DCF-426D-96AC-E860A87C981A.jpeg
E9B6C712-4DCF-426D-96AC-E860A87C981A.jpeg (99.79 KiB) Viewed 743 times
Military and Automotive Electronics Design Engineer (retired)

For the best service: make your thread title properly descriptive, and put all relevant details in the first post (including links - don't make us search)!

Khov
Posts: 24
Joined: Wed Jun 14, 2017 11:16 am
Location: France

Re: Encoder random value

Fri Jun 08, 2018 11:48 am

Thank you really much "Z80 Refugee" to have carefully answered my question (and for the drawing really useful).

I saw that circuit, but I didn't understand it. I was confused, I didn't understand the difference between CHANNEL A and TERMINAL A, I believed it was the same...

Could you tell me why you advice me to use 3.3V despite it is mentioned 5V in the datasheet ?

Thank you,

User avatar
Z80 Refugee
Posts: 358
Joined: Sun Feb 09, 2014 1:53 pm

Re: Encoder random value

Fri Jun 08, 2018 4:28 pm

Khov wrote:
Fri Jun 08, 2018 11:48 am
I didn't understand the difference between CHANNEL A and TERMINAL A, I believed it was the same...
I think you mean you thought that circuit might be already built into the rotary encoder. No, the rotary encoder stops at the dashed line. The drawing in the data sheet isn't very good, confusing use of terms etc, but TERMINAL A on the drawing is where the CHANNEL A signal comes out (for connection to your circuit - in this case the GPIO inputs).
Khov wrote:
Fri Jun 08, 2018 11:48 am
Could you tell me why you advice me to use 3.3V despite it is mentioned 5V in the datasheet ?
You must not put voltages higher than 3.3V onto the GPIO inputs. Simples. In this case using 3.3V won't stop the example circuit working.

For more information see the link I gave you before:
Z80 Refugee wrote:
Fri Jun 08, 2018 7:36 am
For more information about interfacing to the GPIO, see section "Hardware Interfacing" here: viewtopic.php?f=34&t=207597
Military and Automotive Electronics Design Engineer (retired)

For the best service: make your thread title properly descriptive, and put all relevant details in the first post (including links - don't make us search)!

Khov
Posts: 24
Joined: Wed Jun 14, 2017 11:16 am
Location: France

Re: Encoder random value

Sat Jun 09, 2018 12:38 pm

Once more thank you ! It is definitely clearer thanks to the quality of your answer.

Return to “Troubleshooting”