Arduino Uno | magnetic_sensor_spi_example | Arduino IDE working & PlatformIO not working

Hi all,

I am starting with Simple FOC. My setup

  • MCU: Arduino Uno
  • Driver: SimpleFOCShield V2.0.1
  • Motor: iPower GM5208-12
  • Sensor: AS5048A via SPI

I just finished “Getting Started” (testing sensor, testing driver, open loop, closed loop) successfully.

Afterwards I followed the instructions for installing and using PlatformIO. The exact same (+ #include <Arduino.h>) magnetic_sensor_spi_example

#include <Arduino.h>

#include <SimpleFOC.h>

// MagneticSensorSPI(MagneticSensorSPIConfig_s config, int cs)
// config - SPI config
// cs - SPI chip select pin
// magnetic sensor instance - SPI
MagneticSensorSPI sensor = MagneticSensorSPI(AS5048_SPI, 10);
// alternative constructor (chipselsect, bit_resolution, angle_read_register, )
// MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);

void setup() {
// monitoring port

// initialise magnetic sensor hardware

Serial.println(“Sensor ready”);

void loop() {
// iterative function updating the sensor internal variables
// it is usually called in motor.loopFOC()
// this function reads the sensor hardware and
// has to be called before getAngle nad getVelocity
// display the angle and the angular velocity to the terminal

using the platform.ini

platform = atmelavr
board = uno
framework = arduino
lib_archive = false
monitor_speed = 115200
lib_deps = askuric/Simple FOC@^2.3.0

builds and uploads with two warnings

.pio\libdeps\uno\Simple FOC\src\common\foc_utils.cpp: In function ‘float _sqrtApprox(float)’:
.pio\libdeps\uno\Simple FOC\src\common\foc_utils.cpp:68:21: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
i = * ( long * ) &y;
.pio\libdeps\uno\Simple FOC\src\common\foc_utils.cpp:70:22: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
y = * ( float * ) &i;

and the serial monitor shows

0.00 0.00

for angle and velocity.

I tried different things like

  • including SPI and Wire in platform.ini
  • including #include <SPI.h> and #include<Wire.h> in main.cpp
    but did not got any sensor readings.

Can you please give me a hint what I am doing wrong?

There was a different setup/option for Platform IO, I believe @runger mentioned it before.


Another option than



That’s the correct option. Your Platformio file looks ok to me…

I wonder what is going on? Can you exclude a physical explanation like something got unplugged or loose cable or something?

One thing to try: can you define your own settings object, like a copy of AS5048A_SPI? And use that copy instead of the AS5048A_SPI one?

Hi Richard,

nice, the constructor

MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF)


THX from IBK

1 Like

I just have another question about PlatformIO:

when I use an example with commander and enter values in the terminal/ monitor I get strange feedback in the terminal, e.g. when entering “T5” at open_loop_velocity_example

Motor ready!
Set target velocity [rad/s]
Warn: \r detected!

While building an warning occured

src\main.cpp: In function ‘void setup()’:
src\main.cpp:52:47: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
command.add(‘T’, doTarget, “target velocity”);
src\main.cpp:53:44: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
command.add(‘L’, doLimit, “voltage limit”);

Is there any way I should change some monitor settings?

Just use

monitor_eol = LF

in your platformio.ini file.

Thank you! Including

monitor_eol = LF
monitor_echo = yes

it feels much more as I’m used to!

I had much difficulty debugging the mechanisms with arduino, they move too fast. PlatformIO appears advisable. Hopefully this thread will be understandable by others… such as myself at some future date. Right now it makes little sense unless you are already familiar with PlatformIO… I might even advocate for the more widespread adoption for something more suitable than arduino by simpleFOC at it’s core as this appears to be a common difficulty. I’m not too clear but it appears to be a geared to arduino? Not really, I guess, a lot of people seem to have good success with platformio.