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:
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) UnknownApparently, 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 FibonacciSketchLet 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 FibonacciSketchYour 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!