Platform.IO, Visual Studio Code and Arduino – Bye Bye Arduino IDE

Was looking around at some code examples and I stumbled on to someones comments around Platform.IO as a replacement to the Arduino IDE.  I really don’t like the Arduino IDE so was keen to have a look at it.  I tried Visual Micro (allows Arduino development in Visual Studio) once, but found it a bit buggy and slow.  It may be fine these days?

Any way, i though’t i’d put in the time and learn Platform.IO, and I am very glad i did!.  It took a little bit of messing around but it was worth it.  Never going back to Arduino IDE now I am setup 🙂

Here’s the process I went through to get going on Windows 10:

Installation

Download Visual Studio Code – Its only 43mb – i was thinking Visual Studio, It will take 2 hours to install, but Visual Studio Code is really lightweight.

Run Visual Studio as Admin (right click, run as Admin – needed to do the next bit).

I changed the color scheme to light as I am old school 🙂   – File > Preferences > Color Scheme

Install Platform IO extension in to Visual Studio code – follow these instructions here (this is why running as Admin as it failed for me the first time without running as Admin)

 

All going well, you should be ready to go and when you open Visual Studio Code, you should see the PlatformIO home screen, and a little Home icon on the bottom left of the bottom status bar.

 

Create a Project

Click on + New Project

Give it a name

Select a board – you can search here.. this is what i really liked, heaps of support for modern boards.  I initially tried the WEMOS LOLIN board – ESP32 with an OLED Screen on it.  Have also tried with just basic NodeMCU 1.0 boards too.

For this example, using a Wemos D1 Mini.

Set Arduino as the platform.

Press Finish

Once you press Finish, if this is your first project, it can take some time to open and install all the libraries and toolchains etc.

I thought mine had locked up, but after maybe 7-8 mins, it kicked into life.  Now every time i start a new project it’s fast.

Had the same thing when i started a new ESP32 project – it has to go and get all the bits and bobs to make it go so it’s slow the first time.

Moral – let it do its thing.

 

Getting your head around it

Here’s my notes from my initial – getting my head around it and how it works like Arduino IDE.

First off, the main ‘sketch’ is under the src > main.cpp file

We can get something really basic happening here just writing to the terminal.  Use this code:

Now before we upload our basic sketch, configure the board port and speed.

Open the platformio.ini file from the left hand nav at the bottom

If you need to find what port your board is connected on, check under Device Manager in Windows:

Start > type Device Manager

Expand out Ports (COM & LPT)

Once you have set your correct port in the INI file, you can build and upload.

The bottom nav bar is where you find the build, upload and show terminal buttons, similar to Arduino IDE.

The tick = build, the right arrow = upload, and the plug icon = show terminal window.

Press the tick button to build.

You should now have a compiled version of our simple sketch.  If you have any error, you will see in the Terminal window what’s wrong.  Hopefully you should have no errors with basic sketch above.

Now upload (presuming plugged in) your sketch the the board using the right arrow on bottom Nav.

 

Once you are uploaded, you can then switch on the terminal monitor by pressing the plug icon in the bottom nav:

Here you can see our simple app displaying ‘Hello Universe’ in a loop every second:

Yay, that’s the basics of Platform IO… now for the next bit.. handling libraries.

 

Library Management

This is the bit i like about Platform IO, took my head a bit to get.

First off, there’s a library manager system built in like the later versions of Arduino IDE.

Let’s say we want to add Blynk support.

Switch the PIO Home tab, and click on the Libraries button on the left hand side.

Here you can search for a library.

Click in to the Library to read more, get examples, and Install.

Once the library is installed, it will be put in to your file system here:

This is the folder where libraries are stored.

You should be able to then just add a reference to your library you added:

I found though when you include manual libraries, sometimes they get a green squiggle under them.

In this case, you have to go add a reference to the libraries path.

When you see a green squiggle under the #include line, go check the c_cpp_properties.json file in your project.

Make sure the path to the library you want to use is in this file in the top section.

NOTE: the folder path is separated by forward slashes instead of back slashes.. you need to follow this format.

In this example below, i can see the Blynk library was automatically added when we added the library through the library manager search/install process.

 

If i wanted to download a manual library from the internet and use it in my sketch, i would follow these steps:

For example, lets say i want to use this WifiManager library that has been modified to support ESP32 and isn’t necessarily available in the Library Manager.

I’d download the ZIP file from here: https://github.com/zhouhan0126/WIFIMANAGER-ESP32

I’d right click and unblock the ZIP, then i’d extract it to here:

C:\Users\paul.obrien\.platformio\lib\WIFIMANAGER-ESP32-master

I’d go and insert a reference line in the bottom of the includePath section of my c_cpp_properties.json file (changing the \’s for /’s) to the path of the library.

Now i’d add my #include as normal to my sketch and build.

 

Hope that helps someone get started quickly.

The intellisense, problems tab (so you can immediately see issues), and general workflow is so much nicer than the Arudino IDE.

Plastic Micky – My IoT Robot to entertain the toddler

My 2 1/2 year old is robot mad.  I played him an old youtube clip of Metal Micky from the early 1980’s that was on TV when I was a kid and he has been an addict ever since.  There’s some more youtube vids of a guy in the USA retrofitting OnmniBot (again from the 80’s) with modern tech as well and he watches it over and over..

So I thought, why not build him his own one.  He’s fully into the parts and ‘how does it work’ buzz and fizzes when the AliExpress parcels turn up from China with servo’s and bits and pieces.

