Hi pigs,
this is another experiment. In the sunfounder kit talked in the prevoius post there are a lot of components. One of them is the mystical ROTARY ENCODER!
What is a rotary encoder?
It is a device that convert the angular position into a digital or analogical signal.
(http://en.wikipedia.org/wiki/Rotary_encoder). This means that when you rotate knob clokwise or anticlokwise you can let increment or decrement an index, sound volume, shuffle on files, ecc.
The question is: how i can know if the rotary encoder is going clockwise or anti-clokwise? The answer should be this: use two pins with two state and a variable with associate last state. If you are going clockwise , pin A is in state "true" and pin B is in state "false" otherwise pin B is on "true" and pin A is on "false". This isn't the exact explain of how this works but is what i understand....
So what i want to do here is
Increment an index in python when rotating knob on rotary encoder.
what we need?
ok we can go.
Rotary encoder has 5 pins:
we need to connect + to 3.3v pin on raspberry GPIO, GND to GND on raspberry GPIO. CLK and DT are our pin A and pin B and with them we can know if we are turning clockwise or anti-clockwise.Connect CLK pin to GPIO 23 pin and DT pin GPIO 24. Ok well done... there is nothing else to connect.
This is the setup:
ok we can code. The code is almost simple:
import RPi.GPIO as GPIO
### Config
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
# check initial state
oldPinA = GPIO.input(23)
index = 0
inc = 5 # increment unit
while True:
encoderPinA=GPIO.input(23)
encoderPinB=GPIO.input(24)
if(encoderPinA == True) and (oldPinA==False):
if(encoderPinB == False):
index = index + inc # clockwise
else:
index = index-inc # anticlockwise
print index
oldPinA=encoderPinA
ok we have a var oldPinA with the starting state og GPIO pin 23, the index value and the increment value.
In an infinite loop we set always the state of pin 23 and pin 24.
After turning the state is set to false (oldPinA) and so if oldPinA is false and actual state of pin A is true this means that we are rotating the know.If the state of pin B is false we are turning clockwise despite we are turning anti-clockwise.
ok you can go to sleep happy
this is another experiment. In the sunfounder kit talked in the prevoius post there are a lot of components. One of them is the mystical ROTARY ENCODER!
What is a rotary encoder?
It is a device that convert the angular position into a digital or analogical signal.
(http://en.wikipedia.org/wiki/Rotary_encoder). This means that when you rotate knob clokwise or anticlokwise you can let increment or decrement an index, sound volume, shuffle on files, ecc.
The question is: how i can know if the rotary encoder is going clockwise or anti-clokwise? The answer should be this: use two pins with two state and a variable with associate last state. If you are going clockwise , pin A is in state "true" and pin B is in state "false" otherwise pin B is on "true" and pin A is on "false". This isn't the exact explain of how this works but is what i understand....
So what i want to do here is
Increment an index in python when rotating knob on rotary encoder.
what we need?
- rotary encoder
- some wire cable
- a low-cost beer
ok we can go.
Rotary encoder has 5 pins:
- CLK
- DT
- SW
- +
- GND
we need to connect + to 3.3v pin on raspberry GPIO, GND to GND on raspberry GPIO. CLK and DT are our pin A and pin B and with them we can know if we are turning clockwise or anti-clockwise.Connect CLK pin to GPIO 23 pin and DT pin GPIO 24. Ok well done... there is nothing else to connect.
This is the setup:
ok we can code. The code is almost simple:
import RPi.GPIO as GPIO
### Config
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
# check initial state
oldPinA = GPIO.input(23)
index = 0
inc = 5 # increment unit
while True:
encoderPinA=GPIO.input(23)
encoderPinB=GPIO.input(24)
if(encoderPinA == True) and (oldPinA==False):
if(encoderPinB == False):
index = index + inc # clockwise
else:
index = index-inc # anticlockwise
print index
oldPinA=encoderPinA
ok we have a var oldPinA with the starting state og GPIO pin 23, the index value and the increment value.
In an infinite loop we set always the state of pin 23 and pin 24.
After turning the state is set to false (oldPinA) and so if oldPinA is false and actual state of pin A is true this means that we are rotating the know.If the state of pin B is false we are turning clockwise despite we are turning anti-clockwise.
ok you can go to sleep happy
Commenti
Posta un commento