Remotely Controlling a Motor Using Arduino and Node.js
In a previous article on this website, I taught you how to read a temperature measurement from your Arduino board, and how to display it on a server so it can be accessed remotely. In this article, I will show you the other way around: how to send commands to devices and control them remotely. And I will take a simple example: a DC motor. This is a very useful skill to acquire in the home automation world, as you want for example to automatically control curtains in your home, that are basically nothing but a larger DC motor to command. Of course, everything will be done using well known technologies like Arduino and some Node.js & JavaScript.
What You'll Need
| Component | Qty | |
|---|---|---|
| Arduino UNO R3 board | 1 | |
| L293D motor driver | 1 | |
| 5V DC motor | 1 | |
| Breadboard and jumper wires | 1 |
Step by Step
Hardware & Software Requirements
<p>You need a few components in addition to an Arduino Uno board and a breadboard for this project. You first need a DC motor with a nominal voltage close to 5V so it can be powered by the Arduino board alone. In this project, we possibly want to control the motor in both directions, because we want to control curtains up and down for example. For this reason, we cannot simply use a transistor to command the motor, as it will only allow to control the motor in one fixed direction. We will use a small integrated circuit, the L293D motor driver.</p><p>On the software side, you first need the Arduino IDE to be installed on your computer. If you don't have it already, you can find it <a href="https://arduino.cc/en/main/software">here</a>. To control the motor directly from a webpage like we are going to do at the end of the tutorial, you'll also need the <a href="https://github.com/marcoschwartz/aREST">aREST library</a>. To install a library, just download the required folder, and put it into your /Arduino/libraries/ folder.</p><p>You will also need to have Node.js installed on your computer, which we will use to build the interface for this project. You can download it from the official website: <a href="https://nodejs.org/">http://nodejs.org</a></p>

Hardware Configuration
<p>In this project, we need to configure to correctly wire the motor and the motor driver IC. You need to plug the L293D motor driver first, in the middle of your breadboard. You can start by connecting the power for this integrated circuit: connect pins 8 and 9 to the 5V of the Arduino board, and the pin 5 to the ground pin of the Arduino board. Then, there are 3 inputs pin we need to connect, and 2 output pins.</p><p>The output part is easy: you just need to connect the two output pins to the terminal of the DC motor. The output pins we want to use are pins 3 and 6. The first input pin to connect is pin number 1, which is called the Enable pin. This is the pin we will use to set the motor on and off, and to change the speed of the motor. Connect this pin to the pin number 6 of the Arduino board. Finally, we want to connect pins number 2 and 7 of the L293D to pins number 4 and 5 of the Arduino board. These pins will be used to change the direction of the motor.</p><img src="https://s3-us-west-2.amazonaws.com/purepress/files/J6EM2bgXfxenvfEXY-original.png" />
Testing Individual Parts
<p>We need to make sure that the L293D circuit is working correctly, before doing any remote command operation. To check that, we will do something quite simple, like making the motor accelerate & start again continuously. This is the sketch we will use:</p><pre>// Define motor pins int motorPinPlus = 4; int motorPinMinus = 5; int motorPinEnable = 6; void setup() { pinMode(motorPinPlus, OUTPUT); pinMode(motorPinMinus, OUTPUT); pinMode(motorPinEnable, OUTPUT); Serial.begin(9600); } void loop() { // Define speed int motor_speed; // Accelerate forward for (motor_speed = 0; motor_speed < 250; motor_speed++) { setMotor(true, motor_speed); delay(10); } } // Function to control the motor void setMotor(boolean forward, int motor_speed) { digitalWrite(motorPinPlus, forward); digitalWrite(motorPinMinus, !forward); analogWrite(motorPinEnable, motor_speed); }</pre><p>The core of this sketch is the function setMotor, and we will also use it in the rest of this project. It takes two inputs, the direction and the speed. The first step consists in doing two digitalWrite() operations to set the direction of the motor: one pin of the L293D circuit will receive 5V, and the other one will be set at 0V. Finally, we use the analogWrite() command on the enable pin to change the motor speed using PWM.</p><p>Upload the sketch above to the Arduino board, and the motor should successively accelerate and then start again from zero at some point. If everything is going well at that point, you can move to the next part of this tutorial.</p>
Putting it All Together - Arduino Sketch
<p>At this point, you know how to control the DC motor from the Arduino. Let's now put everything we know together and interface the system we designed in this part to the web. There are three different software parts we have to develop. First, we need to slightly modify the Arduino sketch so that it can receive data from the host computer. Then, we will use Node.js to have a nice web interface for the project.</p><p>The Arduino sketch relies on the aREST instance to receive commands. For example, to set a given motor speed, we will make a call from the computer like:</p><pre>/analog/6/200</pre><p>We will make similar calls from the computer interface to control the other motor pins, for example the direction. If you want to learn more about this REST API for Arduino, I invite you to read the corresponding article.</p>
Building the Web Interface with Node.js
<p>The final part of this section consists in designing the web interface so you can remotely set the speed of the motor. To do so, we will use Node.js which allows to code server-side applications in Javascript. We basically need to code 3 files: the main Node.js file, the interface itself, and some Javascript to handle the clicks on the interface.</p><p>First, the Node.js file starts by importing & configuring the Express module:</p><pre>// Module var express = require('express'); var app = express(); // Define port var port = 3000; // View engine app.set('view engine', 'jade'); // Set public folder app.use(express.static(__dirname + '/public')); // Serve interface app.get('/', function(req, res){ res.render('dashboard'); });</pre><p>Then, we import the Node-aREST module, which will handle all the communications between our server & the Arduino board:</p><pre>var rest = require("arest")(app);</pre><p>In this file, we also need to define on which Serial port the Arduino board is connected:</p><pre>rest.addDevice('serial','/dev/tty.usbmodem1a12121',115200);</pre><p>Of course, you need to change this value with your own Serial port, which you can find in your Arduino IDE under Tools>Port.</p>

