0

Hey I'm very new to raspberry pi and decided to start a mall project including a rotary encoder.

I found some guides online that I followed but I just can't get any signal from the encoder transmitted to my python script.

Here's the simple python script:

import RPi.GPIO as GPIO
import time

CLOCKPIN = 5
DATAPIN = 6
SWITCHPIN = 13

def rot(dir):
  print("rotation: %" % dir)

def press():
  print("pressed")

if __name__ == "__main__":
  GPIO.setmode(GPIO.BCM)
  GPIO.setup(CLOCKPIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  GPIO.setup(DATAPIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  GPIO.setup(SWITCHPIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

  counter = 0
  clkLastState = GPIO.input(CLOCKPIN)
  swtLastState = GPIO.input(SWITCHPIN)

  print("start messing around...")

  try:
    while True:
      clkState = GPIO.input(CLOCKPIN)
      dtState = GPIO.input(DATAPIN)
      swtState = GPIO.input(SWITCHPIN)
      if clkState != clkLastState:
        if dtState != clkState:
          counter += 1
        else:
          counter -= 1
      if swtState != swtLastState:
        print("click")
        time.sleep(1.0)
      print(counter)
      clkLastState = clkState
      time.sleep(0.05)
  finally:
    GPIO.cleanup()

when I check the GPIOs with ". gpiotest" I get:

Testing...
Pull up on gpio 30 failed.
Skipped non-user gpios:
Tested user gpios: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22     23 24 25 26 27 28 29 30 31
Failed user gpios: 30

And finally here are pictures how I connected the encoder with the gpios:

enter image description here

enter image description here

enter image description here

What am I doing wrong? Thanks for the help!

  • Hi, from your photos it looks like the problem is that you've forgotten to connect the Raspberry Pi to the GPIO pins :) . – Roger Jones Mar 9 at 18:44
0

What model Pi? pigpio seems to think GPIO 0-31 are all valid.

I would change all the

pull_up_down=GPIO.PUD_DOWN

to

pull_up_down=GPIO.PUD_UP

I don't know the module you are using but mechanical rotary encoders typically pull to ground and float to the external voltage. Unless the module has its own pull-ups you need the Pi GPIO to be normally pulled up.

If that doesn't work could you see what monitor.py reports.

  • Sorry forgot about that. Its a Raspberry pi zero w with Raspian Jessie The Rotary is the KY040 – coffeesteak Mar 9 at 17:22
  • I made the changes yu suggested but I still can't get a signal. The monitor.py gives me somthing like this: [...] G=6 l=1 d=8701 G=13 l=1 d=9240 G=14 l=1 d=9455 G=5 l=0 d=11315 G=6 l=0 d=11325 G=13 l=0 d=10781 G=14 l=0 d=10565 G=5 l=1 d=8710 G=6 l=1 d=8700 G=13 l=1 d=9294 G=14 l=1 d=9435 G=5 l=0 d=11359 G=6 l=0 d=11329 G=13 l=0 d=10726 G=13 l=1 d=5 G=13 l=0 d=5 G=14 l=0 d=10581 G=5 l=1 d=8780 G=6 l=1 d=8775 G=13 l=1 d=9324 G=14 l=1 d=9459 [...] – coffeesteak Mar 10 at 19:03
  • That could well be the encoder pulses. Could you get a screenshot with piscope? abyz.me.uk/rpi/pigpio/piscope.html – joan Mar 10 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.