Arduino Cli VS Arduino IDE compile results - HC-SR04

Hi There!

I’m a developer that now turned to arduino as a way scape the corporate stress life…

Being using the Arduino IDE for a while, running a few small projects, and wanted to try the Command line tool in order to have better productivity when coding. Haven’t done much cpp in a while, so this could possibility be just another case of super simple typos that we can’t find on our own.

All the code is available at: arduino/water_sensor at main · victorhg/arduino · GitHub

Scenario:

  • Ultrasonic sensor to measure distance, using HC-SR04 and oled display
  • Sensor is working fine, measuring proper distances, without a problem, when I’m compiling and uploading to the same arduino using the Arduino IDE

Problem:

  • Compiling and Uploading using Arduino CLI will lead to HC sensor deliver different measurements on the same code I’m compiling and running with Arduino IDE
  • Same code, different results

Arch:

  • SparkFun Pro Micro 5V 16Hmz

Attached:

  • Print from IDE with configuration that I can run a distance measurement test and it gives me proper results (on a scale, given general inaccuracies of the sensor)

Arduino-cli information:

sketch.json:

{
  "cpu": {
    "fqbn": "SparkFun:avr:promicro",
    "cpu": "16MHzatmega32U4",
    "name": "Arduino micro",
    "port": "/dev/cu.usbmodem141101"
  }
}

Build Command:

$> arduino-cli compile --upload -p /dev/cu.usbmodem141101 -v

Same results when running command line with all parameters

The code I’m running:

#include "Ultrasonic.h"
#include "water_display.h"

#define echoPin 9 
#define trigPin 7 
Ultrasonic ultrasonic(trigPin, echoPin);

float distance; 
WaterDisplay waterDisplay;


void setup()
{
  waterDisplay = WaterDisplay();
  Serial.begin(9600); //This pipes to the serial monitor
 
  Serial.println("Initialize Display");
  waterDisplay.startDisplay();

}

void loop() {

  distance = ultrasonic.read();

  Serial.println(distance);

 
  delay(1000);
}

The sensor would present a increasing distance when compared to my control sample (Running the same code with Arduino IDE) - the bigger the distance, the bigger the discrepancy - linear progression.

Control: Sensor: 10cm
Arduino-cli: Sensor: 15cm

Control: Sensor: 20cm
Arduino-cli: Sensor: 30cm

What I have tried:

  • Connected to the sensor and run vanilla sketch with bare distance calculation (not using the Ultrasonic library) - Same Result
  • Changed controller (to Arduino Nano ) - same result
  • changed the sensor - same result

Thanks a lot for the help

Not sure totally necessary, but here a dump of the upload using arduino-cli

Performing 1200-bps touch reset on serial port /dev/cu.usbmodem141101
Waiting for upload port...
No new serial port detected.
"/Users/user/Library/Arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/bin/avrdude" "-C/Users/user/Library/Arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/etc/avrdude.conf" -v -V -patmega32u4 -cavr109 -P/dev/cu.usbmodem141101 -b57600 -D "-Uflash:w:/Users/user/development/arduino/water_sensor/build/SparkFun.avr.promicro/water_sensor.ino.hex:i"

avrdude: Version 6.3-20190619
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/Users/user/Library/Arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/etc/avrdude.conf"
         User configuration file is "/Users/victorhg/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/cu.usbmodem141101
         Using Programmer              : avr109
         Overriding Baud Rate          : 57600
         AVR Part                      : ATmega32U4
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PA0
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  9000  9000 0x00 0x00
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : butterfly
         Description     : Atmel AppNote AVR109 Boot Loader

Connecting to programmer: .
Found programmer: Id = "CATERIN"; type = S
    Software Version = 1.0; No Hardware Version given.
Programmer supports auto addr increment.
Programmer supports buffered memory access with buffersize=128 bytes.

Programmer supports the following devices:
    Device code: 0x44

avrdude: devcode selected: 0x44
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9587 (probably m32u4)
avrdude: reading input file "/Users/user/development/arduino/water_sensor/build/SparkFun.avr.promicro/water_sensor.ino.hex"
avrdude: writing flash (20566 bytes):

Writing | ################################################## | 100% 1.59s

avrdude: 20566 bytes of flash written

avrdude done.  Thank you.

Hi @victorhg. I'm going to ask for some additional information that might show us some difference between the compilation process that's happening when you're using the Arduino IDE vs. Arduino CLI. Please do this:

In the Arduino IDE:

  • File > Preferences
  • Check the box next to "Show verbose output during: > compilation
  • Click "OK"
  • Sketch > Verify/Compile
  • After the compilation finishes, copy the full contents of the black console pane at the bottom of the Arduino IDE window and post it here. It will almost certainly exceed the forum's 9000 character limit, so save it as a text file and attach it to your forum reply. If you click the "Reply" button you'll see the "Attachments and other options" link that will allow you to make the attachment.

Post also the full output from your "arduino-cli compile" command, using the -v flag for verbose output.

