Controlling Arduino with Javascript Using the Johnny Five Library

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello, you are watching MakeUseOf.com my name is Ian Buckley so today we will be using the Johnny Five platform in order to control an Arduino using JavaScript this is a fantastic library which allows people to get into robotics mostly is what it's kind of aimed at but as with all things Arduino there is a huge amount you can do with it. For today we are going to set up a servo with an Arduino Uno and use one of the example projects here in order to control it from the keyboard. So without any further ado let's get going. Before we set up the johnny-five library we need to set up our Arduino so today I'm using an Arduino Uno you could use any other Arduino compliant board there is a huge list of compatible boards and you can find them all linked in the main article on the MakeUseOf website. For now this Arduino Uno will do us fine along with this small hobby servo which you can use any again as long as it is Arduino compliant so I have my servo with the red cable plugged into 5 volts the brown cable plugged into ground and the orange cable plugged in to pin number 10 - you don't have to use pin 10 but since the sketch we're using today uses pin 10 already it makes sense but any PWM friendly pin will work. Once you've got the servo plumbed in use a USB cable to attach your Arduino to the computer and open up the Arduino IDE. Here in the Arduino IDE there really isn't much we need to do First things first check that under tools you have the correct board and port selected if you have several comport here and doesn't actually tell you which board it is you can find out that by going to your device manager looking under ports and even if it doesn't specifically say Arduino Uno usually you can work out what it is from there and the sketch we need to upload to the Arduino is one of these standard sketches that comes bundled with the IDE so look under examples look under Fermata and then open standard Fermata plus and once you have it all you need to do is click upload and it should upload to the board provided that all goes well then you are already set up as far as the Arduino goes this is all of the actual onboard programming as it were we will need to do today so let's move on to johnny-five before we can install johnny-five we need to install jas so if you haven't already got it installed head to node.js org and download whatever version you want I'd take the recommended version if I was you and this will give you a normal Windows installer which you can follow through the one thing to pay special attention to when you're installing this is that it does definitely add it to your paths this will just save you stress later on it means anywhere in the command prompt you can run node from in this version it does it automatically but it's just worth bearing in mind in case they change that in the future so get node installed and then we can get started installing johnny-five once node is installed open up the command prompt and we need to set a few things up just before we actually do the install so we'll be installing it using the node package manager if you've never used it before you might want to run NPM in it just to begin with now there's various things you can do with this but I would suggest just for today literally just pressing enter enter enter enter enter enter just to get through it because it'll initiate it once this is something that can be changed later if you get more into node I'm not someone who uses it all that often so this is all I was told to do and it works from here you are now in a position where you should be able to install johnny-five so you do that by just typing NPM install johnny-five and that will try and find all of the parts for it and it should install all the bits and pieces I had a couple of problems with this first time round and that's because I didn't realize you had to initiate that and one other thing I discovered later on when I tried to run Johnny fibers that I was missing another library called key press so I'm gonna just suggest you install that now with NPM install key press now this is a much more standard library used in the node framework in order to as you may have guessed it get key presses and we'll be using that in order to get the key presses from the keyboard and use them to control our server so that's everything we're doing here for now we can move back out and start doing some coding and let's head to a text editor and start doing that we will be using an example from the johnny-five website today so back on the site the code I'm using you can find it under examples and if you scroll down to servo it's just this servo continuous code again there is another servo here with even more different wiring to what I had before so maybe if your wiring is like this this will help you and this code here is will what is what will do the work for us today so I've copied it into notepad plus plus and before we go on let's just have a very quick look through the code now it's not important that you understand this code but it's always worth having to look through to work it out so you know how to change it if you ever want to in the future and this works much like any other piece of JavaScript in that you called the libraries as variables so in this case we have the johnny-five and key pressed libraries and key press is a standard input process which is what's going to allow us to interrupt the program with our keystrokes and the board here is all part of the johnny-five library the one thing about this which is something I haven't really come across before is that it automatically will find your board and port now I have read that this sometime does not happen and there are ways you can actually specify it manually this is all documented on the johnny-five website as part of the reference and so what's this program actually going to do well it's going to give us the prompt of using the up and down arrows so we can move the servo clockwise and counterclockwise and press space to stop it and if you look slightly further down in the code you will also see that if you actually press the letter Q it will quit the program so now that we've been through this save it whatever you want to call it I'm not particularly imaginative so I've just called it servo dot Jas and I've saved it in my user folder just because that's what comes up in my command prompt so it's easier now the way that you run this is just by typing node because we're using node to run it and the name of your JavaScript file and this should just work however I know that it is not going to which is interesting and I'll tell you why now so when I tested this the Arduino was the only thing connected to my computer - and it just found the right comport and it worked immediately now I have other communication ports together and it's not able to do it which is exactly what I mentioned earlier about it sometimes not finding the right comport and then you have to specify it manually so if you're having this problem if you if you're not having this problem I hope you're having a lot of fun with your server right now if you are I'll show you quickly how to fix it so back over here in whatever editor you were using it's all here about where we specify the board where we bring the board in by using the board function in the brackets here you can specify a comport of your own so all you need to do is add some curly brackets some curly braces and that's not very nicely formatted there we go and they're just poor to colon and then the name of your comport if you save this and then try and do exactly the same thing again you should find it will connect to comport or date automatically and there we go our program is working and I don't if you can hear that in the background but it is moving my servo around and hopefully there's some lovely pictures of it working as well so this has been a very basic start with using JavaScript and Arduino together using the johnny-five library this is by no means the only way to use JavaScript with an Arduino but it's probably the easiest just to get started especially if you're already familiar with JavaScript and node and just want to fiddle around with an Arduino incidentally if python is more your thing we have an article on the make use of website which is exactly the same as this just with python as the programming language if you just search for controlling an Arduino with pison on the make use of website you will find that article 2 I'd hope to do a lot more articles like this in the future if there's a programming language that you would like to see working with an Arduino please let us know in the comments below and don't forget to subscribe for more weekly tech tips and giveaways thanks a lot bye-bye
Info
Channel: MakeUseOf
Views: 51,183
Rating: undefined out of 5
Keywords: arduino javascript, arduino javascript tutorial, javascript arduino tutorial, johhny-five arduino, arduino johhny-five tutorial
Id: 6CmIidOxc2g
Channel Id: undefined
Length: 8min 42sec (522 seconds)
Published: Wed Feb 21 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.