Wifi Remote Final
Wifi Remote Final
Wi-Fi Remote-
Controlled
Robot
In this proj ect you ’ re
going to bu i ld a two -
wh eel, battery-powered
robot with a Raspberry Pi
Zero W an d th e MotoZero
add - on . You can control it
over Wi - Fi using a web app
you ’ ll make with Node-RED.
Cost: $ $ Ti me : 3 h o u r s
Pa r t s R e q u i r e d S o ftwa r e R e q u i r e d
Raspberr y Pi Zero W (or other Node-RED dashboard
4 0 G PIO R a s p b e r r y P i )
Smart robot car chassis kit
MotoZero add-on board (or other
motor controller add-on)
F o u r AA b a t t e r i e s
Por table charger
Jumper wires
24 8 • p roj ec t 2 0
P r oj e ct O u tlin e
Rather than going straight into the project, we’ll highlight the most
important parts of the robot to give you an idea of how it will all work.
Wi-Fi
You’ll control the robot with a Node-RED application, so your
Raspberry Pi needs to have Wi-Fi. Raspberry Pi models 3
and Zero W have built-in Wi-Fi, but if your board doesn’t,
you can use a Wi-Fi dongle compatible with the Pi.
Raspberry Pi Board
We’re using the Raspberry Pi Zero W because its small size makes
it perfect for the small robot chassis. But any Raspberry Pi version
with 40 GPIOs is compatible with this project as long as it has Wi-Fi.
Robot Chassis Kit
We’re using a robot chassis kit that comes with everything you
need to build the robot, including wheels, motors, and screws.
You can find the kit in online marketplaces like Amazon or
eBay by searching for Smart Car Robot Chassis Kit. You
need the kit with two DC motors.
MotoZero Add-on
The DC motors will make the robot move, and you’ll control them
using an add-on board called MotoZero. One place to find the
board is online at The Pi Hut (https://thepihut.com/motozero/ ).
You can also use any other Raspberry Pi–compatible motor
driver add-on for this project or build a circuit with the LC293D
IC chip. We won’t cover how to build that circuit here, but there
are plenty of tutorials online if you want to make your own.
Power
We don’t want to connect the Pi robot to a wall socket, because
we want it to be portable, so we need to power the robot with a
portable charger, or power bank. The power bank must be capable
of outputting 5 V and 2 A. We tested this project with a power bank
that has 2,200 mAh capacity and it worked fine; incorporating a
power bank with more capacity will make the robot run for longer.
The DC motors need to be powered independently from the
Pi, meaning you need two independent power sources. To power
up the motors, we’re using the battery holder that comes with the
chassis kit along with four AA batteries, not included in the kit.
249 • p roj ec t 2 0
Node-RED Application
The Node-RED application you’ll use to control your robot
should be able to make the robot go forward and backward,
move right and left, and stop. Since you’re not running the Pi
as a desktop computer, the Pi needs to automatically start
Node-RED when it boots. You’ll also add an off button to the
application so you can turn the Raspberry Pi off remotely.
Figure 20-1 shows a high-level overview of how your robot
will work.
Figure 20-1:
The robot structure RASPB ERRY PI ZER O W (WI TH WI -F I )
DC M OT OR
CO NNE CTE D
TO M OTOZ E RO
A A B ATTE RY
POWER BA NK
F RONT
B ACK
AA B AT TERY
A A B ATTE RY M OT OZER O
AA B AT TERY
P r e pa r in g t h e Ra s p b e r r y Pi
We’re using the Raspberry Pi Zero W board, shown in Figure 20-2,
which is a variant of Raspberry Pi Zero that has built-in wireless LAN
and Bluetooth, but remember that you can use another Wi-Fi compat-
ible board or a Wi-Fi dongle. The Raspberry Pi Zero W measures only
2.56 inches × 1.18 inches × 0.20 inches (65 mm × 30 mm × 5 mm)
and costs around $10.
The Pi Zero has 40 GPIO pins with the same pinout as the Pi 3.
As you can see in Figure 20-2, it features a mini HDMI connector
and two micro USB connectors, one of which is used exclusively for
power. To use the Pi Zero as a desktop computer, you need a few
extra accessories like a USB hub, a USB-to-micro-USB adapter, and
an HDMI-to-mini-HDMI adapter to connect the peripherals. To save
25 0 • p roj ec t 2 0
you some money, we’ll prepare everything on our regular Raspberry
Pi 3 and then switch the micro SD card to the Pi Zero W.
Figure 20-2:
Raspberry Pi Zero W
251 • p roj ec t 2 0
pi@raspberrypi:~ $ sudo systemctl enable nodered.service
With that done, shut down your Pi and switch the micro SD card
to the Raspberry Pi Zero W.
W i r in g t h e C i r c u it
To build the robot structure you need a chassis for the robot, two DC
motors with corresponding wheels, the MotoZero add-on, jumper
wires, and your Pi (with Wi-Fi). Use Figure 20-1 as a reference. We’ll
start by mounting the MotoZero at the top of the Raspberry Pi and
then wire the motors to MotoZero.
Figure 20-3:
Assembled MotoZero
add-on
1. Connect the right DC motor’s red wire to the Motor 1 positive (+)
pin on the MotoZero, and its black wire to the Motor 1 negative
(–) pin. You’ll need to loosen the screws, place the wires in the
pin slot, and then tighten the screws again.
252 • p roj ec t 2 0
2. Repeat the previous instruction for the left motor, connecting the Note
power wires to the MotoZero Motor 2 connections. If you find the robot’s
wheels are spinning in
3. Without inserting the batteries, connect the battery holder’s red
the opposite direction of
wire to the positive (+) pin on the MotoZero power connector what you intended, you
and its black wire to the negative (–) pin, shown at the bottom of may have to switch the DC
the board in Figure 20-3. motors’ red wires with the
black wires on the positive
Controlling the Motors with MotoZero (+) and negative (–) Motor 1
Each DC motor has three GPIO pins associated with it. One pin, or Motor 2 terminals. You’ll
known as the enable pin, enables the motor and is like an on and off know if you need to do this
when you test the appli-
switch. The other two pins control the power to the positive and nega-
cation at the end of the
tive motor wires. Applying power to one wire and GND to the other project.
makes the motor turn in one direction, while applying power and GND
to the opposite motor wires moves the motor in the opposite direction.
We’re just using the Motor 1 and Motor 2 terminals, which are Note
controlled by the GPIOs shown in the following table, when you For information about
mount the MotoZero on the top of the Pi. the Motor 3 and Motor
4 GPIOs, you can check
To make a motor spin, the enable pin must be HIGH to turn the
motor on, and one—and only one—of the positive or negative pins
should be HIGH. For example, if you want Motor 1 to spin in one
direction, use the following setup:
• GPIO 5: HIGH
• GPIO 27: HIGH
• GPIO 24: LOW
To make the same motor spin in the opposite direction, use this:
• GPIO 5: HIGH
• GPIO 27: LOW
• GPIO 24: HIGH
To turn off the motor, you’d send a LOW signal to all the GPIOs.
The same logic applies to the other motors.
25 3 • p roj ec t 2 0
W r itin g t h e A p p licati o n
Once you’ve built your hardware, it’s time to create the Node-RED
application. As your Pi is already in your robot chassis, the most
practical way to create the robot Node-RED application is to use
your regular desktop or laptop computer and control it from there.
First you’ll need to find your Raspberry Pi Zero W IP address;
you’ll use it to access the Pi’s Node-RED application dashboard,
where you can create a robot application.
You need to make sure the Raspberry Pi is turned on and that
your computer and Pi are connected to the same network before
continuing.
Figure 20- 4:
Finding the Raspberry
Pi IP address with
the Angry IP Scanner
software
254 • p roj ec t 2 0
Creating the Node-RED Flow
On your regular computer, making sure it’s on the same net- Note
work as your Pi, open a web browser tab and go to http://<Pi IP For an introduction to
address>:1880, replacing <Pi IP address> with the Raspberry Node-RED, see Project 17.
Figure 20-5:
Node-RED application
dashboard layout
Add five buttons, a function, six rpi gpio output nodes, and an
exec node to the flow. Wire the nodes and edit their names to match
the ones in Figure 20-6.
Figure 20-6:
Node-RED application
nodes
25 5 • p roj ec t 2 0
Edit the function’s properties so that it has six outputs, assigning
all of the nodes’ properties as shown in Table 20-1.
25 6 • p roj ec t 2 0
Node Properties
GPIO: GPIO22 – 15
– M2
Type: Digital output
Group: Poweroff [Robot]
Size: auto
Poweroff Icon: fa-power-off
Label: Poweroff
Background: red
Command: /usr/bin/sudo
+ Append: not checked
exec
poweroff
Name: Poweroff
Figure 20-7:
exec node properties
257 • p roj ec t 2 0
var msg4 = { payload: 0 };
var msg5 = { payload: 0 };
var msg6 = { payload: 0 };
if (msg.payload === "forward") {
msg1.payload = 1;
msg2.payload = 1;
msg4.payload = 1;
msg5.payload = 1;
}
else if (msg.payload === "left") {
msg1.payload = 1;
msg2.payload = 1;
}
else if (msg.payload === "right") {
msg4.payload = 1;
msg5.payload = 1;
}
else if (msg.payload === "reverse") {
msg1.payload = 1;
msg3.payload = 1;
msg4.payload = 1;
msg6.payload = 1;
}
return [msg1, msg2, msg3, msg4, msg5, msg6];
25 8 • p roj ec t 2 0
• Enable M2: 1
• + M2: 1
• – M2: 0
Action En a b l e + M1 – M1 En a b l e + M2 – M2
M1 M2
Forward 1 1 0 1 1 0
Left 1 1 0 0 0 0
Right 0 0 0 1 1 0
Reverse 1 0 1 1 0 1
Stop 0 0 0 0 0 0
25 9 • p roj ec t 2 0
Figure 20- 8:
Node-RED application
to remotely control the
robot
P o w e r in g U p t h e R o b ot
Now that the application is ready, click the Poweroff button to shut
down the Pi. Then wait a few seconds for it to shut down.
Change the Pi’s power source from the wall socket to the
power bank. Wait a few minutes for the Pi to power up and autostart
Node-RED. On a smartphone or other device that’s on the same
network as your Pi, open a new browser tab and go to http://<Pi IP
address>:1880/ui. Then click on the buttons to remotely control your
robot.
Congratulations—you now have a Wi-Fi-controlled robot!
Ta k in g I t F u r t h e r
There’s a lot of room for upgrades on your robot. Here are some ideas
for upgrades that will need both hardware and software changes.
You’ll need to experiment a bit with Node-RED to get these working:
• Get a robot chassis with four wheels and control four motors
instead of two.
• Add LEDs and buzzers to the robot to make it more interactive.
• Add sensors, like an ultrasonic sensor, so the robot can avoid
obstacles by itself.
2 6 0 • p roj ec t 2 0