Hi Everyone,
I am trying to use a quadrature incremental encoder with the Portenta H7. However I notice that the library Encoder.h does not support the Portenta H7 architecture.
As per this post, it does mention that the NewEncoder library should support the STM32 architecture of Portenta H7. However I always get the error
#error "Interrupts are unknown for this board. Please specify in interrupt_pins.h"
So I did update the interrupt_pins.h with this
#elif defined(STM32H7)
#define ENCODER_AR_USE_INTERRUPTS
#define digitalPinToInterrupt(p) (p) // Direct mapping for Portenta H7
#define CORE_NUM_INTERRUPT NUM_DIGITAL_PINS // Set the number of interrupts to the number of pins on the board
However I get another error after this
In file included from C:\Users\ymalhotra\AppData\Local\Temp\.arduinoIDE-unsaved202523-24016-3zfyi9.jzfnm\sketch_mar3a\sketch_mar3a.ino:2:0:
c:\Users\ymalhotra\Documents\Arduino\libraries\NewEncoder/NewEncoder.h:90:11: error: 'IO_REG_TYPE' does not name a type
volatile IO_REG_TYPE *_aPin_register;
^~~~~~~~~~~
c:\Users\ymalhotra\Documents\Arduino\libraries\NewEncoder/NewEncoder.h:91:11: error: 'IO_REG_TYPE' does not name a type
volatile IO_REG_TYPE *_bPin_register;
^~~~~~~~~~~
c:\Users\ymalhotra\Documents\Arduino\libraries\NewEncoder/NewEncoder.h:92:11: error: 'IO_REG_TYPE' does not name a type
volatile IO_REG_TYPE _aPin_bitmask;
^~~~~~~~~~~
c:\Users\ymalhotra\Documents\Arduino\libraries\NewEncoder/NewEncoder.h:93:11: error: 'IO_REG_TYPE' does not name a type
volatile IO_REG_TYPE _bPin_bitmask;
How can I make use of either of the encoder libraries to read the encoder using Portenta H7. For your reference I am attaching the test code I am using for the NewEncoder library
#define STM32H7 // Force the macro definition
#include <NewEncoder.h>
// Define encoder pins (modify as needed)
#define ENC_A_PIN D12 // Encoder A pin (verify this is interrupt-capable)
#define ENC_B_PIN D14 // Encoder B pin (verify this is interrupt-capable)
// Encoder instance
NewEncoder encoder(ENC_A_PIN, ENC_B_PIN, -100, 100, 0, FULL_PULSE);
// Previous position tracking
int16_t prevPosition = 0;
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("Encoder Test - Arduino Portenta H7");
// Initialize encoder
if (!encoder.begin()) {
Serial.println("Encoder failed to initialize!");
while (1);
}
Serial.println("Encoder initialized successfully.");
}
void loop() {
NewEncoder::EncoderState state;
// Read encoder state
if (encoder.getState(state)) {
// If the state has changed, print the new position
if (state.currentValue != prevPosition) {
Serial.print("Encoder Position: ");
Serial.println(state.currentValue);
prevPosition = state.currentValue;
}
}
delay(50); // Adjust as needed
}
Your help in this regard is highly appreciated.
Thanks