Analog pins on Portenta H7 malfunction when using Arduino_MachineControl library

I'm trying to read analog pins on an Arduino Portenta H7 using the Arduino_MachineControl library. So far, I've only been able to get 3 of the analog pins to work, and, they aren't the expected pin numbers. Reading pin 0 in the code returns the value for the physical pin A3. The same applies for pins 1 and 2 in the code, returning values for physical pins A2 and A1, respectively.

#include <Arduino_MachineControl.h>
using namespace machinecontrol;

void setup() {
Serial.begin(9600);
}

void loop() {
  for(int i = 0; i < 8; i++){
    if(analog_in.read(i)>0){
      Serial.print("Pin Requested = ");
      Serial.print(i);
      Serial.print(" Raw Data = ");
      Serial.println(analog_in.read(i));
    }
  }
  delay(1000);
}

Serial print from that code

07:26:56.817 -> Pin Requested = 0 Raw Data = 46890
07:26:56.817 -> Pin Requested = 1 Raw Data = 46638
07:26:56.817 -> Pin Requested = 2 Raw Data = 46555

Where Pin Requested 0, 1 and 2 each read the physical pins A3, A2 and A1 in that order. None of the other analog pins read.

I know my Portenta H7 is capable of reading the correct pins because of the analogRead example from the Arduino_PortentaBreakout library. Here is that example.

#include <Arduino_PortentaBreakout.h>

const byte ANALOG_PIN_NUMBER = 8;
const int ANALOG_RESOLUTION = 16;

// The access to each analog input is made using the peripheral silk name.
breakoutPin analogPin[] = {ANALOG_A0, ANALOG_A1, ANALOG_A2, ANALOG_A3, 
                           ANALOG_A4, ANALOG_A5, ANALOG_A6, ANALOG_A7};

void setup() {
  Serial.begin(9600);
  while (!Serial);

  // Max analog resolution is 16bits
  Breakout.analogReadResolution(ANALOG_RESOLUTION);
}

void loop() {
  // Loop through all the available inputs and print input value
  for(int i = 0; i < ANALOG_PIN_NUMBER; i++) {
    int w = Breakout.analogRead(analogPin[i]);
    Serial.print("A");
    Serial.print(i);
    Serial.print(": ");
    Serial.println(w);
    
    delay(1000);
  }
}

Returned values line up physical pins with pin number read correctly (analogPin[0] actually reads physical pin A0 and so forth). Here is the serial print from that code.

07:35:40.967 -> A0: 46877
07:35:41.950 -> A1: 46983
07:35:42.940 -> A2: 46829
07:35:43.974 -> A3: 46919
07:35:44.961 -> A4: 39192
07:35:45.950 -> A5: 39146
07:35:46.934 -> A6: 65535
07:35:47.963 -> A7: 25781

I tried using both the Arduino_MachineControl and Arduino_PortentaBreakout libraries, but, they won't compile together for me. I've tried Arduino IDE 1.8.19 and 2.1.1. I've also tried reinstalling the libraries. I think some of these errors might be relevant (concerning pin descriptions), but, I'm not at a level of understanding to know how to proceed. I'd like to keep using the Arduino_MachineControl library.

#include <Arduino_MachineControl.h>
#include <Arduino_PortentaBreakout.h>
void setup() {}
void loop() {}

Compiling Errors (REDACTED)

Arduino: 1.8.19 (Windows 10), Board: "Arduino Portenta H7, Main Core, 1MB M7 + 1MB M4, None"

In file included from \Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\libraries\PDM\src/PDM.h:23:0,

                 from \libraries\Arduino_PortentaBreakout\src/Arduino_PortentaBreakout.h:34,

\Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/pinDefinitions.h:8:8: error: redefinition of 'struct _PinDescription'

 struct _PinDescription

        ^~~~~~~~~~~~~~~

In file included from \libraries\Arduino_MachineControl\src/Arduino_MachineControl.h:13:0,

\Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/pinDefinitions.h:8:8: note: previous definition of 'struct _PinDescription'

 struct _PinDescription

        ^~~~~~~~~~~~~~~

In file included from \Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\libraries\PDM\src/PDM.h:23:0,

                 from \libraries\Arduino_PortentaBreakout\src/Arduino_PortentaBreakout.h:34,

\Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/pinDefinitions.h:16:8: error: redefinition of 'struct _AnalogPinDescription'

 struct _AnalogPinDescription

        ^~~~~~~~~~~~~~~~~~~~~

In file included from \libraries\Arduino_MachineControl\src/Arduino_MachineControl.h:13:0,

\Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/pinDefinitions.h:16:8: note: previous definition of 'struct _AnalogPinDescription'

 struct _AnalogPinDescription

        ^~~~~~~~~~~~~~~~~~~~~

In file included from \Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/mbed/targets/TARGET_STM/TARGET_STM32H7/STM32Cube_FW/STM32H7xx_HAL_Driver/stm32h7xx_hal_rcc.h:29:0,

                 from \Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/mbed/targets/TARGET_STM/TARGET_STM32H7/STM32Cube_FW/stm32h7xx_hal_conf.h:398,

                 from \Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/mbed/targets/TARGET_STM/TARGET_STM32H7/STM32Cube_FW/STM32H7xx_HAL_Driver/stm32h7xx_hal.h:30,

                 from \Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/mbed/targets/TARGET_STM/TARGET_STM32H7/STM32Cube_FW/CMSIS/stm32h7xx.h:223,

                 from\Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/mbed/targets/TARGET_STM/TARGET_STM32H7/cmsis.h:22,

                 from\Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/mbed/targets/TARGET_STM/PinNamesTypes.h:33,

                 from l\Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\variants\PORTENTA_H7_M7/pinmode_arduino.h:24,

                 from \Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/Arduino.h:26,

                 from sketch\sketch_jul20b.ino.cpp:1:

\libraries\Arduino_PortentaBreakout\src/utility/Analog/Analog.h: In constructor 'BreakoutPWM::HRTIMPWMClass::HRTIMPWMClass()':

\Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/mbed/targets/TARGET_STM/TARGET_STM32H7/STM32Cube_FW/STM32H7xx_HAL_Driver/stm32h7xx_hal_rcc.h:2318:48: warning: conversion to void will not access object of type 'volatile uint32_t {aka volatile long unsigned int}'

                                         UNUSED(tmpreg); \

\Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/mbed/targets/TARGET_STM/TARGET_STM32H7/STM32Cube_FW/STM32H7xx_HAL_Driver/stm32h7xx_hal_def.h:70:27: note: in definition of macro 'UNUSED'

 #define UNUSED(x) ((void)(x))

                           ^

\libraries\Arduino_PortentaBreakout\src/utility/Analog/Analog.h:32:9: note: in expansion of macro '__HAL_RCC_HRTIM1_CLK_ENABLE'

         __HAL_RCC_HRTIM1_CLK_ENABLE();

         ^

\Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/mbed/targets/TARGET_STM/TARGET_STM32H7/STM32Cube_FW/STM32H7xx_HAL_Driver/stm32h7xx_hal_rcc.h:1478:48: warning: conversion to void will not access object of type 'volatile uint32_t {aka volatile long unsigned int}'

                                         UNUSED(tmpreg); \

\Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/mbed/targets/TARGET_STM/TARGET_STM32H7/STM32Cube_FW/STM32H7xx_HAL_Driver/stm32h7xx_hal_def.h:70:27: note: in definition of macro 'UNUSED'

 #define UNUSED(x) ((void)(x))

                           ^

\libraries\Arduino_PortentaBreakout\src/utility/Analog/Analog.h:35:9: note: in expansion of macro '__HAL_RCC_GPIOG_CLK_ENABLE'

         __HAL_RCC_GPIOG_CLK_ENABLE();

         ^

exit status 1

Error compiling for board Arduino Portenta H7.

