Error reading pulses on jetson nano for rotary encoder LPD3806-600BM-G5-24C

Hi team,

I am working on getting the encoder pulse count on the jetson nano but i don’t seem to reach the milestone because i have tried many codes which count the pulses and print them on the screen when i physically turn the encoder using my hand i don’t see it printing any data.

import Jetson.GPIO as GPIO
from time import sleep

counter = 0

Enc_A = 17  
Enc_B = 27  


def init():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(Enc_A, GPIO.IN)
    GPIO.setup(Enc_B, GPIO.IN)
    GPIO.add_event_detect(Enc_A, GPIO.RISING, callback=rotation_decode, bouncetime=10)
    return


def rotation_decode(Enc_A):
    global counter
    sleep(0.002)
    Switch_A = GPIO.input(Enc_A)
    Switch_B = GPIO.input(Enc_B)

    if (Switch_A == 1) and (Switch_B == 0):
        counter += 1
        print("direction -> ", counter)
        while Switch_B == 0:
            Switch_B = GPIO.input(Enc_B)
        while Switch_B == 1:
            Switch_B = GPIO.input(Enc_B)
        return

    elif (Switch_A == 1) and (Switch_B == 1):
        counter -= 1
        print("direction <- ", counter)
        while Switch_A == 1:
            Switch_A = GPIO.input(Enc_A)
        return
    else:
        return

def main():
    try:
        init()
        while True :
            sleep(1)

    except KeyboardInterrupt:
        GPIO.cleanup()

if __name__ == '__main__':
    main()

I also connected the signal pins to oscilloscope to check and saw it is giving pulse and also in the code we are attaching interrupt to monitor both the pins whenever there is a rising edge but don’t know why jetson has trouble in counting those pulses, Requesting the community to guide me here in this case.

Board: Jetson-nano 1010
OS: Jetpack 4.6.3

Hi heb3779,

It seems a custom board from SeeedStudio.

Do you configure 2 pins as input and check its status for rising edge?
Is there no any counts or just the counts wrong?

Hi Kevin,

I have configured both the pins as input and checked for the the rising edge manually by giving it 5v and 0v at that time its working but when I run connect the motor and run the code it doesn’t count the pulses.

what is the jetson.gpio version that you are using?

By the time the event is detected to the time you read the GPIO pin the value might have changed. I suggest you rearchitect your code.

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