Creating the User Interface
<p>The interface file is stored in dashboard.jade. Jade is basically a language that simplifies writing HTML code. The Jade file contains the description of the interface, which is then rendered by Node.js & Express. This is the Jade file for this project, which simply defines two buttons (for directions) & one slider (for the motor speed):</p><pre>h1 Motor Control .row.voffset .col-md-4 button.btn.btn-block.btn-lg.btn-primary#1 Forward .col-md-4 button.btn.btn-block.btn-lg.btn-danger#2 Reverse .col-md-4 input(type='range',min='0',max='255',value='0',id='motorspeed')</pre><p>Finally, we need to add a Javascript client script to handle the clicks on the buttons & on the slider. This is the code for the 'Forward' button:</p><pre>$("#1").click(function() { $.get('/motor_control/digital/4/1', function() { $.get('/motor_control/digital/5/0'); }); });</pre><p>We can see that we set pin 4 to HIGH, and pin 5 to LOW. The other button does exactly the opposite, therefore making the motor going in the other direction. To set the speed of the motor, we detect when the slider is released via a mouseup() event:</p><pre>$("#motorspeed").mouseup(function(){ // Get speed speed = $("#motorspeed").val(); // Send command $.get("/motor_control/analog/6/" + speed); });</pre><img src="https://makecademy.com/cdn/storage/Images/ZWx6xausZpqRqRu46/original/ZWx6xausZpqRqRu46.png" />
Testing the Complete Project
<p>We can now test the project. Of course, you can find all the files on the GitHub repository of this tutorial. First upload the remote_motor sketch to the Arduino board. Then, make sure you downloaded all the interface files, and set your own Serial port in the main app.js file.</p><p>Go to this folder via a terminal, and type:</p><pre>npm install</pre><p>And then launch the app with:</p><pre>node app.js</pre><p>You can then go to your favorite web browser and type:</p><pre>localhost:3000</pre><p>You should see the interface being displayed. You can just play it with by first setting a direction, and then using the slider to change the speed. The motor should react accordingly. You can also play with your smartphone or tablet and connect directly to the IP of your computer, and you can then change the speed of the motor directly from your mobile device.</p>
How to Go Further
<p>This is an important tutorial, because it is essential to learn how to control an actuator like a DC motor remotely. For this reason, it is really important to understand all the different parts of the tutorial and I really invite you to try it at your home with your own components. You can also modify the code to add more complex behaviour to your motor. For example, you can modify the interface to introduce a delay between the moment you change the motor direction and the moment the motor actually changes direction.</p><p><strong>Update 18/11/14:</strong> The tutorial has been updated to make use of the aREST framework, which implements a REST API for Arduino. It greatly simplified the code to control the motor from the web page. For example, it completely removed the need of an intermediate language (Python) and the need to store data in text files.</p>
Wrapping Up
This is an important tutorial, because it is essential to learn how to control an actuator like a DC motor remotely. You have learned how to interface an Arduino board with a web server using Node.js, and how to create a simple web interface to control physical hardware. These concepts are fundamental to building IoT and home automation projects. With these skills, you can now extend this project to control multiple motors, add sensors, or integrate with more complex systems. The REST API approach used here is scalable and can be adapted to many different applications.