In file included from \Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\libraries\PDM\src/PDM.h:23:0,

                 from \libraries\Arduino_PortentaBreakout\src/Arduino_PortentaBreakout.h:34,

\Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/pinDefinitions.h:8:8: error: redefinition of 'struct _PinDescription'

 struct _PinDescription

        ^~~~~~~~~~~~~~~

In file included from \libraries\Arduino_MachineControl\src/Arduino_MachineControl.h:13:0,

                 from \Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/pinDefinitions.h:8:8: note: previous definition of 'struct _PinDescription'

 struct _PinDescription

        ^~~~~~~~~~~~~~~

In file included from \Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\libraries\PDM\src/PDM.h:23:0,

                 from \libraries\Arduino_PortentaBreakout\src/Arduino_PortentaBreakout.h:34,

l\Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/pinDefinitions.h:16:8: error: redefinition of 'struct _AnalogPinDescription'

 struct _AnalogPinDescription

        ^~~~~~~~~~~~~~~~~~~~~

In file included from \libraries\Arduino_MachineControl\src/Arduino_MachineControl.h:13:0,

\Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/pinDefinitions.h:16:8: note: previous definition of 'struct _AnalogPinDescription'

 struct _AnalogPinDescription

        ^~~~~~~~~~~~~~~~~~~~~

In file included from \Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/mbed/targets/TARGET_STM/TARGET_STM32H7/STM32Cube_FW/STM32H7xx_HAL_Driver/stm32h7xx_hal_rcc.h:29:0,

                 from \Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/mbed/targets/TARGET_STM/TARGET_STM32H7/STM32Cube_FW/stm32h7xx_hal_conf.h:398,

                 from \Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/mbed/targets/TARGET_STM/TARGET_STM32H7/STM32Cube_FW/STM32H7xx_HAL_Driver/stm32h7xx_hal.h:30,

                 from \Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/mbed/targets/TARGET_STM/TARGET_STM32H7/STM32Cube_FW/CMSIS/stm32h7xx.h:223,

                 from \Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/mbed/targets/TARGET_STM/TARGET_STM32H7/cmsis.h:22,

                 from \Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/mbed/targets/TARGET_STM/PinNamesTypes.h:33,

                 from \Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\variants\PORTENTA_H7_M7/pinmode_arduino.h:24,

                 from \Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/Arduino.h:26,

\libraries\Arduino_PortentaBreakout\src/utility/Analog/Analog.h: In constructor 'BreakoutPWM::HRTIMPWMClass::HRTIMPWMClass()':

\Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/mbed/targets/TARGET_STM/TARGET_STM32H7/STM32Cube_FW/STM32H7xx_HAL_Driver/stm32h7xx_hal_rcc.h:2318:48: warning: conversion to void will not access object of type 'volatile uint32_t {aka volatile long unsigned int}'

                                         UNUSED(tmpreg); \

l\Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/mbed/targets/TARGET_STM/TARGET_STM32H7/STM32Cube_FW/STM32H7xx_HAL_Driver/stm32h7xx_hal_def.h:70:27: note: in definition of macro 'UNUSED'

 #define UNUSED(x) ((void)(x))

                           ^

\libraries\Arduino_PortentaBreakout\src/utility/Analog/Analog.h:32:9: note: in expansion of macro '__HAL_RCC_HRTIM1_CLK_ENABLE'

         __HAL_RCC_HRTIM1_CLK_ENABLE();

         ^

\Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/mbed/targets/TARGET_STM/TARGET_STM32H7/STM32Cube_FW/STM32H7xx_HAL_Driver/stm32h7xx_hal_rcc.h:1478:48: warning: conversion to void will not access object of type 'volatile uint32_t {aka volatile long unsigned int}'

                                         UNUSED(tmpreg); \

\Arduino15\packages\arduino\hardware\mbed_portenta\4.0.2\cores\arduino/mbed/targets/TARGET_STM/TARGET_STM32H7/STM32Cube_FW/STM32H7xx_HAL_Driver/stm32h7xx_hal_def.h:70:27: note: in definition of macro 'UNUSED'

 #define UNUSED(x) ((void)(x))

                           ^

