Skip to content

A library for setting up magnetic rotary encoding in C/C++ & MicroPython, The library also features a haptics controller which allows you to connect and control a vibration motor as a step count haptic feedback.

License

Notifications You must be signed in to change notification settings

ahmsville/Magnetic_rotary_encoding

Repository files navigation

Magnetic_Rotary_Encoding

Learn more about this project -

BUILD INSTRUCTIONS

Hardware Options

20221123_121114_1_2

1. Magnetic Rotary Encoder

This is standalone encoder made to connect to a seperate microcontroller like an Arduino board, this version has a base resolution of 72, extendable up to 72000 steps per revolution. magnetic rotary encoder pinout

Features:

  • Capacitive touch / button input
  • Addressable Led ring (18 LEDs)

Supported Boards / Platform:

  • All Arduino Boards
  • Raspberry Pi Pico C/C++ SDK
  • All Micropython-Supported Boards

2. Magnetic Rotary Encoder MINI

This is standalone encoder similar to the first option, only with a small form factor, this version has a base resolution of 36, extendable up to 36000 steps per revolution. magnetic rotary encoder mini pinout

Features:

  • Capacitive touch / button input
  • Addressable Led ring (12 LEDs)

Supported Boards / Platform:

  • All Arduino Boards
  • Raspberry Pi Pico C/C++ SDK
  • All Micropython-Supported Boards

This is a full fledged development board combined with the magnetic rotary encoder, its built with the Raspberry Pi RP2040 microcontroller chip with a base resolution of 36, extendable up to 36000 steps per revolution. pico dev m3 pinout

Features:

  • RP2040 chip, Dual-core Arm Cortex-M0+ processor at 133 MHz
  • 264kB on-chip SRAM
  • 128Mb on-board QSPI flash
  • 19 multifunction GPIO pins, with 2 analog inputs
  • 2 UARTs
  • 2 SPI controllers
  • 2 I2C controllers
  • 16 PWM channels
  • USB 1.1 controller with host and device support
  • 8 PIO state machines
  • USB type C
  • Lipo charging
  • onboard Button input
  • Addressable Led ring (18 LEDs)

Supported Platform:

  • RP2040 C/C++ SDK
  • Micropython

How to connect and use the Ahmsville labs magnetic rotary encoder.

There are three ways to connect the magnetic rotary encoder, all with their benefits and draw backs.

1. Analog only Detection:

in this mode the library uses just the signals from the hall effect sensors to make sense of the rotation of the encoder. the benefit of this mode is that you only need two analog pins to do all the rotation encoding, also been analog means you can use the libraries built in interpolation mode for an extremely high and granular resolution. The drawback is, this mode requires constant polling of the sensors which is not ideal for a complex system, also if the board does not have analog pins or if your setup cannot spare any of them, then you cannot use this mode.

Connection

To use this mode, simply connect ENC-1 and ENC-2 to two analog input pins, you may also connect the LED pin to any available pin on the board. VCC should be connected to the same voltage level used by the microcontroller on the boards, so power the encoder with 5v if the board uses 5v, and power it with 3.3v it the board uses 3.3v, you should offcus also connect the GND pin.

Asset 1

Code

To test the example code, all you have to do is activate the analog only mode and change the pin assignment according to your connections. For the mode, you have the option to choose continuous rotation detection which returns a continuous count in either direction, or absolute positioning, which returns a value within the encoder’s base resolution, you can also choose the extended interpolated resolution mode for a super-duper high resolution.

C/C++

#define ENCMODE 1 // BASE RESOLUTION ANALOG ONLY
//#define ENCMODE 2 // BASE RESOLUTION ANALOG ONLY ABSOLUTE POSITIONING
//#define ENCMODE 3 // EXTENDED RESOLUTION ANALOG ONLY
//#define ENCMODE 4 // BASE RESOLUTION ANALOG AND DIGITAL
//#define ENCMODE 5 // BASE RESOLUTION ANALOG AND DIGITAL ABSOLUTE POSITIONING
//#define ENCMODE 6 // EXTENDED RESOLUTION ANALOG AND DIGITAL
//#define ENCMODE 7 // BASE RESOLUTION DIGITAL ONLY
//#define ENCMODE 8 // BASE RESOLUTION DIGITAL ONLY ABSOLUTE POSITIONING
#define E1_ADC_PIN 26 // ADC PIN for hall sensor 1
#define E2_ADC_PIN 27 // ADC PIN for hall sensor 2

MicroPython