Thank you for the help @perk!

Here are both outputs (compile + upload)

I noticed the binaries differ in size and even tough both installations are pointing to my user arduino files, they are not always using the same libraries…

Again, thanks a lot!

cli_output.txt (21.9 KB)

ide_output.txt (37 KB)

The reason for the difference is that in the Arduino IDE you have selected Tools > Processor > ATmega32U4 (5V, 16 MHz), which causes the code to be compiled for a 16 MHz clock by adding a -D flag to the compilation commands setting the global F_CPU macro to 16000000L:

-DF_CPU=16000000L

The FQBN you are using with your Arduino CLI configuration doesn't specify a value for the custom cpu board option, so the default value is used (the first option in the menu). So it's the equivalent of compiling in the Arduino IDE with Tools > Processor > ATmega32U4 (3.3V, 8 MHz) selected. This causes the F_CPU macro to be set to 8000000L:

-DF_CPU=8000000L

The solution is to specify the correct value for the cpu custom board option via the FQBN. So in your sketch.json, instead of this:

"fqbn": "SparkFun:avr:promicro",

you should have this:

"fqbn": "SparkFun:avr:promicro:cpu=16MHzatmega32U4",

It looks like you tried to define this via a separate field in sketch.json:

    "cpu": "16MHzatmega32U4",

but this is not supported so it was just ignored by Arduino CLI.

The command:

arduino-cli board details SparkFun:avr:promicro

will show you the available board options and their possible values along with the selected value for each option that is selected by the FQBN you specify to the command.

Thank you @pert! It worked to the mm the sensor! I was even at the sparkFun forum to try to define the sketch file in a better way.

Thanks for the help!!

Quick point though - I can't run the board details, and get a nil pointer exception:

arduino board details SparkFun:avr:promicro -v

INFO[0000] Using config file: /Users/victorhg/Library/Arduino15/arduino-cli.yaml 
INFO[0000] arduino-cli version 0.11.0                   
INFO[0000] Checking if CLI is Bundled into the IDE      
INFO[0000] Loading hardware from: /Users/victorhg/Library/Arduino15/packages 
INFO[0000] Loading package SparkFun from: /Users/victorhg/Library/Arduino15/packages/SparkFun/hardware 
INFO[0000] Loaded platform                               platform="SparkFun:avr@1.1.13"
INFO[0000] Loading package arduino from: /Users/victorhg/Library/Arduino15/packages/arduino/hardware 
INFO[0000] Loaded platform                               platform="arduino:avr@1.8.3"
INFO[0000] Loaded platform                               platform="arduino:megaavr@1.8.6"
INFO[0000] Loaded platform                               platform="arduino:samd@1.8.7"
INFO[0000] Checking existence of 'tools' path: /Users/victorhg/Library/Arduino15/packages/arduino/tools 
INFO[0000] Loading tools from dir: /Users/victorhg/Library/Arduino15/packages/arduino/tools 
INFO[0000] Loaded tool                                   tool="arduino:CMSIS@4.5.0"
INFO[0000] Loaded tool                                   tool="arduino:CMSIS-Atmel@1.2.0"
INFO[0000] Loaded tool                                   tool="arduino:arduinoOTA@1.2.1"
INFO[0000] Loaded tool                                   tool="arduino:arduinoOTA@1.3.0"
INFO[0000] Loaded tool                                   tool="arduino:arm-none-eabi-gcc@7-2017q4"
INFO[0000] Loaded tool                                   tool="arduino:avr-gcc@7.3.0-atmel3.6.1-arduino5"
INFO[0000] Loaded tool                                   tool="arduino:avr-gcc@7.3.0-atmel3.6.1-arduino7"
INFO[0000] Loaded tool                                   tool="arduino:avrdude@6.3.0-arduino17"
INFO[0000] Loaded tool                                   tool="arduino:bossac@1.7.0-arduino3"
INFO[0000] Loaded tool                                   tool="arduino:openocd@0.10.0-arduino7"
INFO[0000] Loading package builtin from: /Users/victorhg/Library/Arduino15/packages/builtin 
INFO[0000] Checking existence of 'tools' path: /Users/victorhg/Library/Arduino15/packages/builtin/tools 
INFO[0000] Loading tools from dir: /Users/victorhg/Library/Arduino15/packages/builtin/tools 
INFO[0000] Loaded tool                                   tool="builtin:ctags@5.8-arduino11"
INFO[0000] Loaded tool                                   tool="builtin:serial-discovery@1.0.0"
INFO[0000] Loading package esp32 from: /Users/victorhg/Library/Arduino15/packages/esp32/hardware 
INFO[0000] Loaded platform                               platform="esp32:esp32@1.0.4"
INFO[0000] Checking existence of 'tools' path: /Users/victorhg/Library/Arduino15/packages/esp32/tools 
INFO[0000] Loading tools from dir: /Users/victorhg/Library/Arduino15/packages/esp32/tools 
INFO[0000] Loaded tool                                   tool="esp32:esptool_py@2.6.1"
INFO[0000] Loaded tool                                   tool="esp32:mkspiffs@0.2.3"
INFO[0000] Loaded tool                                   tool="esp32:xtensa-esp32-elf-gcc@1.22.0-80-g6c4433a-5.2.0"
INFO[0000] Loading hardware from: /Users/victorhg/Documents/Arduino/hardware 
INFO[0000] Loading package sparkfun from: /Users/victorhg/Documents/Arduino/hardware/sparkfun 
INFO[0000] Loaded platform                               platform="sparkfun:avr@"
INFO[0000] Loaded platform                               platform="sparkfun:esp8266@"
INFO[0000] Loaded platform                               platform="sparkfun:samd@"
INFO[0000] Adding libraries dir                          dir=/Users/victorhg/Documents/Arduino/libraries location=user
INFO[0000] current best: arduino:samd@1.8.7              bundle=false managed=true version=1.8.7
INFO[0000] Adding libraries dir                          dir=/Users/victorhg/Library/Arduino15/packages/arduino/hardware/samd/1.8.7/libraries location=platform
INFO[0000] current best: arduino:avr@1.8.3               bundle=false managed=true version=1.8.3
INFO[0000] Adding libraries dir                          dir=/Users/victorhg/Library/Arduino15/packages/arduino/hardware/avr/1.8.3/libraries location=platform
INFO[0000] current best: arduino:megaavr@1.8.6           bundle=false managed=true version=1.8.6
INFO[0000] Adding libraries dir                          dir=/Users/victorhg/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.6/libraries location=platform
INFO[0000] current best: SparkFun:avr@1.1.13             bundle=false managed=true version=1.1.13
INFO[0000] Adding libraries dir                          dir=/Users/victorhg/Library/Arduino15/packages/SparkFun/hardware/avr/1.1.13/libraries location=platform
INFO[0000] current best: esp32:esp32@1.0.4               bundle=false managed=true version=1.0.4
INFO[0000] Adding libraries dir                          dir=/Users/victorhg/Library/Arduino15/packages/esp32/hardware/esp32/1.0.4/libraries location=platform
INFO[0000] current best: sparkfun:avr@                   bundle=false managed=false version=
INFO[0000] Adding libraries dir                          dir=/Users/victorhg/Documents/Arduino/hardware/sparkfun/avr/libraries location=platform
INFO[0000] current best: sparkfun:esp8266@               bundle=false managed=false version=
INFO[0000] Adding libraries dir                          dir=/Users/victorhg/Documents/Arduino/hardware/sparkfun/esp8266/libraries location=platform
INFO[0000] current best: sparkfun:samd@                  bundle=false managed=false version=
INFO[0000] Adding libraries dir                          dir=/Users/victorhg/Documents/Arduino/hardware/sparkfun/samd/libraries location=platform
INFO[0000] current best: SparkFun:avr@1.1.13             bundle=false managed=true version=1.1.13
INFO[0000] current best: arduino:avr@1.8.3               bundle=false managed=true version=1.8.3
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x46b258f]

