Portenta x8, GPIO Setup and Toggle, Command Line Shell

Does anyone know how to set up GPIO's using windows shell.
Have Portenta x8 and Hat Carrier.
Was easy to setup ethernet IP etc for portenta and internet via laptop.
Was also easy to setup uart2 and even enable uart1 and uart3 and have them communicate with external devices.
But simplest thing of using GPIO's seems nearly impossible. How to do this in windows shell.
Goes from one issue to next. Seems like it is not possible but surly there must be a way. Setting up GPIO's would expect to be basic functionality to any electronics.
Wanted to use GPIO2 but seems stuck on watchdog and can't get it loose. Or then it looks like GPIO's not muxed correctly but seems not to be able to change that also.
ChatGPT seems to also go in circles and google gemini gave up and said it is not possible.

Hi @twinkitty. I think it is most common to do this via an Arduino sketch program running on the M4 core of the Portenta X8 board's STM32H747XI microcontroller. There is an example of doing that here:

https://docs.arduino.cc/tutorials/portenta-x8/user-manual/#working-with-arduino

You can communicate between the program running on the M4 core and applications running on the Linux machine via the RPC interface:

https://docs.arduino.cc/tutorials/portenta-x8/user-manual/#communication-between-arduino-and-linux

Thanks

You are welcome. I'm glad if I was able to be of assistance.

Regards, Per

Seem to come to the same sad conclusion that makes me still hope I am missing something.
Even using Arduino sketches it seems impossible to get something elementary as toggling any of the GPIO's to work. Only one able to toggle is the "BUILD_IN" LED.
Points again to GPIO mapping for all pins not correctly configured by the Arduino core for the Portenta X8 which you seem to have no control over and is part of the Linux image and how device tree etc is set up.
Are any one else having this issue and how to resolve ?
Surely a basic thing like setting up GPIO's etc can't be this difficult, must be missing something ?

You can control any of the pins in the same manner. For example, the pin marked "GPIO2" on the Portenta Hat Carrier silkscreen can be "blinked" with this sketch:

byte blinkPin = 2;

void setup() {
  pinMode(blinkPin, OUTPUT);
}

void loop() {
  digitalWrite(blinkPin, HIGH);
  delay(1000);
  digitalWrite(blinkPin, LOW);
  delay(1000);
}

Or if you prefer using the lower level pin names like what was used in the example in the user manual:

PinName blinkPin = PF_3;

void setup() {
 pinMode(blinkPin, OUTPUT);
}

void loop() {
 digitalWrite(blinkPin, HIGH);
 delay(1000);
 digitalWrite(blinkPin, LOW);
 delay(1000);
}

Thanks !!!
That works. Seems I need to read through the documentation carefully rather than using ChatGPT / Gemini

You are welcome.

I noticed that there is some very useful documentation in the Portenta Hat Carrier User Manual that is actually applicable to the Portenta X8 in general rather than exclusive to the Portenta Hat Carrier hardware. So make sure to look there in addition to the documentation for the X8:

https://docs.arduino.cc/tutorials/portenta-hat-carrier/user-manual/

If you would prefer to control the GPIO in other ways than via an Arduino sketch program running on the M4 core as I suggested, there are some tutorials for doing that.

Via the shell on the Linux machine:

https://docs.arduino.cc/tutorials/portenta-hat-carrier/user-manual/#hello-world-with-portenta-x8-shell

Via a Python script running on the Linux machine:

https://docs.arduino.cc/tutorials/portenta-hat-carrier/user-manual/#hello-world-using-linux-and-python-scripts

Another useful reference for the Portenta Hat Carrier is the pinout PDF that is downloadable from the links under the "DOWNLOADABLE RESOURCES" section of the documentation page:

https://docs.arduino.cc/hardware/portenta-hat-carrier/

Thanks again. This will keep me busy for a while.

So seems GPIO control is now easy via Arduino but controlling debug serial, uart1, uart2, uart3 not so straight forward. Any advise ?.

Trying to get simple uart test working. What the test does is flash the GPIO3 (connected to LED on hat carrier)
Flash 500ms if none of the uarts (when Tx, Rx connected with jumper) gives a loop back, flash 1s for 1 of the uarts loop backing, 2 seconds for 2 of the uarts and 3 seconds for all 3.

First problem is that the arduino debug serial shows no print messages etc.
GPIO3 definately works but always flashing 500ms indicating none of the uarts are working.

Also below code does not compile until I remove all uart2 and uart3 related functions like serial2 and serial3 etc, seems compiles but linker has issues.

// =======================================================
// Portenta X8 Multi-UART Test
// =======================================================

// This library is required for the Serial object on the M4 core.
#include <SerialRPC.h>

// --- Pin Definitions ---
// The GPIO pin to blink as an indicator
#define INDICATOR_PIN 3 

// --- Test Variables ---
const char* testString = "Loopback Test";

