Installation and Simulation

Introduction

Today, we’re looking at installing Arduino-based devices for interaction with through the command-line. The command-line is more transparent than an IDE, and offers us extensibility.
As you will see, I’ve embedded a digital simulation environment. We’re programming a Freenove microcontroller, for no reason, other than it was the first RFID that I came across.

Dependencies

In this guide, we will be using the Arch Linux package manager (AUR ), which comes with any Arch (or Artix) Linux distribution, such as LuminOS . LuminOS is an operating system I wrote which comes bundled with the dependencies we will be discussing, and is a build of features which make my life easier.
The dependencies are the following:

Install them via the AUR with the following command ```{.bash .cb.run} yay -S arduino-cli --noconfirm ``` The Freenove device is compiled with the [standard Arduino avr core](https://docs.arduino.cc/software/ide-v1/tutorials/getting-started/cores/arduino-avr), which we install via the following shell command ([what's 'shell'?](https://linuxcommand.org/lc3_lts0010.php)): ```{.bash .cb.run} arduino-cli core install arduino:avr ``` ~~~BOX {title="Arduino core"} The Arduino core refers to the onboard assembly language of the Arduino microcontroller, as well as to the C-compiler libraries for writing C instruction codes to the assembly. Microcontrollers change, as does the assembly language for each model, but [C, created in the 1970s](https://en.wikipedia.org/wiki/C_(programming_language)), remains constant. ~~~ # Write the C code Arduino C code is referred to as a *sketch*. Following [their tutorial](https://arduino.github.io/arduino-cli/0.35/getting-started/), we'll create one named `MyFirstSketch` in the current directory with the following command: ```{.bash .cb.run} arduino-cli sketch new MyFirstSketch ``` ~~~BOX {title="Why not just write the file directly?"} The `arduino-cli` expects a folder with a `.ino` file with the same name. During compile time, the `arduino-cli` uses wrappers around the `.ino` file. These are the assembly library and any additional necessary C libraries located at ```bash $HOME/.arduino15/packages/arduino/hardware/avr/1.8.6/ ``` ~~~ The command creates a folder with a `MyFirstSketch.ino` sketch file, which is essentially C code. Open the sketch file with the best text editor: ```bash nvim MyFirstSketch/MyFirstSketch.ino ``` The file reads: ```cpp void setup() { }

void loop() { }

We add the following:
```cpp
void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(1000);
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);
}

We can simulate the Arduino using wokwi.com –click the green icon What a time to be alive!

Compile and upload the code

Connect the board via USB, and check it is communicating via the following command:

arduino-cli board list
1Port         Protocol Type              Board Name FQBN Core
2/dev/ttyUSB0 serial   Serial Port (USB) Unknown

Apparently, the FQBN for the Freenove is Unknown. Fortunately, we can use that of the Arduino Uno.

## Missing FQBN (Fully Qualified Board Name)
Although we lack a name for the architecture of 
the board --the FQBN --we know that the Freenove
microcontroller shares the architecture of an Arduino.
I guessed by trying out this command with the most
well-known Arduino microcontrollers, and happily,
the most-famous and first one I tried --the
*Arduino Uno* --worked.  
Sorry Arduino, we love you for starting this 
beautiful movement. I mean no harm in using a Freenove,
I just happened to come across it first, and it 
seems to have also have been built with a lot of love.

Compile the code with the following command

arduino-cli compile --fqbn arduino:avr:uno MyFirstSketch

Optionally, add the -v verbose flag to read the location of the binaries.
Finally, upload binaries to the microcontroller with the following command, changing the location of your USB port as necessary:

sudo arduino-cli upload -p /dev/ttyUSB0 --fqbn arduino:avr:uno MyFirstSketch

The Arduino will now flash on and off with a one-second interval.

A Fibonacci sketch

Let’s try tinkering with the code to make the arduino blink in a more interesting manner. Create a new sketch with the usual command:

1arduino-cli sketch new FibonacciSketch

Let us code the Fibonacci sequence in C. We write the following to the FibonacciSketch.ino:

 1int fib(int initial) {
 2  if (initial <=1) {
 3    return initial;
 4  }
 5  else {
 6    return fib(initial-1)+fib(initial-2);
 7  }
 8}
 9
10void setup() {
11  pinMode(LED_BUILTIN, OUTPUT);
12}
13
14
15int i = 1;
16int r = 0;
17int bounce = 11;
18
19void loop() {
20
21  digitalWrite(LED_BUILTIN, HIGH);
22  delay(fib(i)*10);
23  digitalWrite(LED_BUILTIN, LOW);
24  delay(fib(i)*10);
25  
26  if (r == 1) {
27    i--;
28  }
29  else {
30    i++;
31  };
32
33  if (i > bounce) {
34    r = 1;
35  };
36
37  if (i < 1) {
38    r = 0;
39  };
40
41}

Compile and run the code in one line with bash ; operator:

1arduino-cli compile --fqbn arduino:avr:uno FibonacciSketch; sudo arduino-cli upload -p /dev/ttyUSB0 --fqbn arduino:avr:uno FibonacciSketch

Your Arduino will now be displaying a beautiful golden ratio .

For those who prefer digital simulation, here is another simulation from the wonderful wokwi.com .

Share with your friends!
Learn more by continuing this series , or download and run the source code for this document below!

TutorLumin partners with QuantaLumin

You’re connecting to your QuantaLumin account on members.quantalumin.com.