I wanted the thing to be a similar size to him, be able to move around, wave its arms, move its head left and right and control various LED lights and strips.  A project we can build up to do different things and tinker around with on Sunday afternoons.

Key things to sus:

  • Body – what to use? (plastic rubbish bins :))
  • Head – what to use – how to make move left and right (servo, bracket etc.)
  • Movement – Tank Tracks on the bottom for movement – Ultrasonic sensor so it can avoid stuff – like Roomba 🙂
  • Lights /Buttons to play with
  • Camera/Screen – got an old android tablet with forward facing camera to stick on the front.
  • Brain – ESP8266 Wifi Micro Controller connected to motors/servo’s/relays to control everything
  • Control – C++ Code on the ESP8266 (Arduino style) and something like Blynk for mobile phone control
  • Semi resemble Metal Micky – we call him ‘Plastic Micky’.

 

So this post is going to be the process of building it up, physically, sourcing all the bits and the electronics (as simply and cheaply as possible) to bring Micky to life..

I aim to build up the electronics using simple cheap readily available parts.. and maybe when the design is ‘stable’ – make in to a single PCB – maybe others can contribute to the design?? I’m going ‘open source’ on the electronics design and C++ code.

Here’s a link to GitHub project for the source code.. https://github.com/paulobriennz/plasticmicky

 

I’m just a beginner really with C++ and micro controllers, but I’ll have a crack.. if any one wants to make the code better – please feel free 🙂  I was even thinking, this could be a project to build in school’s for kids to learn electronics.. there’s ton’s of ‘instructable’ type articles around on the net about how to use an Arduino to make some wheels move, or lights flash, or a servo do things but not really any one stop guide that combines all the components to build a walking talking robot with mobile control and a bit of intelligence to combine movement with ‘personality’.

Also going to build an Alexa skill.. so Micky can do things by voice command as well as mobile phone control.. I’ll post the code to this as well.

 

High level, I’ve come up with an electronics design based trying a few different bits and pieces – :

  • 12v 7A Alarm Battery for Power Source (need to source a battery charging module)
  • NodeMCU/Wemos Development board – ESP8266 based – easy to ‘swap out’ and try different firmware’s etc
  • L298N Motor Controller (2A motor rating) to drive the tracks for movement (initially tried a ESP12E Motor Controller board (600ma motor rating) but not enough guts to handle robot’s weight – chip kept overheating).
  • PCA9685 16 Channel I2c Servo Control Board to move heads, arms, ears, mouth etc..
  • PCF8574 8 Channel I2c GPIO Extender – as we’re going to need a few pins to make this all happen – 6 alone for the L298N Motor Controller
  • Adjustable Buck Regulators to get 5V for Relays and electronics, LCD etc., 6-7v for Solenoids – 12v for Motors, LED Strips etc
  • OLED I2c Screen to help ‘communicate’ what’s going on.
  • Tank Tracks for movement – brought a kit with small ESP12E Motor Shield and NodeMCU – but the chip on the motor driver board is pretty gutless and just overheated trying to drive any weight on the tracks – hence the L298N controller above.  Motors connected to 12v battery directly and it was grunty as so the 12v motors and track mechanism have def got enough guts to move the thing.
  • Pan and Tilt Servo bracket for head – just using a Servo to spin the head left and right (although this area of the design needs more (mechanical) work..

 

Plan is to be able to control via Blynk (or some IoT control app) to move about and make head move, control lights etc. and have an autonomous mode (like a Roomba) that can move around and use its ultrasonic sensor (radar) to avoid objects and change course.  Also expose a ‘web service’ so we can create an Alexa skill to get it to do things.  Simple Wi-Fi Setup so you don’t have to mess round with C++ code to get it to connect – plug and play!

 

Procurement

First mission – what is the body and head going to be… after watching Metal Micky on YouTube for the 407th time with toddler, I thought – rubbish bins like you see in cafeteria’s.

I went to the Warehouse (general everything store we have in NZ – like Home Depot) and got 2 rubbish bins.

1 is a mini ‘wheelie’ bin and the other was a round bin with a push flap

 

https://www.thewarehouse.co.nz/p/living-co-wheelie-bin-black-60l/R2120555.html#start=1

https://www.thewarehouse.co.nz/p/taurus-rad-bin-50l-assorted/R638533.html#start=1

I’ll use the top off the round one for Micky’s head, and turn the wheelie bin upside down, remove the lid and casters and make a wooden base for the wheelie bin to sit on upside down.

This base can have the tracks screwed to it so its relatively ‘stable’.

 

First Time Caller…

Hello World, as they say.

I’m getting all modern and entering the world of blogging…

I’m a 40 year old lad from NZ who likes playing around with gadgets and things and has worked in the IT industry and Cloud Computing space most of my working life…

I’ve also been messing about with some of these new low cost micro controllers and sensors and IoT things over the past couple of years and thought I would share some of my findings that aren’t always so obvious to figure out.. especially if one is new to the whole thing like I was…

Things are changing every day and you can spend hours researching something, and then find someone has already built something that is exactly what you need and you can implement it in 5 minutes….

Anyways, I spend a lot of time online with work and personal stuff finding answers and shortcuts to what I am trying to do and am forever grateful to all the example code and bits and pieces I have learnt from others online over the years that share their knowledge..

Feeling philanthropic in my old age, I hope something I write can help someone else who may be interested in this whole IoT world that is coming… or maybe the odd script or something I write for work that could shortcut someone else having to do the same thing from scratch…