Arduino Native (CLI)

This article demonstrates how to develop for Arduino boards using only basic command line utilities, without having to use the Arduino IDE.

Preparation

Required ports:

devel/arduino-core

/devel/arduino-mk

/comms/uarduno

Install the ports as root. Alternatively, you can install them with pkg:

  # pkg install arduino-core arduino-mk uarduno

For a larger set of tools, you can install arduino-tools which will pull arduino-core and arduino-mk automatically; the current setup is enough though.

Now that you have all the software installed, add the following line to your /boot/loader.conf in case you want the Arduino kernel module to load automatically on boot. If you want to manually load the module whenever you need it, skip this step:

  uarduno_load="YES"

Load the kernel module:

  # kldload uarduno

Check your ~/.arduino/preferences.txt and see if the following lines exist (source):

 serial.port=/dev/cuaU0
 launcher=/usr/local/bin/firefox

Add your user to the dialer group:

  # pw group mod dialer -m $USER

Connecting the board

Standard Arduino boards connect as /dev/cuaU0 and/or /dev/ttyU0 on FreeBSD. In case these serial ports don't show up in /dev, you might need to press your board's reset button. After you've plugged your board into a USB port, you should get the following output from dmesg. Although the output may vary, the important thing is that your board is connected and detected.

  ugen1.5: <Arduino (www.arduino.cc) product 0x0043> at usbus1
  uarduno0: <Arduino (www.arduino.cc) product 0x0043, class 2/0, rev 1.10/0.01, addr 5> on usbus1

If dmesg returned information about your board, you should also see cuaU0 and/or ttyU0 in /dev. In case your board is still not detected - considering it's not a fake one, try using a different USB cable or reset it again and make sure you've followed the setup steps correctly.

The Makefile

As I said above, the only thing you're going to need in order to get started is just a Makefile that'll be used to compile and upload your Arduino programs. Make a new directory for your Arduino project and then make a Makefile with the following lines:

  ARDUINO_DIR = /usr/local/arduino
  ARDMK_DIR = /usr/local/arduino-mk
  ARDUINO_LIBS = /usr/local/arduino/libraries
  ARDUINO_PORT = /dev/cuaU0
  ARDUINO_BOARD = uno

  include /usr/local/arduino-mk/Arduino.mk

In this case my board is an Arduino Uno, but if you have a different one, edit the ARDUINO_BOARD variable. You can see which board types are available in /usr/local/arduino/hardware/arduino/boards.txt. If you want to install new libraries copy them over to /usr/local/arduino/libraries.

Building and uploading a program

So now the only thing you have to do is to actually write some Arduino code. After you're done writing your code and you're ready to compile and upload, run the following command (you have to use gmake for that):

  # gmake clean && gmake upload clean

If all went well you should see the board executing your new code, if it doesn't, try to see what errors the Makefile produced.

Monitoring

The Arduino IDE provides a serial monitor feature, but FreeBSD has a builtin monitoring utility which can be accessed directly from the terminal. Run this whenever you want to monitor your board and exit with ~!:

  $ cu -l /dev/cuaU0

Using board types other than the Uno

As it's mentioned above, we're using the uarduno kernel module. Even though the module's description is "FreeBSD Kernel Driver for the Arduino Uno USB interface", you can, in fact, use different board types other than the Uno. According to uarduno's website, you can modify /usr/ports/comms/uarduno/files/ids.txt to include more board types; the two fields are Vendor ID and Product ID. Read the comments inside the file for more information.

  { 0x2341, 0x0001 },  // Arduino UNO, vendor 2341H, product 0001H
  { 0x2341, 0x0042 },  // Arduino MEGA (rev 3), vendor 2341H, product 0042H
  { 0x2341, 0x0043 },  // Arduino UNO (rev 3), vendor 2341H, product 0043H
  { 0x2341, 0x0010 },  // Arduino MEGA 2560 R3, vendor 2341H, product 0010H
  { 0x2341, 0x8037 },  // Arduino Micro

When you're done, clean and re-build the port.

Known issues and their fixes

Even though you might have plugged your board to your machine, you might notice that there is no device appearing in /dev. Although there is no definite answer as to why this is happening, make sure that the USB cable is connected properly; on some boards - certainly mine - you have to hear a click sound.

When trying to use a new library, you might notice that your code doesn't compile. Considering the library isn't broken, a common issue is that you haven't stored the library in the correct path. As already mentioned, libraries are stored in /usr/local/arduino/libraries, so you have to move it there.

Arduino/NativeCLI (last edited 2021-03-28T05:55:45+0000 by KubilayKocak)