My Saturday Afternoon Robot

How to build a Raspberry Pi robot written with JavaScript and controlled with an iPhone

Gabriel G Baciu
5 min readJan 4, 2015

--

Robots. It’s a very sci-fi term, one would think that robots are something seen only in movies. This is far from the truth because you can make a robot and you can build it in the comfort of your own home, all in a few hours.

This is how. What you need is of course parts. This is what you need:

  1. Raspberry Pi (model b+ is used)
  2. Breadboard
  3. 2 DC Motors + Wheels attached
  4. One small wheel to be the third wheel (this robot has only 3 wheels, and only 2 are powered)
  5. 1 Chassis
  6. 2 AA Battery powered 5v output to power your PI and the wheels
  7. Jumper wires
  8. Raspberry Pi WiFi dongle
  9. 1 L293D Chip

I bought all the above from http://tinkersphere.com/. They even have a robot kit that is Arduino compatible, this is what I bought. I believe it’ll save you some money if you buy the pack rather than separate parts.

They even have a tutorial on how to assemble the robot and code it. If you bought their kit, it’s better to follow their tutorial, not to reinvent the wheel (http://tinkersphere.com/documents/TS427TinkersphereArduinoRobotUserGuide.pdf).

There were, though, two problems with my robot: I had a Raspberry Pi and not an Arduino, and the battery power seemed not to output exactly 5v and my Raspberry Pi was thrown off, it was lighting the on led but was not booting the Debian OS.

The battery problem is easy to solve. If you have an iPhone or Android external battery that outputs on micro USB, you can use that. It has a ton more power and it’s stable and outputs exactly 5v.

Having a Raspberry Pi to do the GPIO for the robot is quite problematic because the GPIO on Pi differs from model to model (mine has 40 GPIO pins, model b+) and there is no mapping on the board on which one is ground, which one is 5v, etc. So you can easily screw up by outputting other voltage from or in the board and you can break things or even burn some circuits on the board or peripherals.

Luckily, there is a tutorial on how to connect two DC motors on a Raspberry Pi (thanks guys!): http://computers.tutsplus.com/tutorials/controlling-dc-motors-using-python-with-a-raspberry-pi--cms-20051.

The good thing about different models of Raspberry Pi is that they maintained backward compatibility on the location and function of the pins. So if you have a Pi with 26 pins on GPIO and one with 40, the first 26 pins are the same as in older models, so the mapping applies.

This wiring of the Pi board with the breadboard and the two DC motors can take a while and you have to get it right. If you miss a pin or you don’t put the power in the right place, the circuit will not work and you can even break things. So pay attention to the wiring.

After the physical parts are assembled, using the two tutorials above (tinkersphere.com, computers.tutsplus.com), we get to the really fun part, writing logic to move the robot.

Some software specifications, Raspberry Pi runs on a flavor of Linux called Debian. Because it’s a Linux machine, you can SSH into it and you can connect it to the internet via the WiFi router (if you don’t know how to do that, there is some doc here: http://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md).

The best feature of the Linux machine is that it can run Node.js. Yay! You can download your Linux binary from here: http://nodejs.org/download/. Instructions on how to install Node and configure it on the machine are assumed to be known if you got so far reading this article.

So in terms of software components, this is what we will use:

  1. Node.js
  2. Express
  3. Socket.io
  4. Jonny-Five: https://github.com/rwaldron/johnny-five
  5. Raspi-io: https://github.com/bryan-m-hughes/raspi-io
  6. Virtualjoystick: https://github.com/jeromeetienne/virtualjoystick.js

Basically, we gonna run an express server on the robot, that serves a static page. The static page will have socket.io to send user events back to the server. The events that are being sent are keyboard arrows events or virtual joystick events.

The server receives direction events via the TCP sockets and maps a direction to a function. Each function sets voltage high or low on the GPIO pins.

Ok, let's see some code. The pins are jonny-five Pins:

var Motor1A = ‘P1–16';
var Motor1B = ‘P1–18';
var Motor1E = ‘P1–22';
var Motor2A = ‘P1–23';
var Motor2B = ‘P1–21';
var Motor2E = ‘P1–19';
// Normalize and store in vars
var p16 = new five.Pin(Motor1A);
var p18 = new five.Pin(Motor1B);
var p22 = new five.Pin(Motor1E);
var p23 = new five.Pin(Motor2A);
var p21 = new five.Pin(Motor2B);
var p19 = new five.Pin(Motor2E);

This is a function that moves the robot forward by spinning wheels clockwise.

function goForward(){
// Set pin outputs
p16.high();
p18.low();
p22.high();
p23.high();
p21.low();
p19.high();
};

If you want to go left or right is enough to move only one of the wheels and the robot will go in that direction.

function turnRight(){
// Set pin outputs
p16.high();
p18.low();
p22.high();
}

The express server is simple and only has two routes.

var app = require(‘express’)();
var server = require(‘http’).Server(app);
var io = require(‘socket.io’)(server);
server.listen(3000);var interv = setInterval(function(){
if(new Date() — lastUpdate > 40){
stopMoving();
}
}, 20);
app.get(‘/’, function (req, res) {
res.sendfile(__dirname + ‘/index.html’);
});
app.get(‘/virtualjoystick.js’, function (req, res) {
res.sendfile(__dirname + ‘/virtualjoystick.js’);
});

And the socket.io events that map to moving functions.

io.on(‘connection’, function (socket) {
socket.on(‘direction’, function (data) {
switch(data) {
case ‘front’:
goForward();
lastUpdate = new Date();
break;
case ‘back’:
goBackwards();
lastUpdate = new Date();
break;
case ‘right’:
turnRight();
lastUpdate = new Date();
break;
case ‘left’:
turnLeft();
lastUpdate = new Date();
break;
default:
stopMoving();
}
});

A timeout is being used to see if an event has happened in the last ~33 milliseconds, and if it hasn’t, the robot will stop. This ensures that if you don’t keep pressing the keyboard or touch the screen on the mobile phone, the robot stops moving.

The code is entirely on GitHub and it is open source, so feel free to use it when making your own robot: https://github.com/motiooon/pi-robot

And here’s a video if you want to see the robot in action:

Enjoy making your own robot at home and stay tuned for the next robot venture.

--

--

Gabriel G Baciu

Software engineer, leader, entrepreneur, lately focusing on the science of leadership and success, healthy living, and low latency software systems.