ENCMODE = 1 # BASE RESOLUTION ANALOG ONLY
#ENCMODE  = 2 # BASE RESOLUTION ANALOG ONLY ABSOLUTE POSITIONING
#ENCMODE  = 3 # EXTENDED RESOLUTION ANALOG ONLY
#ENCMODE  = 4 # BASE RESOLUTION ANALOG AND DIGITAL
#ENCMODE  = 5 # BASE RESOLUTION ANALOG AND DIGITAL ABSOLUTE POSITIONING
#ENCMODE  = 6 # EXTENDED RESOLUTION ANALOG AND DIGITAL
#ENCMODE  = 7 # BASE RESOLUTION DIGITAL ONLY
#ENCMODE  = 8 # BASE RESOLUTION DIGITAL ONLY ABSOLUTE POSITIONING
E1_ADC_PIN = 26  # ADC PIN for hall sensor 1
E2_ADC_PIN = 27  # ADC PIN for hall sensor 2

2. Analog and Digital Detection:

in this mode the library uses the signals from the hall effect sensors together with two additional digital interrupt enabled input pins. This mode has all the same features and benefits as the analog only detection mode without the need for constant polling of the hall effect sensors, this is extremely usefull for complex systems, as the interrupt pins notify the code of when a rotation occurs, so constant polling is not needed, this mode is in fact the mode used on the Ahmsville Dial V2. Please note that the code will return to constant polling if you select the Extended Resolution mode, since this process requires interpolation between the encoder’s steps.

Connection

To use this mode, simply connect ENC-1 and ENC-2 to two analog input pins, and E1-INT1 and E2-INT1 to two interrupt enabled pins on the board, you may also connect the LED pin to any available pins on the board. VCC should be connected to the same voltage level used by the microcontroller on the boards, so power the encoder with 5v if the board uses 5v, and power it with 3.3v it the board uses 3.3v, you should offcus also connect the GND pin.

Asset 2

Depending on your encoder version, you may also need to bridge the config pads on the PCB to use this mode. ASDADASDASDASDAS

Code

To test the code, all you have to do is activate the analog and digital mode and also change the pin assignment according to your connections. For this mode, you also have the option to choose continuous rotation detection which returns a continuous count in either direction, or absolute positioning, which returns a value within the encoder’s base resolution, you can also choose the extended interpolated resolution mode for a super-duper high resolution.

C/C++

//#define ENCMODE 1 // BASE RESOLUTION ANALOG ONLY
//#define ENCMODE 2 // BASE RESOLUTION ANALOG ONLY ABSOLUTE POSITIONING
//#define ENCMODE 3 // EXTENDED RESOLUTION ANALOG ONLY
#define ENCMODE 4 // BASE RESOLUTION ANALOG AND DIGITAL
//#define ENCMODE 5 // BASE RESOLUTION ANALOG AND DIGITAL ABSOLUTE POSITIONING
//#define ENCMODE 6 // EXTENDED RESOLUTION ANALOG AND DIGITAL
//#define ENCMODE 7 // BASE RESOLUTION DIGITAL ONLY
//#define ENCMODE 8 // BASE RESOLUTION DIGITAL ONLY ABSOLUTE POSITIONING
#define E1_INT1 16 // hall sensor 1 interrupt pin 1
#define E2_INT1 18 // hall sensor 2 interrupt pin 1

#define E1_ADC_PIN 26 // ADC PIN for hall sensor 1
#define E2_ADC_PIN 27 // ADC PIN for hall sensor 2

MicroPython

#ENCMODE = 1 # BASE RESOLUTION ANALOG ONLY
#ENCMODE  = 2 # BASE RESOLUTION ANALOG ONLY ABSOLUTE POSITIONING
#ENCMODE  = 3 # EXTENDED RESOLUTION ANALOG ONLY
ENCMODE  = 4 # BASE RESOLUTION ANALOG AND DIGITAL
#ENCMODE  = 5 # BASE RESOLUTION ANALOG AND DIGITAL ABSOLUTE POSITIONING
#ENCMODE  = 6 # EXTENDED RESOLUTION ANALOG AND DIGITAL
#ENCMODE  = 7 # BASE RESOLUTION DIGITAL ONLY
#ENCMODE  = 8 # BASE RESOLUTION DIGITAL ONLY ABSOLUTE POSITIONING
E1_INT1 = 16 # hall sensor 1 interrupt pin 1
E2_INT1 = 18 # hall sensor 2 interrupt pin 1

E1_ADC_PIN = 26 # ADC PIN for hall sensor 1
E2_ADC_PIN = 27 # ADC PIN for hall sensor 2

