Platformio/platformio-vscode-ide Can Build
Manage Two Arduinos with Ease Using PlatformIO
Sometimes, we demand to test out communication protocols like UART and I2C on two Arduino boards. Based on my experience, the testing experience on the Arduino IDE is far from optimal. First, yous have to create a split up instance of the IDE in order to open ii serial ports simultaneously. Second, ports are occasionally deselected by "magic" and you accept to reselect them from the tools->port dropdown. Third, the IDE lacks useful features like autocomplete, goto definition, and flexible pane layout. Despite these shortcomings, I do not intend to blame the Arduino IDE because it's designed to exist beginner-friendly. Nonetheless, when it comes to more complicated applications, we can bear some added complexity in exchange for flexible configurations. In this case, the Platformio IDE is the best tool. Information technology leverages the familiar & well-crafted Visual Studio Code IDE while providing flexible configurations through a simple platformio.ini file.
Requirements
Hardware
- Arduino Nano 33 BLE Sense
- Arduino Nano 33 BLE Sense (or any other Arduino board that works at iii.3V)
- ii x mini breadboard or one x long breadboard
- 3 x jumper wires
Software
- Platformio IDE (install from here)
Skills
- Familiarity with Visual Studio Code
- Basic command-line skills
Overview
Nosotros are going to show how you can take advantage of Platformio past demonstrating a UART instance between two Arduino Nano 33 BLE boards. In this example, nosotros will power both the Arduino boards through the computer. And then, nosotros volition use the Serial Monitor to transport some commands from the transmitter board through UART to the receiver board. Depending on the commands received, the receiver board volition turn ON or OFF its born orangish LED. You can browse through this official Arduino tutorial to learn more near the instance. Hither's the schematic of what we are building:
Code
Go to Visual Studio Code, and create a new Platformio project.
In the project sorcerer, select Arduino Nano 33 BLE equally the board and Arduino as the framework.
Under the src directory, create two files named receiver.cpp and transmitter.cpp.
Here'south the code for receiver.cpp:
Here'due south the code for transmitter.cpp:
The two source files above are adopted from the Arduino tutorial with an added bugfix on the transmitter side and useful debug messages on the receiver side.
Configure
After y'all select the board and framework in the Project Wizard, Platformio generates a platformio.ini file to match your selections. Withal, the configuration does not work out-of-the-box for projects with multiple main files, i.east. receiver.cpp and transmitter.cpp. If you build the project, you will receive an error like the one below:
Linking .pio\build\nano33ble\firmware.elf
c:/users/lixiang/.platformio/.../ld.exe:
.pio\build\nano33ble\src\transmitter.cpp.o: multiple definition of `setup';
.pio\build\nano33ble\src\receiver.cpp.o: first defined here
c:/users/lixiang/.platformio/.../ld.exe:
.pio\build\nano33ble\src\transmitter.cpp.o: multiple definition of `loop';
.pio\build\nano33ble\src\receiver.cpp.o: commencement divers here
collect2.exe: error: ld returned 1 exit condition
The fault is long and nasty, but it conveys a very unproblematic message: Platformio doesn't know which master file to use during the build because they both contain a setup function and a loop function. Essentially, Platformio is confused nigh which version of the functions to pick. This is not a bug though if we analyze the default platformio.ini generated by Platformio:
Each project may have multiple configuration environments defining the available project tasks for building, programming, debugging, unit testing, device monitoring, library dependencies, etc.¹ By default, Platformio generated the [env:nano33ble]
configuration environment for us. To adjust the 2 chief files, we demand to excerpt the common configurations into the common environment called [env]
and make ii more environments specific to each main file. Then during testing, nosotros can build and upload those two main files separately.
Before nosotros proceed to add those environments, we need to understand how Platformio includes/excludes source files from build. Unlike IDEs like Eclipse where you correct-click on a filename to exclude information technology from build, Platformio offers a more powerful and generic src_filter
property in the platformio.ini file. To include every file under the src/ directory, use +<*>
, where *
is a wildcard that matches all file names. To exclude a particular file from build, apply -<filename.cpp>
.² Notation that all paths are relative to the src/
directory, not the project root. You tin change the default by specifying the base of operations path in the src_dir
belongings.³
Now, nosotros tin create the two new environments named [env:transmitter]
and [env:receiver]
.
In the common environment [env]
, we include all source files using the most generic src_filter
. Then in [env:transmitter]
environs, we exclude the receiver.cpp. Similarly, we exclude the transmitter.cpp in the [env:receiver]
environment. Annotation that ${env.src_filter}
only refers to the src_filter
property of the [env]
environment.
Before nosotros start edifice and testing the instance, we can save ourselves some hassle past configuring the USB port for each surroundings. The ports have different names on different operating systems but the steps to obtain them are the aforementioned. Beginning, connect your 2 Arduinos to the computer through USB. Second, open up a terminal (Command Prompt on Windows, Terminal on Mac & Linux) and type:
pio device listing
This will list all bachelor devices continued to the figurer.⁴ On my Windows computer, I get this output:
> pio device list
COM12
-----
Hardware ID: USB VID:PID=1234:5678 SER=F8EF8889D0211923 LOCATION=1-three.2:x.0
Description: USB 串行设备 (COM12) COM11
-----
Hardware ID: USB VID:PID=1234:5678 SER=DAB991012486A110 LOCATION=1-3.1:x.0
Clarification: USB 串行设备 (COM11)
The important $.25 are the underlined names: COM11 and COM12. On Mac & Linux, you will become dissimilar names. We choose COM11 every bit the transmitter port and COM12 every bit the receiver port. This determination is arbitrary as either board can be the transmitter/receiver.
It's time to update our platformio.ini with port configurations:
The upload_port property specifies the port to upload the build and the monitor_port belongings specifies the port to monitor during execution.
Build & Upload
Open a new final in VS Lawmaking past going to Terminal->New Terminal.
If your default last does not back up Platformio (like WSL concluding on Windows), create a new terminal by clicking on the dropdown arrow next to the little + icon.
In the final, build and upload the transmitter and receiver environments by typing:
pio run
You lot will encounter build logs like to the one below:
Monitor
Open port monitors from the left side:
First, starts monitoring the transmitter:
2nd, starts monitoring the receiver:
Third, drag the transmitter monitor to the left of the receiver monitor:
Now, you become a nice split monitor with the transmitter on the left and the receiver on the right:
Test
Focus your mouse on the the left transmitter monitor and type the number i on your keyboard. Y'all should run across the message LEDS ON immediately printed on both monitors. Accept a look at your boards, you should see both born orange LEDs light upwardly.
Adjacent, type the number 0 in the left transmitter terminal. You should see the message LEDS OFF immediately printed on both monitors. Take a look at your boards, you should see both built-in orange LEDs light off.
Congrats!
Y'all have successfully learned how to manage multiple Arduinos using the powerful Platformio IDE.
Source: https://kevinxli.medium.com/manage-two-arduinos-with-ease-using-platformio-4f83ad4a8868
0 Response to "Platformio/platformio-vscode-ide Can Build"
Post a Comment