Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon

Programming Raspberry-Pi Robots with JavaScript

Save for later
  • 6 min read
  • 16 May 2016

article-image

The Raspberry Pi Foundation recently announced a smaller, cheaper single-board computer—the Raspberry Pi Zero. Priced at $5 and measuring about half the size of Model A+, the new Pi Zero is ideal for embedded applications and robotics projects. Johnny-Five supports programming Raspberry Pi-based robots via a Firmata-compatible interface that is implemented via the raspi-io IO Plugin for Node.js. This post steps you through building a robot with Raspberry-Pi and Johnny-Five.

What you'll need

  • Raspberry Pi (for example, B+, 2, or Zero)
  • Robot chassis. We're using a laser-cut acrylic "Smart Robot Car" kit that includes two DC motors with wheels and a castor. You can find these on eBay for around $10.
  • 5V power supply (USB battery packs used for charging mobile phones are ideal)
  • 4 x AA battery holder for the motor
  • Texas Instruments L293NE Motor Driver IC
  • Solderless breadboard and jumper wires
  • USB Keyboard and mouse
  • Monitor or TV with HDMI cable
  • USB Wi-Fi adapter
  • For Pi Zero only: mini HDMI—HDMI adaptor or cable, USB-on-the-go connector and powered USB Hub

 programming-raspberry-pi-robots-javascript-img-0

A laser cut robot chassis

Attach peripherals

If you are using a Raspberry Pi B+ or 2, you can attach a monitor or TV screen via HDMI, and plug in a USB keyboard, a USB Wi-Fi adapter, and a mouse directly. The Raspberry Pi Zero doesn't have as many ports as the older Raspberry Pi models, so you'll need to use a USB-on-the-go cable and a powered USB hub to attach the peripherals. You'll also need a micro-HDMI-to-HDMI cable (or micro-HDMI-to-HDMI adapter) for the monitor.

The motors for the robot wheels will be connected via the GPIO pins, but first we'll install the operating system.

Prepare the micro SD card

Raspberry Pi runs the Linux operating system, which you can install on an 8 GB or larger micro SD card:

  • Use a USB adapter or built-in SD card reader to format your micro SD card using SD Formatter for Windows or Mac.
  • Download the "New Out Of the Box Software" install manager (NOOBS), unzip, and copy the extracted files to your micro SD card.
  • Remove the micro SD card from your PC and insert it into the Raspberry Pi.

Power

The Raspberry Pi requires 5V power supplied via the micro-USB power port. If the power supplied drops suddenly, the Pi may restart, which can lead to corruption of the micro SD card. Use a 5V power bank or an external USB power adaptor to ensure that there will be an uninterrupted supply. When we plug in the motors, we'll use separate batteries so that they don't draw power from the board, which can potentially damage the Raspberry Pi.

Install Raspbian OS

Power up the Raspberry Pi and follow the on-screen prompts to install Raspbian Linux. This process takes about half an hour and the Raspberry Pi will reboot after the OS has finished installing.

The latest version of Raspbian should log you in automatically and launch the graphical UI by default. If not, sign in using the username pi and password raspberry. Then type startx at the command prompt to start the X windows graphical UI.

Set up networking

The Raspberry Pi will need to be online to install the Johnny-Five framework. Connect the Wi-Fi adapter, select your access point from the network menu at the top right of the graphical UI, and then enter your network password and connect.

We'll be running the Raspberry Pi headless (without a screen) for the robot, so if you want to be able to connect to your Raspberry Pi desktop later, now would be a good time to enable remote access via VNC.

Make sure you have the latest version of the installed packages by running the following commands from the terminal:

sudo apt-get update
sudo apt-get upgrade

Install Node.js and Johnny-Five

Raspbian comes with a legacy version of Node.js installed, but we'll need a more recent version. Launch a terminal to uninstall the legacy version, and download and update to the latest by running the following commands:

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at £16.99/month. Cancel anytime
sudo apt-get uninstall nodejs-legacy
cd ~
wget http://node-arm.herokuapp.com/node_latest_armhf.deb
sudo dpkg -i node_latest_armhf.deb

If npm is not installed, you can install it with sudo apt-get install npm.

Create a folder for your code and install the johnny-five framework and the raspi-io IO Plugin from npm:

mkdir ~/myrobot
cd myrobot
npm install johnny-five 
npm install raspi-io

Make the robot move

A motor converts electricity into movement. You can control the speed by changing the voltage supplied and control the direction by switching the polarity of the voltage. Connect the motors as shown, with an H-bridge circuit:

programming-raspberry-pi-robots-javascript-img-1

Pins 32 and 35 support PWM, so we'll use these to control the motor speed. We can use any of the digital IO pins to control the direction for each motor, in this case pins 13 and 15. See Raspi-io Pin Information for more details on pins.

Use a text editor (for example, nano myrobot.js) to create the JavaScript program:

var raspi = require('raspi-io');
var five = require('johnny-five');
var board = new five.Board({
  io: new raspi()
});

board.on('ready', function() {
  var leftMotor = new five.Motor({
    pins: {pwm: "P1-35", dir: "P1-13"},
    invertPWM: true
  });

  var rightMotor = new five.Motor({
    pins: {pwm: "P1-32", dir: "P1-15"},
    invertPWM: true
  });

  board.repl.inject({
    l: leftMotor,
    r: rightMotor
  });

  leftMotor.forward(150);
  rightMotor.forward(150);
});

Accessing GPIO requires root permissions, so run the program using sudo: sudo node myrobot.js.

Use differential drive to propel the robot, by controlling the motors on either side of the chassis independently. Experiment with driving each wheel using the Motor API functions (stop, start, forward, and reverse, providing different speed parameters) via the REPL. If both motors have the same speed and direction, the robot will move in a straight line. You can turn the robot by moving the wheels at different rates.

Go wireless

Now you can unplug the screen, keyboard, and mouse from the Raspberry Pi. You can attach it and the batteries and breadboard to the chassis using double-sided tape. Power the Raspberry Pi using the 5V power pack. Connect to your Raspberry Pi via ssh or VNC over Wi-Fi to run or modify the program.

Eventually, you might want to add sensors and program some line-following or obstacle-avoidance behavior to make the robot autonomous. The raspi-io plugin supports 3.3V digital and I2C sensors.

About the author

Anna Gerber is a full-stack developer with 15 years of experience in the university sector. She was a technical project manager at The University of Queensland (ITEE eResearch). She specializes in digital humanities and is a research scientist at the Distributed System Technology Centre (DSTC). Anna is a JavaScript robotics enthusiast and maker who enjoys tinkering with soft circuits and 3D printers.