void setup() {
  // Initialize USB Serial for debugging
  Serial.begin(9600);
  
  // Set the indicator pin as an output
  pinMode(INDICATOR_PIN, OUTPUT);
  
  // Initialize the UART ports
  Serial.begin(9600); // For USB debug
  Serial1.begin(9600); // For external comms on D20/D21
  Serial2.begin(9600); // For external comms on D12/D13
  Serial3.begin(9600); // For external comms on D23/D25

  Serial.println("Setup Complete.");
  Serial.println("UART1, UART2, and UART3 initialized.");
  Serial.println("Please ensure loopback jumpers are in place for the test.");
  Serial.println("----------------------------------------");
}

void loop() {
  // --- Test Logic ---
  int successCount = 0;
  
  // Test UART1
  Serial.print("Testing UART1... ");
  Serial1.print(testString);
  delay(50);
  if (Serial1.available() > 0) {
    String receivedString = Serial1.readString();
    if (receivedString.equals(testString)) {
      successCount++;
      Serial.println("SUCCESS");
    } else {
      Serial.println("FAILURE (String Mismatch)");
    }
  } else {
    Serial.println("FAILURE (No Data)");
  }

  // Test UART2
  Serial.print("Testing UART2... ");
  Serial2.print(testString);
  delay(50);
  if (Serial2.available() > 0) {
    String receivedString = Serial2.readString();
    if (receivedString.equals(testString)) {
      successCount++;
      Serial.println("SUCCESS");
    } else {
      Serial.println("FAILURE (String Mismatch)");
    }
  } else {
    Serial.println("FAILURE (No Data)");
  }

  // Test UART3
  Serial.print("Testing UART3... ");
  Serial3.print(testString);
  delay(50);
  if (Serial3.available() > 0) {
    String receivedString = Serial3.readString();
    if (receivedString.equals(testString)) {
      successCount++;
      Serial.println("SUCCESS");
    } else {
      Serial.println("FAILURE (String Mismatch)");
    }
  } else {
    Serial.println("FAILURE (No Data)");
  }

  // --- Set Blink Interval Based on Success Count ---
  unsigned long blinkInterval; 
  
  if (successCount == 3) {
    blinkInterval = 3000;
  } else if (successCount == 2) {
    blinkInterval = 2000;
  } else if (successCount == 1) {
    blinkInterval = 1000;
  } else {
    blinkInterval = 500;
  }
  
  Serial.print("Status: ");
  Serial.print(successCount);
  Serial.println(" UARTs working.");
  Serial.println("----------------------------------------");
  
  // --- Toggle the indicator LED ---
  digitalWrite(INDICATOR_PIN, HIGH);
  delay(blinkInterval);
  digitalWrite(INDICATOR_PIN, LOW);
  delay(blinkInterval);
}

It is explained here:

https://docs.arduino.cc/tutorials/portenta-x8/python-arduino-data-exchange/#debugging-the-arduino-sketch

To check if the Arduino sketch is working correctly, you may want to read the messages from the Serial.println statements. You cannot currently read them directly in the serial monitor of the Arduino IDE. Instead, you can use a simple service called py-serialrpc, which listens for those messages and prints them to the console.

There are details about using py-serialrpc on the documentation page at the above link.

Ok thanks !

Have done this but seems to ignore shorting Tx1 and Rx1. Also, what about uart2 and uart3 ?

// =======================================================
// Portenta X8 UART1 Loopback Test
// =======================================================

// This library is required for the Serial object on the M4 core.
#include <SerialRPC.h>

// --- Pin Definitions ---
// The GPIO pin to blink as an indicator
#define INDICATOR_PIN 3 

// --- Test Variables ---
const char* testString = "Loopback Test";

void setup() {
  // Initialize USB Serial for debugging
  Serial.begin(9600);
  
  // Set the indicator pin as an output
  pinMode(INDICATOR_PIN, OUTPUT);
  
  // Initialize the UART ports
  Serial1.begin(9600); // For external comms on D20/D21

  // The following lines are commented out due to a known compiler bug on the Portenta X8:
  // Serial2.begin(9600);
  // Serial3.begin(9600);

  Serial.println("Setup Complete.");
  Serial.println("UART1 initialized.");
  Serial.println("Please ensure a loopback jumper is in place.");
  Serial.println("----------------------------------------");
}

void loop() {
  // --- Test Logic ---
  bool uart1Works = false;
  
  // Test UART1
  Serial.print("Testing UART1... ");
  Serial1.print(testString);
  delay(50);
  if (Serial1.available() > 0) {
    String receivedString = Serial1.readString();
    if (receivedString.equals(testString)) {
      uart1Works = true;
      Serial.println("SUCCESS");
    } else {
      Serial.println("FAILURE (String Mismatch)");
    }
  } else {
    Serial.println("FAILURE (No Data)");
  }

  // --- Set Blink Interval Based on Results ---
  unsigned long blinkInterval; 
  String statusMessage;
  
  if (uart1Works) {
    blinkInterval = 2000;
    statusMessage = "Status: SUCCESS on UART1.";
  } else {
    blinkInterval = 500;
    statusMessage = "Status: FAILURE on UART1.";
  }
  
  Serial.println(statusMessage);
  Serial.println("----------------------------------------");
  
  // --- Toggle the indicator LED ---
  digitalWrite(INDICATOR_PIN, HIGH);
  delay(blinkInterval);
  digitalWrite(INDICATOR_PIN, LOW);
  delay(blinkInterval);
}

