RangeSlicer is a general-purpose CircuitPython analog value converter that linearly scales the input then quantizes it into a collection of precise output slice values. The class detects input value changes and applies selectable hysteresis when slice edge thresholds are reached to eliminate dead-zone noise issues without filtering delays. Applications include converting rotary knob position to discrete ranges of MIDI values, analog signal noise processing, level detection, rotary switch emulation, and signal display.
The effect of using hysteresis to clean up a noisy signal:
This driver depends on:
Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle or individual libraries can be installed using circup.
Make sure that you have circup installed in your Python environment. Install it with the following command if necessary:
pip3 install circupWith circup installed and your CircuitPython device connected use the following command to install:
circup install cedargrove_rangeslicerOr the following command to update an existing version:
circup update"""Reads two potentiometer inputs and produces -1.0 to +1.0 normalized outputs, one with an inverted output range.""" import time import board import cedargrove_rangeslicer as rs from analogio import AnalogIn print("Two Normalized Outputs: RangeSlicer example 01") """Establish range_slicer instances, one for each analog potentiometer input. Input ranges are adjusted for unique potentiometer inaccuracies and noise. Slice size divides the output into 10 slices. Hysteresis factor is 25% of a slice.""" ctrl_0 = rs.Slicer(200, 65335, -1.0, +1.0, 0.2, 0.25) ctrl_1 = rs.Slicer(375, 65520, +1.0, -1.0, 0.2, 0.25) # establish analog inputs pot_0 = AnalogIn(board.A0) pot_1 = AnalogIn(board.A1) while True: # read potentiometer values control_0 = pot_0.value control_1 = pot_1.value # calculate output values and print (or plot in Mu) out_0 = ctrl_0.range_slicer(control_0) out_1 = ctrl_1.range_slicer(control_1) print( (control_0 / 65535, control_1 / 65535, out_0, out_1) ) time.sleep(0.1) # pause for 0.1 secondRangeSlicer CircuitPython Driver API Class Description
Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.

