In this tutorial, we will walk through using the Arduino CLI with an Arduino Uno to compile, upload, and monitor a simple "Hello World" program. Arduino CLI is a command-line tool that provides the same capabilities as the Arduino IDE, allowing you to work with your Arduino boards directly from the terminal.
1. Install Arduino CLI
You can install Arduino CLI with the use of package managers like Homebrew and common Linux package managers or directly using the installation script.
Step 1: Install Arduino CLI:
Install via Package Manager:
macOS:
Use Homebrew to install Arduino CLI:
brew install arduino-cli
Linux:
- Arch Linux: Install Arduino CLI from the AUR:
yay -S arduino-cli
- Debian/Ubuntu:
Install Arduino CLI by downloading the binary manually or via
apt
:
sudo apt update
sudo apt install arduino-cli
- Void Linux:
Install using
xbps
:
sudo xbps-install -S arduino-cli
Windows:
Install using scoop
:
scoop install main/arduino-cli
Install via Installation Script (Linux or macOS only):
Use the official Arduino CLI installation script:
curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh
This script automatically detects your operating system and installs the appropriate version.
Step 2: Verify Installation
After installation, verify that Arduino CLI is correctly installed by running the following command:
arduino-cli version
You should see output showing the installed version of Arduino CLI.
2. Set Up Arduino CLI for Arduino Uno
Step 1: Update Core Index
To set up your Arduino environment, you first need to update the list of available Arduino cores and libraries.
arduino-cli core update-index
Step 2: Install the Arduino Uno Core
The Arduino Uno uses the arduino:avr
core. To install this core:
arduino-cli core install arduino:avr
Step 3: Confirm the Board Installation
To verify that the core has been installed and Arduino Uno is recognized, run:
arduino-cli board listall
3. Create a "Hello World" Sketch
Step 1: Create a Directory for the Sketch
Create a directory for your Arduino sketches if it doesn't already exist:
mkdir -p ~/Arduino/hello_world
cd ~/Arduino/hello_world
Step 2: Create the "Hello World" Program
Create a new file called hello_world.ino
in the Arduino folder:
vim hello_world.ino
Paste the following "Hello World" Arduino code into the file:
void setup() {
Serial.begin(9600); // Start the serial communication
}
void loop() {
Serial.println("Hello, World!"); // Send "Hello, World!" to serial monitor
delay(1000); // Wait for 1 second
}
Save and exit the editor.
4. Compile the Arduino Program
Now that the sketch is ready, we will compile it using the Arduino CLI.
Step 1: Compile the Sketch
To compile the sketch for the Arduino Uno, use the following command. Make sure you are inside the directory where the hello_world.ino
file is located.
arduino-cli compile --fqbn arduino:avr:uno hello_world
--fqbn arduino:avr:uno
specifies the fully qualified board name (FQBN) for Arduino Uno.hello_world
is the name of the sketch.
If the compilation is successful, you will see a confirmation message without any errors.
5. Upload the Program to Arduino Uno
Step 1: Connect Arduino Uno
Connect your Arduino Uno board to your computer using a USB cable.
Step 2: List Connected Boards
To find the port your Arduino Uno is connected to, use the following command:
arduino-cli board list
You will see a list of connected boards along with their ports. For example, it might show something like:
Port Type Board Name FQBN Core
/dev/ttyACM0 Serial Port (USB) Arduino Uno arduino:avr:uno arduino:avr
Step 3: Upload the Program
Once you know the port (in this case, /dev/ttyACM0
), upload the compiled program to the Arduino Uno using:
arduino-cli upload -p /dev/ttyACM0 --fqbn arduino:avr:uno hello_world
This will upload the sketch to your Arduino Uno. You should see a confirmation once the upload is complete.
6. Monitor Serial Output
Now that the sketch is running on your Arduino Uno, you can view the serial output.
Step 1: Open Serial Monitor
To monitor the serial output, use the monitor
command in Arduino CLI. Make sure to specify the correct port (/dev/ttyACM0
in this case).
arduino-cli monitor -p /dev/ttyACM0 -c baudrate=9600
You should now see "Hello, World!" printed every second in the terminal.
Step 2: Stop Serial Monitor
To stop the serial monitor, press CTRL+C
.
7. Conclusion
In this tutorial, we learned how to use the Arduino CLI to:
- Set up the environment for Arduino Uno.
- Compile a "Hello World" sketch.
- Upload the program to the Arduino Uno board.
- Monitor the serial output to verify that the program is running.
This method is efficient for users who prefer working with the command line and provides flexibility to automate processes or integrate with other development tools.