Rotary encoder on Lincoln Utility board
The code for a rotary encoder is easy to incorporate into your project. As far as hardware is concerned you need two Pico pins to use for interrupt routines, connected to your encoder.
The following is a standard basic test program and the operation of this is shown in the video. The test program works well even without a 56nF capacitors across the pins to ground.
The following link lists the rotary.py and rotary_irq_rp2.py files that are needed for this test.
- MCU units with sensors and datalogging.
- Break out boards with user interface. Suitable for datalogging and interactive function including stepper motors.
- Break out board customizations. Sensors, stepper motors, WiFi, SD cards and Fram.
- Design of circuit boards, including design of circuit boards that are a customization of a break out board pilot project.
- WiFi and server collection of data.
# example for MicroPython rotary encoder
import sys
if sys.platform == 'esp8266' or sys.platform == 'esp32':
from rotary_irq_esp import RotaryIRQ
elif sys.platform == 'pyboard':
from rotary_irq_pyb import RotaryIRQ
elif sys.platform == 'rp2':
from rotary_irq_rp2 import RotaryIRQ
else:
print('Warning: The Rotary module has not been tested on this platform')
import time
r = RotaryIRQ(pin_num_clk=14,
pin_num_dt=15,
min_val=0,
max_val=5,
incr=1,
pull_up=True,
half_step=True,
reverse=False,
range_mode=RotaryIRQ.RANGE_WRAP)
val_old = r.value()
while True:
val_new = r.value()
if val_old != val_new:
val_old = val_new
print('result =', val_new)
time.sleep_ms(50)
==============
Program Output
==============
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot
result = 1
result = 2
result = 3
result = 4
result = 5
result = 0
result = 1
result = 2
result = 3
result = 2
result = 1
result = 0
result = 5
result = 4
result = 3
result = 2
result = 1
result = 0


Push button example
This push button example uses an interrupt event handler that triggers on rising and falling signals from the switch. Here is the video. The test program works well even without a 56nF capacitor across the switch.
- MCU units with sensors and datalogging.
- Break out boards with user interface. Suitable for datalogging and interactive function including stepper motors.
- Break out board customizations. Sensors, stepper motors, WiFi, SD cards and Fram.
- Design of circuit boards, including design of circuit boards that are a customization of a break out board pilot project.
- WiFi and server collection of data.
from machine import Pin
import time
interrupt_flag=0
pin = Pin(11,Pin.IN,Pin.PULL_UP)
led = Pin(25, Pin.OUT)
def callback(pin):
global interrupt_flag
interrupt_flag= 1 if pin.value() == 0 else 0
led.value(interrupt_flag)
pin.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=callback)
downtime = 0
counter = 0
while True:
if interrupt_flag is 1:
print(str(counter) + " Button is down")
else:
print(str(counter) + " Button is up")
if interrupt_flag == 0:
downtime = 0
if downtime > 25:
interrupt_flag=0
time.sleep_ms(200)
counter += 1
downtime += 1
==============
Program Output
==============
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot
0 Button is up
1 Button is up
2 Button is up
3 Button is up
4 Button is up
5 Button is up
6 Button is up
7 Button is up
8 Button is up
9 Button is up
10 Button is up
11 Button is up
12 Button is up
13 Button is up
14 Button is up
15 Button is up
16 Button is up
17 Button is up
18 Button is up
19 Button is up
20 Button is up
21 Button is up
22 Button is up
23 Button is up
24 Button is up
25 Button is up
26 Button is up
27 Button is up
28 Button is up
29 Button is up
30 Button is down
31 Button is down
32 Button is down
33 Button is up
34 Button is up
35 Button is up
36 Button is down
37 Button is down
38 Button is down
39 Button is up
40 Button is up
41 Button is up
42 Button is up
43 Button is up
44 Button is up
45 Button is down
46 Button is down
47 Button is down
48 Button is up
49 Button is up
50 Button is up
51 Button is up
52 Button is up
53 Button is down
54 Button is down
55 Button is up
56 Button is up
57 Button is up
58 Button is up
59 Button is down
60 Button is down
61 Button is down
62 Button is down
63 Button is down
64 Button is up
65 Button is up
66 Button is up
67 Button is up
68 Button is up
69 Button is down
70 Button is down
71 Button is down
72 Button is down
73 Button is down
74 Button is up
75 Button is up
76 Button is up
77 Button is up
78 Button is up
79 Button is up
80 Button is up
81 Button is up
82 Button is down
83 Button is down
84 Button is down
85 Button is down
86 Button is up
87 Button is up
88 Button is up
89 Button is up
90 Button is up
91 Button is up
92 Button is up
93 Button is down
94 Button is up
95 Button is up
96 Button is down
97 Button is up
98 Button is up