I was wondering if it is possible to use the analog outputs from the Arduino Portenta Machine Control to create a PWM signal. I was fiddling around with the functions analog_out.write( int pin, int voltage) and analog_out.period_ms( int pin, int period), however this does not yield any result as a PWM signal.
Would there be anyone who knows how to create a PWM signal on the Arduino Portenta Machine Control? Or how to use the internal timers to setup a PWM signal based on the timers.
My understanding:
There are not any analog output pins (all is digital).
You have analog input pins which go to an ADC.
PWM can create an "analog" signal - but just with external components (an RC circuit to "integrate", filter, the digital pulse output).
Before you can use a PWM (as a device and output) - you had to setup and initialize a PWM device. Just calling functions to set a "value" will not work if PWM device is not configured.
Sorry, not very helpful: I am not an expert with PWM on Portenta H7 and Arduino/mbed. I have PWM running on a similar STM MCU but on a different platform.
I guess: you miss the "creation" of the PWM device (and don't forget your external components, the RC circuit).
You can try my Portenta_H7_PWM library, which is tested on Portenta_H7 board. I don't have Portenta Machine Control board to test, but I guess you can use the library, with some pin mods, if necessary.
We have now switches from the Portenta Machine Control to the Portenta H7 with breakout board. This works with PWM. When looking at the bottom of the Portenta Machine Control, it looks like there is more circuitry which uses the PWM pins for analog outputs, controlling the voltage.
Generating PWM signals on the Portenta Machine Control is tricky, especially with the limitations of the analog_out functions. Here's what I've learned:
Using analog_out:
While it seems like a good starting point, analog_out is actually meant for generating analog voltages, not true PWM signals. Setting the period and writing a voltage won't control the pulse width.
Alternative approaches:
Timer-based PWM: This is the way to go for true PWM control. The Portenta has dedicated timers that can be configured for PWM output. You'll need to access the timer registers directly, which can be a bit low-level, but there are libraries like Portenta_H7_TimerInterrupt and Portenta_H7_PWM that can help simplify things.
I2C PWM expanders: If you're not comfortable with low-level programming, you can use I2C-based PWM expanders like the PCA9683. These chips take care of the timer configuration and provide multiple PWM outputs you can control through I2C communication.