The Arduino IDE is a great tool to get started with, but once your project gets bigger and you're dealing with multiple files and boards, it can be frustrating. I tried a couple of IDEs and come to like PlatformIO. It's very useful at the most basic level and even more powerful once you know the ins and outs. It could be better, but it has potential.
To introduce PlatformIO, I've created a project around neopixel to show how we can add libraries to our project and different boards can then compile and upload them to the boards.
In this project, you will learn to do the following with PlatformIO:
- Install it
- Create a project
- Add libraries
Look at the schematic (if I have it up) to figure how to hook up the neopixel to an Arduino Uno. If you're using different hardware differences are minor hopefully.
Arduino Uno to Neopixel
- Vin to 5V
- Ground to Ground
- Pin 6 to Data
For the Arduino Uno you can access the USB 5V through Vin. For some boards it's a different name (ie 5v) or it's not allowed in which case you'll have to source the 5V somewhere else.
Pin 6 is fairly arbitrary so you can use any digital pin you want.
Installing PlatformIOPlatformIO is an IDE that is essentially a plugin for Atom or VScode, both of which are very flexible text editors. I won't go over install Platform IO as it's fairly simple and you can find instructions on their website.
Atom vs VScode. I don't too much of the difference between them but if you do or end up trying them both, leave a comment and let other people know why.
Install Plugins/PackagesHere some other very useful plugins that will making your coding experience much easier. These plugin can be installed in the same way you install the PlatformIO IDE.
- Auto-complete clang -- Basically like Google's auto complete. As you type it will give a list a suggestion. If it's a function of a class it will list only function of that class. Very handy.
- Linter gcc -- Helps catch error as you type rather than waiting until you compile. Great time saver especially when you are hunting around trying to figure out the issue when all you need is a missing semicolon.
- Mini-map -- Nice to have, especially if your file becomes long.
- Highlight-selected -- Make it easy to spot out variable of the same name that you selected.
- TODO -- Haven't used this yet but sounds useful.
You can find more better instructions on creating a new projects for Atom and VScode by visiting the PlatformIO tutorials.
Open the console with "Ctrl+`" - mac is "cmd-shift-t."
Create a new folder where you want your project. When you installed Platform.io, it created a new folder under documents/
# create new directory
> mkdir User/Documents/PlatformIO/new_directory
# go to it
> cd User/Documents/PlatformIO/new_directory
If you don't create a folder, a new folder will be created for you and a random numerical name, like "171120-22477110-uno," will be used.
When you first open atom you should see the PlatformIO home page. Click "New Project" to start the project wizard which will help you create the project, folders and files associated with it.
Select your board here and select "Arduino" for Framework. Uncheck the "Use default location" and select the folder you created earlier. Click "Finish" to create your project.
Once it's done, you'll see a popup that says "PROJECT_NAME has been initialized." You'll see two new folders and a couple of files.
You have created your first project.
Install Neopixel LibraryOpen the console with "Ctrl+`" or "cmd-shift-t" (Mac).
Use the following command line to search for library that contains the word Neopixel.
pio lib search neopixel
This will list libraries matching that criteria. Make sure the library is compatible with your platforms (i.e. board). In the case of the Arduino Uno we need to make sure Atmel AVR is compatible.
We saw that the Adafruit NeoPixel library is ID # 28 so will use that to identify which library to download and install.
pio lib install 28
Open main.cpp in the source file and include the new library:
#include <Adafruit_NeoPixel.h>
Create a neopixel object. As you type you should have notice suggestion popping up for you.
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// ----- Copy here and below -----
#define DATA_PIN 6
const int numOfLeds=60;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(numOfLeds, DATA_PIN, NEO_GRB + NEO_KHZ800);
- DATA_PIN -- Put in the pin that is hooked up to the data line of your neopixel.
- numOfLeds -- Put in the number of leds that you have on your strip
- NEO_GRB + NEO_KHZ800 -- Defines set by the library. If you have the same led strips listed in this project you'll want to use this.
!!!IMPORTANT!!!
If you have more than 8 leds and they are only being powered through USB make sure you call setBrightness and set it low. If you don't the led will draw too much power and can potentially fry your board and your computer along with it.
void setup()
{
strip.begin();
//Insure we don't pull to much power.
strip.setBrightness(3);
.....
.....
.....
}
Let's add lights now.
For this demo we'll look at the two main functions.
/*
x - index of the led strip
g,r,b - values of the individual colors
*/
strip.setPixelColor(x,g,r,b);
Calling setPixelColor by itself won't do anything. You'll need to tell the neopixel to show your changes.
/*
Output the changes you made since you last call to show()
*/
strip.show();
So with those to functions:
void loop() {
for(int x=0;x<numOfLeds;x++){
int g = random(0,255);
int r = random(0,255);
int b = random(0,255);
strip.setPixelColor(x,g,r,b);
strip.show();
delay(25);
strip.setPixelColor(x,0,0,0);
}
for(int x=numOfLeds - 1; x >= 0; x--){
int g = random(0,255);
int r = random(0,255);
int b = random(0,255);
strip.setPixelColor(x,g,r,b);
strip.show();
delay(25);
strip.setPixelColor(x,0,0,0);
}
}
Try out this code and see what it does.
Note: You can copy and paste the code attached from github if you want to take the short route.
Build and Upload to BoardFinally you can build and upload your code. Click the upload button on the top left which will build and upload to your build.
You can also click build (the check mark) to just build without upload if you ever want to just build to check your code.
Adding More BoardsIf you decide to use a different board for this project, you can always change it after the fact.
There are two ways to do this. One is to simply to edit the project's config file.
- Go to the boards manager and make sure that your desired board's package is installed
- Open the config file (platformio.ini) from the sidebar
- Choose your platform. You can find the list of platform names here.
- Add your board. You can find a list of boards here.
- Choose the framework for your board. You can find your framework here.
The other way is to initialize the project with a new board.
Make sure you're in the current project in the command line. Type:
pio boards
Copy your board ID from the list.
Type:
pio init -b [YOUR BOARD ID]
It will say "Project has been successfully initialized!"
Your board should show up in a new environment in the platformio.ini file (in this case, env:nanoatmega328):
If you want to be able to choose a board from the command line when you build your project, add a "build flag."
As you can see, Platform.io offers dozens of boards, and even has the option to configure custom boards. Let us know how it goes as you get started in development using Platform.io!
Comments