Using Arduino Zephyr Template#

The Arduino Core API module for zephyr leverages the power of Zephyr under an Arduino-C++ style abstraction layer thus helping zephyr new-comers to start using it without worrying about learning new APIs and libraries.

Using this template you can run arduino code on your BeagleConnect Freedom.

Pin Numbering For BeagleConnect Freedom#

You will see pins over Mikrobus-1 and Mikrobus-2 that can be used for Arduino code. You can set each pin to either read signals (input) for things like buttons & sensors or you can set them to send signals (output) to things like LEDs and motors. This lets you interact with and control the physical world using Arduino code on BeagleConnect Freedom board.

Commonly used GPIOs are specified:

  • D0-D18 Digital GPIO pins

  • A0-A5 ADC GPIO pins

  • D2 and D6 PWM GPIO pins

Reference to all GPIO pins are shown in the below images.

BeagleConnect Freedom MB1 pinout

Fig. 476 Mikrobus-1 front annotated arduino pinout#

BeagleConnect Freedom MB2 pinout

Fig. 477 Mikrobus-2 back annotated arduino pinout#

Setup Arduino workspace#

If this is your first time using zephyr, Install Zephyr SDK and install cc1352-flasher using command pip install cc1352-flasher then reboot your system before following the steps below.

  1. Create a workspace folder:

mkdir arduino-workspace
cd arduino-workspace
  1. Setup virtual environment

python -m venv .venv
source .venv/bin/activate
pip install west
  1. Setup Zephyr app:

west init -m https://openbeagle.org/beagleconnect/arduino-zephyr-template .
west update
  1. Setup Arduino module

ln -srf modules/lib/ArduinoCore-API/api modules/lib/Arduino-Zephyr-API/cores/arduino/.
  1. Install python deps

pip install -r zephyr/scripts/requirements-base.txt

Arduino Code#

You can find main.cpp file in the directory arduino-workspace/arduino-zephyr-template/src/ which contains your arduino code. The default code prints Hello World on the serial monitor. Since you are already in the arduino-workspace directory, then proceed with writing the following code.

nano arduino-zephyr-template/src/main.cpp
Listing 11 main.cpp#
#include <Arduino.h>

void setup() {
    Serial.begin(115200);
}

void loop() {
    Serial.println("Hello World");
    delay(5000);
}

Press CTRL+O and ENTER to save, CTRL+X to exit.

Important

You must start your main.cpp code with #include <Arduino.h>.

Build the Arduino directory#

Before flashing, run the command below to build the arduino-zephyr-template for the board beagleconnect_freedom.

west build -b beagleconnect_freedom arduino-zephyr-template -p

Note

Only if you are following the steps from the beginning then the above command will work. Otherwise, make sure that you are in the arduino-workspace directory and setup virtual environment using command source .venv/bin/activate.

Flash BeagleConnect Freedom#

Make sure that your BeagleConnect Freedom is connected with your linux system via USB.

west flash

Serial Output#

Considering your BeagleConnect Freedom is connected to /dev/ttyACM0 you can see the serial output coming from your BeagleConnect Freedom.

tio /dev/ttyACM0