Arduino on FreeBSD

tags: bsd · programming · tutorials

This article demonstrates how to develop for Arduino boards using only basic command line utilities, without having to use the Arduino IDE. The article has also been published on the FreeBSD Wiki.

Tested on FreeBSD 12.2 and above.

Prerequisites

Required ports:

devel/arduino-core
devel/arduino-bsd-mk
devel/avr-gcc
devel/avr-libc
devel/avrdude
comms/uarduno

With all the software installed, add the following line to /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

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 a Makefile with the following lines:

ARDUINO_DIR= 	/usr/local/arduino
ARDUINO_MK_DIR= /usr/local/arduino-bsd-mk
#ARDUINO_LIBS=	
AVRDUDE_PORT=	your_board_port
ARDUINO_BOARD= 	your_board_name
SRCS=		your_source_files
TARGET=		your_program_name

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

In my case my board is an Arduino Uno, so I’d have to set ARDUINO_BOARD to uno. You can see which other board types are available in /usr/local/arduino/hardware/arduino/avr/boards.txt. If you want to install new libraries, copy them over to /usr/local/arduino/hardware/arduino/avr/libraries/.

Avoid having source files named main.

Building and uploading a program

Write some Arduino code, and when you’re ready to compile and upload, run the following command:

# make install flash clean cleandepend

If all went well you should see the board executing the 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 ~! (use the appropriate port):

$ 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, you have to hear a click sound.

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