breadcat248
Posts: 1
Joined: Mon May 13, 2019 12:40 am

Retrieving counts from an [Absolute Rotary Encoder]

Mon May 13, 2019 12:57 am

Hi fellow users, i am currently trying to interface a (1024 PPR or 10-bit) absolute rotary encoder: https://www.bourns.com/docs/product-dat ... EMS22A.pdf . My aim is to display all 0-1023 counts from the encoder by rotating the shaft but so far the count only displays 1023. Any help would be greatly appreciated. Have a nice day!

Code: Select all

import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

PIN_CLK = 2
PIN_DAT = [3,14]
PIN_CS  = 4
delay = 0.0000005
ns = 1                              # number of sensors attached

bitcount = 16                  # total 10 bits to be extracted from SSI signal

# pin setup done here
try:
    GPIO.setup(PIN_CLK,GPIO.OUT)
    GPIO.setup(PIN_DAT[:],GPIO.IN)
    GPIO.setup(PIN_CS,GPIO.OUT)                                                                                                    
    GPIO.output(PIN_CS,1)
    GPIO.output(PIN_CLK,1)
except:
    print ("ERROR. Unable to setup the configuration requested" )                                    

#wait some time to start
time.sleep(0.5)

print ("GPIO configuration enabled")

def clockup():
    GPIO.output(PIN_CLK,1)
def clockdown():
    GPIO.output(PIN_CLK,0)
def MSB():
    # Most Significant Bit
    clockdown()

def readpos():
    GPIO.output(PIN_CS,0)
    time.sleep(delay*2)
    MSB()
    data = [0]*ns
    
    for i in range(0,bitcount):
        if i<10:
            #print i
            clockup()
            for j in range(0,ns):
                data[j]<<=1  
                data[j]|=GPIO.input(PIN_DAT[j])
            clockdown()
        else:
            for k in range(0,6):
                clockup()
                clockdown()
    GPIO.output(PIN_CS,1)
    return data

try:
    while(1):
        print (readpos())
        time.sleep(0.001)
        #break
        
finally:
    print ("cleaning up GPIO")
    GPIO.cleanup()

User avatar
MrYsLab
Posts: 331
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Retrieving counts from an [Absolute Rotary Encoder]

Mon May 13, 2019 1:44 pm

I don't have any experience with this encoder, but I did find a Git repository that handles it for the Arduino. https://github.com/heavydetail/EMS22A_Arduino Hopefully it should give you some ideas about how to control the device, but of course you will need to port the C++ to Python. You might want to contact the author if you need some guidance. Here is his email address: [email protected]

ghp
Posts: 1352
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Retrieving counts from an [Absolute Rotary Encoder]

Mon May 13, 2019 5:30 pm

Hello, according to the datasheet there are 3.3V and 5V flavors of this device available. Be sure to use the 3.3V device or provide a level shifter for the input signal.
The device has two data pins DI (data in) and DO (data out). The data input DI pin should be set to low when not needed to cascade multiple encoders. You should only read from the DO data out pin. Could be that this solves the problems.

pootle
Posts: 320
Joined: Wed Sep 04, 2013 10:20 am
Location: Staffordshire
Contact: Website

Re: Retrieving counts from an [Absolute Rotary Encoder]

Tue May 14, 2019 7:09 am

Is it always the same value that is missing? If so which is the missing value?

Return to “Python”