Platformio Cannot Upload Waiting for New Poert
It'south time to look at a more advanced development environment for programming our microcontrollers, so today nosotros'll take a await at PlatformIO.
It's a fleck of a learning curve, but well worth it, as PlatformIO has many advantages over using the Arduino IDE. I'll prove you how to set it upwardly and how to use information technology with the Arduino Uno, ESP32, and Seeeduino XIAO.
Introduction
When we begin working with the Arduino one of the first steps is to install the Arduino IDE (Integrated Development Environs). Information technology's a cardinal piece of software that runs on Linux, Windows, or Mac OSX and it allows united states of america to program our little microcontroller wonders to do just about anything.
The Arduino IDE has a lot going for information technology. It'southward very like shooting fish in a barrel to use, especially for beginners, and it comes with a smashing array of sample sketches to get y'all going. Past adding additional Boards Managers you lot can use it for more than just Arduino boards. And, because it's then popular, you'll detect an abundance of information to help you on the web and on YouTube (and, of course, hither on the DroneBot Workshop!).
Merely for all of its glory, the Arduino IDE does have many shortcomings.
- It lacks a debugger, a tool that allows you to insert breakpoints into your code and and so observe the state of cardinal variables when these points are reached.
- It requires you to manually make up one's mind which USB or serial port your microcontroller is continued to, sometimes (particularly with Linux or Mac OSX) this is not obvious.
- It does not provide help such as auto-complete or built-in references to allow you to catch errors earlier you compile.
- It cannot be integrated with a code repository, such equally GitHub.
For beginners, many of those features, like the debugger and Git integration, are not essential. But beginners can however benefit from features like auto-consummate and an integrated lawmaking reference, as they aid anyone write code with fewer errors.
Enter PlatformIO.
PlatformIO has those missing features, along with many more. So it is certainly worth taking a look at, no matter what your level of coding experience.
Before we begin, let's run over a few features mutual to many IDE'due south.
Integrated Development Environments
Developing code, whether it be for microcontrollers, mobile devices or desktop platforms, requires a number of common steps:
- You need to use some form of text editor to write the code in the desired programming language. The most common languages for writing microcontroller code are C++ and Python (or microPython).
- That text needs to exist converted into car-readable lawmaking, suitable for your target device. This is a job for either a compiler or an interpreter, depending upon which linguistic communication you are using,
- That machine-readable code is then uploaded to the target device. In the example of interpreted languages, such equally Python, the editor interfaces straight with the device and an interpreter translates the code every fourth dimension it is run.
You can use a basic text editor and a command-line utility to practise all of the to a higher place, but most people find information technology easier to use specialized GUI-based tools.
The Arduino IDE has all of the features you lot need to compose, compile, and upload lawmaking to your target microcontroller. It also has a Series Monitor to observe activeness on the microcontroller and to exchange commands with information technology. It can manage libraries and yous can add alternate microcontrollers to it.
Sounds like it has all we need. And then why switch?
Well despite all of its features the Arduino IDE is really but a basic IDE, and it is missing a lot of features that advanced editors and IDEs have. Things like auto-complete, which tin can salve you some typing, inline fault checking to catch your mistakes as you lot make them, and an onboard reference to aid you empathize your code.
PlatformIO has more features than the Arduino IDE, features that brand it much easier to create and troubleshoot your code.
PlatformIO
According to the PlatformIO documentation "PlatformIO is a cross-platform, cross-architecture, multiple framework, professional person tool for embedded systems engineers and for software developers who write applications for embedded products."
Put another way, this is a development tool that can run on about operating systems and nether many unlike code editor packages. A few of the editors it runs under are:
- Visual Studio Code (VS Code)
- Atom
- Codeblocks
- Eclipse
- Netbeans
- Sublime Text
Information technology can likewise be run on cloud-based packages like Codeanywhere and Eclipse Che.
Nosotros will exist running it under Microsoft Visual Studio Code, a free development platform available for Linux, Windows and Mac OS X.
Visual Studio Code
While many people don't associate Microsoft with free software they actually have created several complimentary products, many of them evolution environments.
- Visual Studio Community is a free IDE for developing iOS, Android, Windows and spider web applications.
- Visual Studio Dev Essentials is a collection of tools, deject services and software trials.
And, of course, Visual Studio Code, the free open up-source code editor that can run on whatsoever operating system. This is the product that we will be running PlatformIO under.
Visual Studio Code includes IntelliSense, an advanced auto-complete and syntax highlighting system that tin help you in creating better lawmaking without errors. This allows you to catch and correct coding errors earlier you compile your code.
Information technology besides has a debugger, a software diagnostic tool that allows you to troubleshoot code that isn't working the way you expect it to. This is a more than advanced characteristic that we won't be examining today.
I of the greatest features of Visual Studio Code, or VS Lawmaking, is that it supports extensions. These extensions allow yous to add together boosted functionality to VS Code, enabling you lot to use VS Code for virtually any platform and coding language.
PlatformIO is an extension to VS Lawmaking.
Installing PlatformIO with VS Code
Equally we volition be using the PlatformIO extension for VS Code the first thing we will need to practise is get Visual Studio Code installed.
Another requirement for using PlatformIO is to have Python version 3.5 or higher installed.
The installation procedure differs depending upon your operating system, but it's pretty easy. Follow the instructions for your Os.
Linux Installation
There are two means (at to the lowest degree) to install VS Code and Python on Linux.
- Use the Snap Store snap. This also requires the installation of a Python Virtual Surroundings.
- Install from the file downloaded from the VS Lawmaking website. In virtually cases, the native Python on your Linux distribution will piece of work without updating.
I described the first installation method, using the Snap store, in the article and video I published for Building a Developers Linux Workstation. So rather than repeat myself I'll refer you to that article if you want to employ the Snap store. This method has the advantage of the VS Lawmaking updates existence applied automatically when you practise a system update.
The second method is to grab the installation file from the Visual Studio Lawmaking website. The site should defaut to the correct installation files for your operating system, but if you want a different version y'all can also go to the downloads section at the bottom of the page.
For Ubuntu and other Debian-based distributions of Linux, y'all'll want to take hold of the .deb file. If y'all're running a derivative of RedHat Linux then the .rpm file is what you lot need.
If you're running Ubuntu then you can but click on the .deb file in one case it finishes downloading. The Software Installer will open up and you can click Install to brainstorm the installation process.
You'll need to authenticate start, then the installer will add VS Code to your organization.
With this type of installation method that's really all there is to it, equally you likely already have Python iii. To verify this open a Terminal and blazon the following:
This will display your current version of Python three. Equally long equally it is at least version 3.5 you are good to get.
You tin now skip ahead to the PlatformIO plugin installation procedures.
Mac OS X Installation
Installation of VS Code on Mac OS 10 is pretty simple, you'll be installing both VS Code and Python 3.
The beginning step is to visit the VS Code Website and download the application for Mac OS X, information technology should default to that when you load the page.
The download is a complete application, and you lot tin can run it directly as soon every bit it is installed. It would probably be a proficient idea to move it from your downloads folder into a more than advisable folder, like Applications.
The first fourth dimension you run VS Code you'll demand to concur to trust it, as it is a file downloaded from the Internet.
After getting Visual Studio Code you'll need to install Python.
Caput over to the Python website downloads page. As with the VS Code website you should come across a big button to download an installation package with the latest version of Python for the Mac.
This time you are downloading a package that you lot volition demand to install. Follow the instructions and accept the license agreement and the installer volition run.
When the installer has finished you have an pick to put the original install packet into the trash, which is probably a adept idea.
Subsequently you lot complete the Python installation, open up your Terminal and utilise the same command we used in Linux to bank check the electric current version of Python:
This volition print back the version of Python, which should (hopefully) exist above version three.5. You lot are now prepare to proceed to the installation of the PlatformIO plugin, detailed in a fleck.
Microsoft Windows 10 Installation
The Windows 10 installation is as well very simple. As with the Mac, you'll need to install both VS Code and Python 3.
Again yous need to grab the installation program from the Visual Studio Lawmaking website. Click on the file you downloaded to outset installing VS Lawmaking.
As with most Windows programs you'll need to select a location to install your new software and make up one's mind if you want information technology placed in the Start menu, unless you take a specific reason for irresolute these you tin just accept the default settings. You'll likewise need to take a license agreement.
Ane affair that is very important is to go out the box for "Add to PATH" checked on the Select Additional Tasks dialog box. You lot may select other choices here equally well.
After making all your selections VS Code will begin to install. The installation can accept a bit of time, so please be patient!
Once the installation is finished you have the option of opening Visual Studio Code, yous may wish to exercise that just to ensure that everything installed correctly.
But we aren't done even so, we even so need to install Python.
You tin can caput over to the Python Download Page on python.org to go the installation file. One matter to note is that the processor yous are using may not accept the 64-scrap version of Python, even if y'all are running 64-bit Windows. You tin can then choose to grab the latest version of the 32-fleck version of Python, it will work fine with PlatformIO.
On the dialog box with the "Install Now" choice, you'll see a checkbox allowing you to "Add together Python iii.x to Path". You must have that selected for PlatformIO to role correctly.
Subsequently selecting the checkbox you lot can click "Install Now" and the installation will begin.
Once Python is installed you are all set up to add together the PlatformIO plugin to Visual Studio Lawmaking.
Install PlatformIO Plugin for VS Lawmaking
The instructions here apply to whatever operating system.
Open up Visual Studio Code. You'll initially be greeted with a welcome screen that outlines some of the features of VS Code. You can shut this screen after you cease examining it.
On the left side of the VS Code interface you'll run into a number of icons. As yous move your mouse over these icons their functions will be displayed.
The bottom icon (bold you lot oasis't installed any plugins all the same) is shaped similar a group of squares, this is the Extensions icon. Click on information technology, this volition open another pane where you can search for extensions.
Blazon "platformio" in the search box. One of the items in the results will be "PlatformIO IDE". In that location will exist a blue "Install" push button abreast the search consequence. Click on it to install PlatformIO.
The PlatformIO plugin installation will have a little while, so be patient and don't do anything in VS Code while it is installing. You lot tin can monitor the installation progress on the lower right side of the screen, at one point during the installation the Final area will likewise display some information.
Once the installation is finished you will need to shut and reopen Visual Studio Code. When you open VS Code information technology volition cheque all of its extensions, including PlatformIO. After information technology is done you are ready to use PlatformIO.
PlatformIO Basics
When you first showtime Visual Studio Lawmaking with the PlatformIO extension you lot'll be greeted by the PlatformIO Home screen. If you don't see information technology look for a small icon on the lesser taskbar shaped like a "firm" and click on it.
The Dwelling house screen displays the version of PlatformIO and also has a Quick Access section, which we will use to first our first project.
Earlier we get to that let'due south examine some of the other icons down the side of the PlatformIO screen.
- Home – You lot accept already seen this, information technology has the current version and the Quick Access box that allows you to create new projects.
- Projects – A listing of all the projects you have created. You can edit these to add descriptions.
- Inspect – This allows you to inspect a project for statistics like memory utilization.
- Libraries – This is the Library Managing director, which we will describe in detail later on in this article.
- Boards – A list of the boards supported past PlatformIO. Equally of this writing, in that location are over 900.
- Platforms – Platforms like the ArduinoAVR, Espressif ESP32, and others are listed here. The list will grow as you lot build projects with new boards.
- Devices – A list of the boards that are currently attached to your computer. This is congenital up automatically so you don't need to select the port, unlike the Arduino IDE.
Creating Your First Project
In PlatformIO your "sketches" are actually role of a "projection", the term "sketches" is not used here.
All of the resources required for your project are contained in 1 place, this includes libraries and code files.
Get back to your PlatformIO Home screen and click the New Projection button. This will launch the Projection Wizard.
The Projection Wizard makes it very easy to create all of the initial files required for a PlatformIO project. You volition need to supply the following information:
- Project Proper noun – Obviously the proper noun of your project!
- Board – The blazon of microcontroller lath you lot are using.
- Framework – The framework must match the board, and PlatformIO will determine this automatically for you once you select a board.
- Location – Where you want your files stored. Y'all can just leave this checked to accept the default location or uncheck it to choose a specific one.
Selecting a board may announced to be quite daunting, as PlatformIO has over 900 of them! But you lot certainly don't need to scroll through the listing manually, merely showtime typing the bard proper noun and the search will be narrowed down to ane or more than matching selections.
After you make the selections for your board PlatformIO will ready up the files for your new project. If this is the first time you have used this type of lath it will need to grab several files from the Net, so you may have to wait a infinitesimal for it to finish. Subsequent selection of the same lath will be much quicker, every bit it only has to do this one time per board.
main.cpp File
I of the first big differences between PlatformIO and the Arduino IDE is the type of files y'all'll exist coding.
In the Arduino IDE, most of your files apply the .ino extension. When you start a new projection in the Arduino IDE you typically proper noun the main file something like "mycode.ino". When you commencement save it the IDE will place information technology in a binder chosen "mycode".
In PlatformIO in that location are a number of files created for a project. The principal one is called main.cpp. The .cpp extension means "C++", the language yous are coding in.
You lot will continue all of your code files in a subdirectory called src, which is an abbreviation for "source".
On the left side pane, you should run across your projection listed, and beneath information technology, y'all'll see a number of subdirectories. Expand the src subdirectory past clicking on it and you should run across a main.cpp file. Click on this file to open it in the editor window.
Y'all will notice that the primary.cpp file already has a line in it that reads as follows:
This line must be present in every program y'all write for microcontrollers using the Arduino framework, including non-Arduino boards similar the ESP32. PlatformIO will automatically insert this line into the main.cpp file for you. If you are copying some existing Arduino lawmaking y'all can paste it underneath information technology.
Arduino Uno Blink Examination
Let's commencement with the "Hi World" programme for microcontrollers – the infamous Blink! Connect an Arduino Uno to one of your USB ports to follow forth.
Here is Blink, modified for PlatformIO with the inclusion of the Arduino.h library:
ane ii 3 4 5 half dozen 7 8 ix 10 xi 12 xiii 14 xv sixteen 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include <Arduino.h> /* Simple Glimmer sketch unproblematic-glimmer.cpp Use for PlatformIO demo From original Arduino Blink Sketch https://www.arduino.cc/en/Tutorial/Blink DroneBot Workshop 2021 https://dronebotworkshop.com */ // Set LED_BUILTIN if undefined or not pin 13 // #define LED_BUILTIN 13 void setup ( ) { // Initialize LED pivot as an output. pinMode ( LED_BUILTIN , OUTPUT ) ; } void loop ( ) { // Set the LED HIGH digitalWrite ( LED_BUILTIN , Loftier ) ; // Wait for a second delay ( 1000 ) ; // Set the LED LOW digitalWrite ( LED_BUILTIN , Low ) ; // Await for a 2nd delay ( thousand ) ; } |
The code hardly needs whatsoever description, it's basically the Glimmer sketch yous know and love (or at least know), using the constant LED_BUILTIN to correspond the Arduino's onboard LED, which is connected to pivot xiii. Note that the definition of LED_BUILTIN is remarked out, as the Arduino Uno framework already knows it.
Equally with the Arduino IDE, you lot'll need to compile the code, and then upload information technology to your Arduino Uno.
Compiling the code is washed using the PlatformIO Build push, which is a checkmark on the lower toolbar. Click on the checkmark and find the progress in the terminal window. The code should compile successfully.
The side by side step is to upload it to the Arduino. Click on the key next to the Build key, the ane shaped like an arrow. This is the Upload primal. Once again I'll betoken out that we never had to tell PlatformIO which USB port we had our Arduino continued to, it figures it out past itself.
PlatformIO will upload the compiled lawmaking to the Arduino, and you should run into the familiar flashing LED as a result.
ESP32 Blink
Now that we have seen how to compile and upload lawmaking using PlatformIO permit'due south change our microcontroller board.
Remove the Arduino Uno and claw upwards an ESP32 board, pretty well any ESP32 board volition practice. After you take done that get back to the PlatformIO Home page and kickoff a new project with the Quick Access New Project button.
Give your project a proper noun and type "esp32" into the Lath textbox. A listing of ESP32 boards will be displayed. Coil through the list until you find a board that matches yours.
Note that the Framework will be filled in equally "Arduino", this is correct equally despite our lath using an ESP32 processor we are still using the Arduino framework to program it.
Once again you'll need to wait while the required support files are downloaded and installed. When that task is completed you volition see a new project on the left pane, right below the get-go projection.
Expand the src subdirectory and open up the main.cpp file for editing. It will look identical to the one nosotros opened for the Arduino.
Now paste the same Blink code into the principal.cpp file in your editor. Later that hit the Build button (checkmark) to compile it.
This time you lot'll get an mistake, and the code will fail to compile. And there is a good reason for that!
The error is produced because the abiding LED_BUILTIN is non predefined for the ESP32. In improver, the ESP32 board uses a different pivot for the built-in LED.
In order to set this, you'll need to unremark the definition of LED_BUILTIN, besides every bit change its value from "xiii" to an appropriate pivot number. On my ESP32 that is pin 2.
One time you make those changes you can Build and Upload the lawmaking, and your ESP32 will start flashing its approval!
Seeeduino XIAO with Serial Monitor
I repeated the Blink code, this time with a projection for the Seeeduino XIAO. LED_BUILTIN is already a predefined constant on the XIAO, so it will run correctly.
I then modified the lawmaking, every bit just blinking was getting a bit dull, and I added statements to print the LED land to the Serial monitor.
You lot tin follow the steps I took in the video accompanying this article, starting at the 21:09 mark. Pay attention to how PlatformIO "assisted" me when writing the code for the serial monitor. This is actually part of the Visual Studio Code Intellisense feature I mentioned earlier.
My final code looked like this:
1 2 iii 4 five 6 7 8 9 10 eleven 12 13 xiv 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 thirty 31 32 33 34 35 36 37 38 39 forty | #include <Arduino.h> /* Uncomplicated Blink sketch simple-blink.cpp Use for PlatformIO demo From original Arduino Glimmer Sketch https://www.arduino.cc/en/Tutorial/Glimmer DroneBot Workshop 2021 https://dronebotworkshop.com */ // Set LED_BUILTIN if undefined or not pivot thirteen // #define LED_BUILTIN thirteen void setup ( ) { // Initialize LED pin as an output. pinMode ( LED_BUILTIN , OUTPUT ) ; Serial . begin ( 9600 ) ; } void loop ( ) { // Set the LED HIGH digitalWrite ( LED_BUILTIN , High ) ; Serial . println ( "HIGH" ) ; // Look for a second filibuster ( 1000 ) ; // Set the LED LOW digitalWrite ( LED_BUILTIN , LOW ) ; Serial . println ( "Low" ) ; // Wait for a second filibuster ( grand ) ; } |
Compile and upload the code to your Seeeduino XIAO, and as you might expect the onboard LED volition beginning blinking. But how do nosotros see the Series Monitor?
On the taskbar on the bottom of the IDE there is an icon that looks like a "plug", this is the PlatformIO Serial Monitor. Click the icon and the Terminal expanse will switch to a serial monitor, and you'll be able to observe the LED state here.
And if you're using the Seeeduino XIAO keep in mind that a HIGH turns off the LED, which is astern from the Arduino Uno!
To shut the Serial Monitor hit Ctrl-C (command-C on a Mac). Then striking your Enter key (or whatsoever other key, it really doesn't matter). This volition close the Series Monitor and brandish the Terminal over again.
Using Libraries with PlatformIO
Ane task we need to know how to perform is to employ libraries with PlatformIO.
In PlatformIO libraries work a flake differently than they practice with the Arduino IDE, although you lot also have the option of using them in the aforementioned way.
Library Management – Arduino IDE
In the Arduino IDE we have a Library Managing director. This has access to thousands of Arduino libraries, and we can search for them and install them into our IDE.
Libraries installed in the Arduino IDE are available for every Arduino sketch. So, for case, if we install Version ane of our library in the IDE and nosotros accept four sketches ALL of the sketches have access to that library.
If we upgrade that library to Version ii then all of our sketches have Version 2.
While this may seem to exist a good affair it sometimes can cause bug:
- If nosotros re-create our lawmaking to another computer that doesn't have the required library the code will neglect.
- If nosotros base our code on Version ane it is possible that Version 2 will break it.
- If we find another library that has the aforementioned proper name as our first one nosotros can't use it unless nosotros remove the original one.
PlatformIO handles libraries in a different way.
Library Management – PlatformIO
In PlatformIO libraries are managed on a per-project basis. You install your libraries into your projection, not into the whole IDE.
And so, to repeat our concluding example, we have PlatformIO with iv projects.
In the in a higher place example, nosotros have Version ane of our library jump to both Projection 1 and Project three. The other ii projects practice non need this library.
At present we take updated Project one to apply Version 2 of our library. We take likewise added Version 2 to Project 4. Only Projection three has not been updated, perhaps we are concerned that information technology may break due to changes in the new version. PlatformIO allows you lot to do this.
And so the big difference in PlatformIO is that y'all add your libraries to the Project, not to the whole IDE.
BTW, when you lot use the Arduino framework you already get all the built-in libraries that the Arduino has. And, like the Arduino IDE, they are available for all of your projects.
Dual Servo Library Demo
To illustrate this I'k going to put together a small project, 1 with an Arduino Uno, a couple of servo motors, a PCA9685 16-channel I2C servo commuter, and a 2×16 LCD display with an I2C backpack.
The PCA9685 and the LCD display will require libraries, and we will also need the Arduino Wire library as well. So information technology's a skilful demo for using libraries, plus it's a cool little project.
You tin claw everything up as shown hither:
Once you lot take everything hooked upwards nosotros volition need some code.
We will start another new project using the Project Wizard and choose an Arduino Uno for our board. The code we will demand to put into main.cpp is equally follows:
one ii iii 4 5 half-dozen 7 8 9 x 11 12 13 14 xv sixteen 17 18 19 20 21 22 23 24 25 26 27 28 29 thirty 31 32 33 34 35 36 37 38 39 twoscore 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 sixty 61 62 63 64 65 66 67 68 69 lxx 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | /* Servo Motor Controller Demo for PlatformIO servo-control-demo.cpp Controls ii servo motors, uses PCA9685 PWM Controller Displays status on 16x2 LCD Uses LiquidCrystal PCF8574 LCD Library Uses Adafruit PWM library Uses 2 potentiometers Uses I2C LCD Display DroneBot Workshop 2021 https://dronebotworkshop.com */ // Include Arduino framework #include <Arduino.h> // Include PCF8574 Library for I2C LCD #include <LiquidCrystal_PCF8574.h> // Include Wire Library for I2C Communications #include <Wire.h> // Include Adafruit PWM Library #include <Adafruit_PWMServoDriver.h> // Define I2C Address - change if required const int i2c_addr = 0x3F ; // Define LCD object LiquidCrystal_PCF8574 lcd ( i2c_addr ) ; // PWM Parameter Definitions #define MIN_PULSE_WIDTH 650 #ascertain MAX_PULSE_WIDTH 2350 #define FREQUENCY 50 // Define PWM object Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver ( ) ; // Define Potentiometer Inputs int control0 = A0 ; int control1 = A1 ; // Define Motor Outputs on PCA9685 board int motor0 = 0 ; int motor1 = one ; // Define Motor position variables int mtrDegree0 ; int mtrDegree1 ; // Define Motor previous position variables int mtrPrevDegree0 = 0 ; int mtrPrevDegree1 = 0 ; // Variable to determine if display needs updating boolean updatedisplay = 0 ; // Function to movement motor to specific position void moveMotorDeg ( int moveDegree , int motorOut ) { int pulse_wide , pulse_width ; // Catechumen to pulse width pulse_wide = map ( moveDegree , 0 , 180 , MIN_PULSE_WIDTH , MAX_PULSE_WIDTH ) ; pulse_width = int ( float ( pulse_wide ) / 1000000 * FREQUENCY * 4096 ) ; //Control Motor pwm . setPWM ( motorOut , 0 , pulse_width ) ; } // Function to convert potentiometer position into servo bending int getDegree ( int controlIn ) { int potVal , srvDegree ; // Read values from potentiometer potVal = analogRead ( controlIn ) ; // Summate bending in degrees srvDegree = map ( potVal , 0 , 1023 , 0 , 180 ) ; // Return angle in degrees render srvDegree ; } void setup ( ) { // Setup PWM Controller object pwm . begin ( ) ; pwm . setPWMFreq ( FREQUENCY ) ; // Set brandish blazon as 16 char, 2 rows lcd . begin ( 16 , two ) ; //Turn on the LCD Backlight lcd . setBacklight ( 255 ) ; // Clear the display lcd . clear ( ) ; lcd . home ( ) ; // Print on showtime row of LCD lcd . setCursor ( 0 , 0 ) ; lcd . impress ( "Servo 0: 0" ) ; // Print on 2nd row of LCD lcd . setCursor ( 0 , 1 ) ; lcd . print ( "Servo 1: 0" ) ; // First Serial Monitor Series . begin ( 19200 ) ; } void loop ( ) { //Control Servo Motor 0 // Get desired position mtrDegree0 = getDegree ( control0 ) ; // Move motor merely if control position has changed if ( mtrDegree0 != mtrPrevDegree0 ) { // Move motor moveMotorDeg ( mtrDegree0 , motor0 ) ; // Update motor moved variable updatedisplay = 1 ; // Update previous position mtrPrevDegree0 = mtrDegree0 ; } //Control Servo Motor 1 // Get desired position mtrDegree1 = getDegree ( control1 ) ; // Move motor only if control position has changed if ( mtrDegree1 != mtrPrevDegree1 ) { // Motility motor moveMotorDeg ( mtrDegree1 , motor1 ) ; // Update motor moved variable updatedisplay = 1 ; // Update previous position mtrPrevDegree1 = mtrDegree1 ; } // Update display if required if ( updatedisplay == 1 ) { // Articulate the display lcd . articulate ( ) ; lcd . home ( ) ; // Print on outset row of LCD lcd . setCursor ( 0 , 0 ) ; lcd . impress ( "Servo 0: " ) ; lcd . print ( mtrDegree0 ) ; // Impress on second row of LCD lcd . setCursor ( 0 , i ) ; lcd . print ( "Servo ane: " ) ; lcd . print ( mtrDegree1 ) ; } // Print to Serial Monitor Serial . print ( "Motor 0: " ) ; Serial . print ( mtrDegree0 ) ; Serial . print ( "\t" ) ; Serial . print ( "Motor ane: " ) ; Serial . println ( mtrDegree1 ) ; // Reset the motor moved variable updatedisplay = 0 ; // Add curt delay delay ( 100 ) ; } |
You tin can try to compile the code right now with the Build button, however, you won't be successful. The reason is probably pretty obvious, you haven't installed the libraries.
To do that you'll demand to utilize the PlatformIO Library Managing director.
Using The Library Manager
Open the PlatformIO Domicile screen, recollect you can ghetto it with the footling "house-shaped" icon on the bottom taskbar. You can also notice information technology by clicking the PlatformIO icon on the left panel of VS Code and looking in Quick Access for Dwelling house.
The library manager is the fourth icon downward, yous tin't miss it as it's labeled Libraries! Click on the icon to open it.
You'll see a search box where y'all tin can search for your required library. Information technology'south a pretty advanced search, y'all tin can input the library name, its file name, or the proper name of the component you need a library for.
Our servo projection requires the following libraries:
- LiquidCrystal PCF8574 LCD Library
- Adafruit PWM library
Search for the beginning library. You'll come across it displayed prominently amongst the results. Click on the desired library, and its details will exist displayed. Note that y'all also get sample code and versioning information with the library.
There is an Add to Project push that you tin use to add the library to your project. Click on it and notice the project you demand to add information technology to, note that the project you are currently working on volition be displayed first.
When the library is added a message will be displayed, congratulating you lot on accomplishing your task!
Click the Library Manager icon once more to search for the second library, and echo the same steps to add it to the project.
At present become back into the main.cpp for the projection and endeavour compiling it once more. This time it will compile successfully, and you can upload information technology to the Arduino.
Try out the project, you should exist able to move both servo motors and find their position on the display.
platformio.ini File
You will notice that the lawmaking too makes use of the Serial Monitor. Click the "plug" icon on the lesser taskbar to open the series monitor.
You probably won't like what yous see in the Serial Monitor, it'southward only a bunch of random characters. Go back into the code and you'll see why we aren't getting a proper display.
In this code, I ready the serial output to 19,200 baud, instead of 9600. But since our Serial Monitor defaults to 9600 baud, information technology is not displaying the text correctly. We need to change the speed.
Nosotros do this by editing the platformio.ini file. You'll find this file at the lesser of every projection, in the left pane.
The platformio.ini file contains the parameters for your project. It is a bones text file that you can edit.
Yous'll notice lines for the environment y'all are working in, the board and the framework.
There are also lines for library dependencies, in fact you can just edit this file to install libraries and PlatformIO will install any missing ones automatically.
The platformio.ini file makes information technology possible to easily move your project to another reckoner.
Nosotros will need to edit our platformio.ini file to set up the speed of the series monitor. In the editor add together the following line to the file:
At present save the file (Ctrl-C or command-C on a Mac) and go back into the Serial Monitor. You will notice that the text is now readable and the monitor displays the servo position.
You can examine the platformio.ini files for our other projects if you wish to get a better understanding of how it all works.
Determination
The learning curve for PlatformIO is a scrap steeper than information technology is for the Arduino IDE, just the effort is well worth information technology. PlatformIO is a more advanced code editor that will assistance you write better code for a multitude of microcontrollers.
I'll be using PlatformIO for many of my upcoming projects, and we will likewise revisit PlatformIO in the most futurity to larn more about the advanced features of this wonderful develop[ment surround.
Happy coding!
Summary
Article Proper name
Getting Started with PlatformIO
Description
PlatformIO is a evolution surroundings for the Arduino, ESP32, and over 900 other microcontrollers. It has many advanced features that go far a dandy alternative to the Arduino IDE. Learn how to install and use PlatformIO.
Author
DroneBot Workshop
Publisher Proper name
DroneBot Workshop
Publisher Logo
Source: https://dronebotworkshop.com/platformio/
0 Response to "Platformio Cannot Upload Waiting for New Poert"
Post a Comment