I’ve put together a pi pico, motor driver board, and a motor with 2 integrated hall effect encoders. The latter is from adafruit and the example code is written for Arduino. I’ve attempted to adapt this for MicroPython and my specific motor driver, but I just seem to get ‘0 RPM’ when the motor is moving, or when I manually move the encoder wheel. I’ve been at this for a while and I’m at a loss, any clue where I might be going wrong?
from machine import Pin
import time
import PicoRobotics
# connect to the two encoder outputs
ENCODER_A = 24
ENCODER_B = 25
# ticks-to-RPM conversion factors
GEARING = 20
ENCODERMULT = 12
# encoder pins
encoder_a = Pin(ENCODER_A, Pin.IN, Pin.PULL_UP)
encoder_b = Pin(ENCODER_B, Pin.IN, Pin.PULL_UP)
# initialise variables
RPM = 0
lastA = time.ticks_us()
def interruptA(pin):
global lastA, RPM, motordir
motordir = encoder_b.value()
currA = time.ticks_us()
if time.ticks_diff(currA, lastA) > 0:
rev = time.ticks_diff(currA, lastA)
rev = 1.0 / rev
rev *= 1000000
rev *= 60
rev /= GEARING
rev /= ENCODERMULT
RPM = rev
lastA = currA
encoder_a.irq(trigger=Pin.IRQ_RISING, handler=interruptA)
def printRPM():
print(int(RPM), " RPM")
def loop():
board = PicoRobotics.KitronikPicoRobotics()
directions = ["f","r"]
board.motorOn(1, "f", 20)
while True:
time.sleep(0.05)
printRPM()
loop()