neteng
Posts: 24
Joined: Wed May 11, 2016 3:56 am

Rotary Encoder

Mon Oct 10, 2016 2:21 am

Hi All,

Below is a code for a rotary encoder:

Code: Select all

from RPi import GPIO
from time import sleep

clk = 17
dt = 18


GPIO.setmode(GPIO.BCM)
GPIO.setup(clk, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(dt, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)


counter = 0
clkLastState = GPIO.input(clk)

try:

        while True:
                clkState = GPIO.input(clk)
                dtState = GPIO.input(dt)
                if clkState != clkLastState:
                        if dtState != clkState:
                                counter += 1
                        else:
                                counter -= 1                                   
                        print (counter)
                clkLastState = clkState
                
finally:
        GPIO.cleanup()


When I plug my encoder Vcc and GND to the 3v3 and GND rail, and run the code.....It runs fine.....I am able to get both counter and counter clockwise counts......that being....my count values increases as it goes in one direction, and decreases in the other direction...

However, I want to connect it to an external power supply as I want to use 6 encoders and 5 limit switches so that sufficient power is supplied. But I don't get readings in both direction I only get reading in a single direction. When turned the other way, the reading just keeps on increasing, which shouldn't be the case as it should decrease.

I tried connecting all the peripherals to the power rails of the pi, but it restarted......and when external power is removed and connected again while all the peripherals are connected, the pi wont start up....

What could be the problem??...

Thanks,
NT

User avatar
Jednorozec
Posts: 809
Joined: Sun Nov 24, 2013 2:17 pm
Location: Deposit, NY

Re: Rotary Encoder

Mon Oct 10, 2016 6:03 am

Is your external supply 3.3V? Do you have the RPi and external supply GNDs connected?
The most important leg of a three legged stool is the one that's missing.
It's called thinking. Why don't you try it sometime?

hal8000
Posts: 187
Joined: Fri Oct 04, 2013 5:22 pm

Re: Rotary Encoder

Mon Oct 10, 2016 7:18 am

What peripherals are you connecting and how are you connecting the other rotary encoders?
The Pi only has limited output capabilities so to switch a heavier current device you would either
need a relay driver or MOSFET driver on one of the GPIO pins.

You would also have to write the same code again but using a different variable for each rotary encoder.

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Rotary Encoder

Mon Oct 10, 2016 7:26 am

There is no need for an external supply, GPIO's set to input take very little current (current limiting resistors should be added to prevent damage if the GPIO's accidentally get switched to output).

The chances are that your external supply has broken your pi, does it run with nothing connected to the GPIO?
Doug.
Building Management Systems Engineer.

neteng
Posts: 24
Joined: Wed May 11, 2016 3:56 am

Re: Rotary Encoder

Mon Oct 10, 2016 10:57 am

Hi again everyone,

Thanks for the reply...

Finally able to get my optical encoders working.The limiting resistors on the GPIO input were the problem...I think they had provided a far greater potential drop, making the pi unable to read the inputs.....the gpio input limiting resistors were 1.1K....Maybe they need to be lesser......???

BUT..I am cautious of the GPIO terminal.....Would some one suggest a way to protect it now...

Thanks,
neteng

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Rotary Encoder

Mon Oct 10, 2016 11:36 am

If you want to be ultra cautious then the RASPIO pro HAT or RASPIO easywire pro adds zener diodes for over-voltage protection and 330 ohm resistors for over-current protection.
Doug.
Building Management Systems Engineer.

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Rotary Encoder

Mon Oct 10, 2016 12:53 pm

With my (limited) experience of rotary encoders, you do not need to connect any supply to them. Instead of using the internal pull-down resistors and a power rail, just use the pull-ups instead......
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

ewaller
Posts: 32
Joined: Tue Oct 04, 2016 5:24 pm

Re: Rotary Encoder

Mon Oct 10, 2016 6:11 pm

If you want to be completely safe -- and you don't want to tie the external power supply ground to the Raspberry Pi ground, you can use optoisolators

http://www.arunet.co.uk/tkboyd/ec/ec1optoiso.htm

Return to “General discussion”