Close

pwm 'hello world' Arduino vs Mbed

A project log for STM32 BluePill Frameworks Evaluation

The STM32F103 BluePill, what's under the hoods of mbed, platformIO, Arduino,...

wassimWassim 04/15/2017 at 12:132 Comments

I expect the Arduino to be easier, was in a hurry to see some pwm output, so I started with it.

Note that I'm testing with PB4 which after checking the pinout (at least), does support the hardware pwm functionnality, I'm not interested in any soft pwm, and I also expect libraries to do the necessary configuration for me.

new Arduino Project

pio init --board bluepill_f103c8 --project-option="framework=arduino"
#include <Arduino.h>

void setup()
{
  pinMode(PC13, OUTPUT);
  pinMode(PB4, PWM);  
}

void loop()
{
  analogWrite(PB4,0);
  digitalWrite(PC13, HIGH);
  delay(200);
  analogWrite(PB4,255);
  digitalWrite(PC13, LOW);
  delay(1000);
}

Result = Fail !

Fail !It keeps hanging, no LED plinking, well, you could debug it, but no, welcome to Arduino, I do not have serial connected to my board (early dev phase) and USB is out of flash size.

After some googling I found another API for PWM :

  pwmWrite(PB4,0);
  pwmWrite(PB4,200);
this is a uint16 api, looks more promising

Result = Fail !

Is this a framework supposed to be easier for newbies ? Not even the simplest peripheral has a unified API, why do we say that the STM32 is supported by this framework then, if I have to manipulate registers to get my pwm to run ?

new Mbed Project

pio init --board bluepill_f103c8 --project-option="framework=mbed"
#include "mbed.h"

DigitalOut myled(PC_13);

PwmOut my_pwm(PB_4);

int main() 
{
    my_pwm.period_ms(20);
    while(1) 
    {
        my_pwm.write(0.5);
        myled = 1; // LED is ON
        wait(0.2); // 200 ms
        my_pwm.write(0.0);
        myled = 0; // LED is OFF
        wait(1.0); // 1 sec
    }
}

>pio run
>pio run -t upload
Result = Success

The LED is blinking, the quick measures show correct behavior of the PWM pio, did not check it with oscillo yet, but sounds correct.

Conclusion

Arduino (-1)

Mbed (+1)

PlatformIO (+1)

The context of this project is the STM32 Bluepill, Arduino might be better for 8bits or AVR, but for the Bluepill, mbed defeats arduino on its own field of excellence which is the simplicity of use for the very simple peripherals. PlatformIO here again shows how easy it is to compare different frameworks by still using the same commands.

Discussions

WestfW wrote 04/19/2017 at 09:41 point

The arduino failure is interesting.  It looks OK.  Could you try the same code with the Arduino IDE to figure out whether it's pio or the arduino code that is at fault?

  Are you sure? yes | no

Wassim wrote 04/19/2017 at 17:50 point

I tried the same code with Arduino IDE, I had rather another failure "test_pwm:6: error: 'PWM' was not declared in this scope" I'm using Arduino IDE 1.8.2

The stm32 package of PlatformIO find that definition of PWM in "C:\Users\User\.platformio\packages\framework-arduinoststm32\STM32F1\cores\maple\io.h"
So with PlatformIO I did not had to include anything more than , it compiles but it hangs, the Arduino IDE is at least more consistent, just do not compile.
I have found some examples on arduino forum about including "PWM.h" and setting frequency, but that would be beyond the 'hello world' simple example, rather a small project in itself.
If you have any suggestions or recommendation, or tested running pwm example, it is welcome, as I'd like to provide Arduino projects for the simple things in the future with the IoT boards I'm preparing.

I can also attach build logs in case you cannot reproduce it and interested to debug this further.

  Are you sure? yes | no