3. Digital only Detection:

in this mode the library uses only digital interrupt enabled input pins; four of them. This mode is similar in operation to traditional rotary switches, the analog signals from the hall effect sensors have been converted to digital signals using comparators, so no analog input pins are required. This mode was developed for systems that don’t have or cannot spare analog input pins, its also the fastest mode and requires zero polling, which is why I used it to encode the Pick N Place Wheel. Because this mode uses digital signals alone, you cannot use the interpolated extended resolution mode that is available in the previous modes.

Connection

To use this mode, simply connect E1-INT1, E1-INT2, E2-INT1 and E2-INT2 to four interrupt enabled pins on the board, you may also connect the LED pin to any available pins on the board. VCC should be connected to the same voltage level used by the microcontroller on the boards, so power the encoder with 5v if the board uses 5v, and power it with 3.3v it the board uses 3.3v, also connect the GND pin.

Asset 3

Code

To test the code, all you have to do is activate the digital only mode and also change the pin assignments according to your connections. For this mode, you have the option to choose continuous rotation detection which returns a continuous count in either direction, or absolute positioning, which returns a value within the encoder’s base resolution.

C/C++

//#define ENCMODE 1 // BASE RESOLUTION ANALOG ONLY
//#define ENCMODE 2 // BASE RESOLUTION ANALOG ONLY ABSOLUTE POSITIONING
//#define ENCMODE 3 // EXTENDED RESOLUTION ANALOG ONLY
//#define ENCMODE 4 // BASE RESOLUTION ANALOG AND DIGITAL
//#define ENCMODE 5 // BASE RESOLUTION ANALOG AND DIGITAL ABSOLUTE POSITIONING
//#define ENCMODE 6 // EXTENDED RESOLUTION ANALOG AND DIGITAL
#define ENCMODE 7 // BASE RESOLUTION DIGITAL ONLY
//#define ENCMODE 8 // BASE RESOLUTION DIGITAL ONLY ABSOLUTE POSITIONING
#define E1_INT1 16 // hall sensor 1 interrupt pin 1
#define E1_INT2 17 // hall sensor 1 interrupt pin 2
#define E2_INT1 18 // hall sensor 2 interrupt pin 1
#define E2_INT2 19 // hall sensor 2 interrupt pin 2

MicroPython

#ENCMODE = 1 # BASE RESOLUTION ANALOG ONLY
#ENCMODE  = 2 # BASE RESOLUTION ANALOG ONLY ABSOLUTE POSITIONING
#ENCMODE  = 3 # EXTENDED RESOLUTION ANALOG ONLY
ENCMODE  = 4 # BASE RESOLUTION ANALOG AND DIGITAL
#ENCMODE  = 5 # BASE RESOLUTION ANALOG AND DIGITAL ABSOLUTE POSITIONING
#ENCMODE  = 6 # EXTENDED RESOLUTION ANALOG AND DIGITAL
#ENCMODE  = 7 # BASE RESOLUTION DIGITAL ONLY
#ENCMODE  = 8 # BASE RESOLUTION DIGITAL ONLY ABSOLUTE POSITIONING
E1_INT1 = 16  # hall sensor 1 interrupt pin 1
E1_INT2 = 17  # hall sensor 1 interrupt pin 2
E2_INT1 = 18  # hall sensor 2 interrupt pin 1
E2_INT2 = 19  # hall sensor 2 interrupt pin 2

Complimentary Input on the Encoder

You have two options when it comes to complimentary input on the encoder, you can choose to use capacitive touch or a normal push button, both the capacitive touch and the push button will be included with the kit if you buy it from my store. To use capacitive touch, you will need an Arduino board, if you use other development boards like the ESP32 or Raspberry PI Pico, you will have to use the push button.

SAGSAFFF zczczczcc

Capacitive touch

To use the capacitive touch on an Arduino, you must have assembled the encoder according to the instructions, so that you can simply connect CT1 and CT2 to two available pins and then update the pin assignment in the example code accordingly.

C/C++

#define CT1 5 // CAPACITIVE TOUCH (CT1)
#define CT2 6 // CAPACITIVE TOUCH (CT2)

Push button

To use the push button, you should solder the included button to the encoder board and then connect only CT1 to an available pin on the development board.

C/C++

#define CT1 15 // PUSH BUTTON (CT1)

MicroPython

CT1 = 15 # PUSH BUTTON (CT1)

About

A library for setting up magnetic rotary encoding in C/C++ & MicroPython, The library also features a haptics controller which allows you to connect and control a vibration motor as a step count haptic feedback.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published