\libraries\Arduino_PortentaBreakout\src/utility/Analog/Analog.h:35:9: note: in expansion of macro '__HAL_RCC_GPIOG_CLK_ENABLE'

         __HAL_RCC_GPIOG_CLK_ENABLE();

         ^

exit status 1

I have no experience with regards to your board or the machine control library. However, from https://github.com/arduino-libraries/Arduino_MachineControl/blob/master/src/Arduino_MachineControl.h

	 /**
	 * read the sampled voltage from the selected channel
	 * @param  channel integer for selecting the analog input (0, 1 or 2)
	 * @return the analog value between 0.0 and 1.0 normalized to a 16-bit value (uint16_t)
	 */
	uint16_t read(int channel) {
        uint16_t value = 0;
        switch (channel) {
            case 0:
                value = in_0.read_u16();
                break;
            case 1:
                value =  in_1.read_u16();
                break;
            case 2:
                value =  in_2.read_u16();
                break;
            default:
                break;
            }
        return value;
    }

So only 3 analogue pins are supported.

Further you will have to check if A0 maps to channel 0 etc. Based on your results, that is not the case.

1 Like

Thanks for the help! Your answer pointed me in the right direction. I went into the Arduino_MachineControl.h file and made some changes. Now it works!

Added cases 3-7

public:

	 /**
	 * read the sampled voltage from the selected channel
	 * @param  channel integer for selecting the analog input (0, 1 or 2)
	 * @return the analog value between 0.0 and 1.0 normalized to a 16-bit value (uint16_t)
	 */
	uint16_t read(int channel) {
        uint16_t value = 0;
        switch (channel) {
            case 0:
                value = in_0.read_u16();
                break;
            case 1:
                value =  in_1.read_u16();
                break;
            case 2:
                value =  in_2.read_u16();
                break;
            case 3:
                value = in_3.read_u16();
                break;
            case 4:
                value =  in_4.read_u16();
                break;
            case 5:
                value =  in_5.read_u16();
                break;
            case 6:
                value = in_6.read_u16();
                break;
            case 7:
                value =  in_7.read_u16();
                break;
            default:
                break;
            }
        return value;
    }

Added cases 3-7

	mbed::AnalogIn& operator[](int index) {
		switch (index) {
			case 0:
				return in_0;
			case 1:
				return in_1;
			case 2:
				return in_2;
			case 3:
				return in_3;
			case 4:
				return in_4;
			case 5:
				return in_5;
			case 6:
				return in_6;
			case 7:
				return in_7;
		}
	}

Addes 3-7
Note for others: I had to change the addresses shown to those found in the H7 datasheet.

private:
	mbed::AnalogIn in_0 = mbed::AnalogIn(PA_0C);
	mbed::AnalogIn in_1 = mbed::AnalogIn(PA_1C);
	mbed::AnalogIn in_2 = mbed::AnalogIn(PC_2C);
	mbed::AnalogIn in_3 = mbed::AnalogIn(PC_3C);
	mbed::AnalogIn in_4 = mbed::AnalogIn(PC_2);
	mbed::AnalogIn in_5 = mbed::AnalogIn(PC_3);
	mbed::AnalogIn in_6 = mbed::AnalogIn(PA_4);
	mbed::AnalogIn in_7 = mbed::AnalogIn(PA_6);
};

Output, values are as expected.

09:39:03.499 -> Pin Requested = 0 Raw Data = 47259
09:39:03.499 -> Pin Requested = 1 Raw Data = 46992
09:39:03.499 -> Pin Requested = 2 Raw Data = 46831
09:39:03.499 -> Pin Requested = 3 Raw Data = 224
09:39:03.499 -> Pin Requested = 4 Raw Data = 36516
09:39:03.499 -> Pin Requested = 5 Raw Data = 41064
09:39:03.499 -> Pin Requested = 6 Raw Data = 65535
09:39:03.499 -> Pin Requested = 7 Raw Data = 22465

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.