Can see the serial output now but uart1 seems to not be working and GPIO3 LED also indicate so:

Hi, any comment on the above ?
I have also tried the "Hello World With Portenta X8 Shell" section as per:
https://docs.arduino.cc/tutorials/portenta-hat-carrier/user-manual/#hello-world-with-portenta-x8-shell
and it does not work. The GPIO3 LED does not come on:

Those pins are controlled by the Linux machine, not by the M4 core that runs the Arduino sketch. So you should use them via a Python script running on the Linux machine.

A very minimal demonstration:

  1. Run the adb shell to open a shell in the Linux machine.
  2. Run the following command to start a Python interactive mode terminal in a Docker container:
    sudo docker run --device --interactive -tty /dev/ttymxc2 python:3.9-alpine
    
  3. Run the following Python code in the terminal to install the pySerial package that allows you do do serial port communication from Python code:
    import sys, subprocess; subprocess.run([sys.executable, '-m', 'pip', 'install', 'pyserial'])
    
  4. Wait for the package installation to finish.
  5. Run the following Python code to import the pySerial package for use in subsequent code:
    import serial
    
  6. Run the following Python code to open the /dev/ttymxc2 port (UART2):
    ser = serial.Serial('/dev/ttymxc2', 9600)
    
  7. Run the following Python code to write to the port:
    ser.write(b'hello')
    
  8. When you are done, use the following to exit the container:
    exit()
    
fio@portenta-x8-2a0fca09dab6fad9:/var/rootdirs/home/fio$ sudo docker run --device --interactive -tty /dev/ttymxc2 python:3.9-alpine
Python 3.9.22 (main, Apr  9 2025, 23:23:57) 
[GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys, subprocess; subprocess.run([sys.executable, '-m', 'pip', 'install', 'pyserial'])
Collecting pyserial
  Downloading pyserial-3.5-py2.py3-none-any.whl (90 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 90.6/90.6 kB 684.9 kB/s eta 0:00:00
Installing collected packages: pyserial
Successfully installed pyserial-3.5
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

[notice] A new release of pip is available: 23.0.1 -> 25.2
[notice] To update, run: pip install --upgrade pip
CompletedProcess(args=['/usr/local/bin/python3', '-m', 'pip', 'install', 'pyserial'], returncode=0)
>>> import serial
>>> ser = serial.Serial('/dev/ttymxc2', 9600)
>>> ser.write(b'hello')
5
>>> exit()
fio@portenta-x8-2a0fca09dab6fad9:/var/rootdirs/home/fio$

I chose this approach as the most simple "smoke test" of using the serial port. Obviously this approach is not something you would ever use for a real project. You would instead create a Docker image that already has the dependencies such as pySerial installed, include the Python script file in that image, and configure that image to execute the script as its entry point. You can see an example of such an approach in this demo:

https://docs.arduino.cc/tutorials/portenta-x8/python-arduino-data-exchange/#python-on-the-x8

This also works with /dev/ttymxc3. However, when I tried it with /dev/ttymxc1, I found that I did not see any activity from the TX1 pin. I'm not sure why.

Thanks but as mentioned before from linux/python I was able to control and smoke test uart1, uart2 and uart3 successfully (you have to first do some stuff to enable uart1 and uart3).

Currently my struggle is that controlling GPIO's from linux commands does not work as per manual ?

The GPIO3 LED does not go on/off as per commands and stays always off.

Great news! Thanks for the update.

Unfortunately I don't have access to the Portenta Hat Carrier (I have all the other Portenta carriers except that one), but when I follow that tutorial I can see the voltage on the pin change as expected.

If you connect a voltmeter, or LED with appropriate current limiting resistor, to the pin marked "GPIO2" on the Portenta Hat Carrier's silkscreen, then run the same commands, except with /sys/class/gpio/gpio162 instead of /sys/class/gpio/gpio163, do you see the pin changing state as expected?

I have re-flashed the image to portenta x8. Though it also flashes the M4 carrying arduino sketches but seems it does not as arduino sketch stayed.
So I loaded a dummy Arduino sketch that does nothing except delay 1s in main loop.

Then tried in command line shell again setting up GPIO2 and GPIO3 several times and manually toggle 1/0. Tried this several times but nothing happens on GPIO2 or GPIO3

Are you then also saying that for the Portenta x8:

  1. From Arduino sketches can control GPIO's but not uart's ?
  2. From Arduino sketches can you control SPI, I2C etc etc ?
  3. From linux side and python, should be able to control everything, GPIO's, uart's, SPI, I2C etc. ?

Hi, have you got any feedback on this or anyone that can assist please ?

Can anyone assist ??
Seems these Portenta x8 boards are not developed properly and does not do what it says.