goroutine 1 [running]:
github.com/arduino/arduino-cli/commands/board.Details(0x4b073a0, 0xc000126008, 0xc00095bd60, 0x46bdf07, 0xc00018e200, 0xc000587d70)
	github.com/arduino/arduino-cli/commands/board/details.go:65 +0x4df
github.com/arduino/arduino-cli/cli/board.runDetailsCommand(0xc000166b00, 0xc000137020, 0x1, 0x2)
	github.com/arduino/arduino-cli/cli/board/details.go:57 +0x145
github.com/spf13/cobra.(*Command).execute(0xc000166b00, 0xc000137000, 0x2, 0x2, 0xc000166b00, 0xc000137000)
	github.com/spf13/cobra@v1.0.0/command.go:846 +0x29d
github.com/spf13/cobra.(*Command).ExecuteC(0xc000166000, 0x0, 0x0, 0xc000108058)
	github.com/spf13/cobra@v1.0.0/command.go:950 +0x349
github.com/spf13/cobra.(*Command).Execute(...)
	github.com/spf13/cobra@v1.0.0/command.go:887
main.main()
	github.com/arduino/arduino-cli/main.go:31 +0x36

thank you anyway!

I’m glad to hear it’s working now!

As for that nil pointer exception, I’ll try to look into that further later today. I just tried your command on Linux and Windows with Arduino CLI 0.11.0 and Sparkfun:avr 1.1.13, just like you and didn’t get that NPE, so I’ll need to try it on my macOS machine to see if it’s an OS-specific issue.

Hi @victorhg. I apologize for taking so long to reply. I did try it on macOS, but still wasn't able to reproduce that NPE. I wanted to suggest that you try the new release of Arduino CLI to see if the problem you're having was already fixed by one of the many improvements that have been made since the 0.11.0 release, and that release is now out!: