Menu Close

NodeMCU Motor Shield

This tutorial of Robo India explains the working of NodeMCU Motor shield.

1. Introduction 

The NodeMCU Motor Shield is a driver module for motors that allows you to use to control the working speed and direction of the motor .This NodeMCU Motor shield is designed and developed based on ESP-12E from ESP8266, which can be controlled by mobile, PC etc

Robo India offers compatible NodeMCU Motor shield that can run 2 DC motor and 9-12v battery.

 1.2 Hardware required

S.No.ItemQuantity
1Nodemcu Motor Shield 1
2DC Motor 2
3Male to male jumper wire 8
46xAA battery holder 1

2. Connections

1) The NodeMCU motor shield digital pins of D0(GPIO 16) and D1(GPIO 5) is connecting to Left Motor

2) The NodeMCU motor shield digital pins of D2(GPIO 4) and D3(GPIO 0) is connecting to Right Motor

3) The 9-12v photocell battery is connecting to GND and + with NodeMCU Motor Shield.

Make the connection as shown above.

2.1 Mobile app

Download the APK file from here to install in your android phone from the below link.

You may download the APK file for the app here.

or

Download the .aia file from the below link to make any changes in the app.

Download the MIT App Inventor project (.aia) file.

3. Programming 1:

Once the circuit part is done, NodeMCU motor shield is needed to be programmed. Here is the code to run this circuit on NodeMCU motor shield.

You may download this code (Arduino Sketch) from here.

/* Create a WiFi access point and provide a web server on it to receive command to control motor. */

#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>

/* Set these to your desired credentials. */
const char *ssid = "My_Robot";
const char *password = "iamnotarobot";

ESP8266WebServer server(80);

/* Just a little test message.  Go to http://192.168.4.1 in a web browser
 * connected to this access point to see it.
 */
void handleRoot() {
  server.send(200, "text/plain", "hello from Robot!");
}

void motor_forward(){
    digitalWrite(16, 1);
    digitalWrite(5, 0);
    digitalWrite(4, 1);
    digitalWrite(0, 0);
  }
void motor_stop(){
    digitalWrite(16, 0);
    digitalWrite(5, 0);
    digitalWrite(4, 0);
    digitalWrite(0, 0);
  }
void motor_back(){
    digitalWrite(16, 0);
    digitalWrite(5, 1);
    digitalWrite(4, 0);
    digitalWrite(0, 1);
  }

void motor_left(){
    digitalWrite(16, 0);
    digitalWrite(5, 1);
    digitalWrite(4, 1);
    digitalWrite(0, 0);
  }
void motor_right(){
    digitalWrite(16, 1);
    digitalWrite(5, 0);
    digitalWrite(4, 0);
    digitalWrite(0, 1);
  }


void setup() {
  // prepare Motor Output Pins
  pinMode(16, OUTPUT);
  digitalWrite(16, 0);
  
  // prepare GPIO5 relay 1
  pinMode(5, OUTPUT);
  digitalWrite(5, 0);
  
  pinMode(4, OUTPUT);
  digitalWrite(4, 0);
  
  pinMode(0, OUTPUT);
  digitalWrite(0, 0);

  
	delay(1000);
	Serial.begin(115200);
	Serial.println();
	Serial.print("Configuring access point...");
	/* You can remove the password parameter if you want the AP to be open. */
	WiFi.softAP(ssid, password);

	IPAddress myIP = WiFi.softAPIP();
	Serial.print("AP IP address: ");
	Serial.println(myIP);

 
	server.on("/", handleRoot);
 
  server.on("/inline", []() {
    server.send(200, "text/plain", "this works as well");
  });

  server.on("/fw", []() {
    motor_forward();
    server.send(200, "text/plain", "Forward");
  });
  server.on("/bk", []() {
    motor_back();
    server.send(200, "text/plain", "Back");
  });

  server.on("/st", []() {
    motor_stop();
    server.send(200, "text/plain", "Stop");
  });
  server.on("/lt", []() {
    motor_left();
    server.send(200, "text/plain", "Left");
  });
  server.on("/rt", []() {
    motor_right();
    server.send(200, "text/plain", "Right");
  });

	server.begin();
	Serial.println("HTTP server started");


  
}

void loop() {
	server.handleClient();
}

Output-1

After the connection you will copy and paste this code in Arduino IDE. Than go to the mobile phone and connect with wifi (“My_Robot”) and password(“iamnotarobot”).Than open the app in your mobile phone and set the IP address is mention in your code .If you press forward button the left motor move forward direction and the right motor move backward direction .If you press the left button the left motor move forward direction and the right motor move backward direction and so on.And you will see that all the given command it will show in status button of the app.

If you press the accelerate controller button, the movement of your mobile phone will rotate the connected motors. The motors will move in left, right, backward, forward direction according to the movements of your mobile phone. The other buttons will not work, when the accelerate Controller is ON.

Programming 2:

Once the circuit part is done, NodeMCU motor shield is needed to be programmed. Here is the code to run this circuit on NodeMCU motor shield.

You may download this code (Arduino Sketch) from here.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

const char* ssid = "your_network_ssid";
const char* password = "your_network_password";

ESP8266WebServer server(80);

void handleRoot() {
  server.send(200, "text/plain", "hello from Robot!");
}

void motor_forward(){
    digitalWrite(16, 1);
    digitalWrite(5, 0);
    digitalWrite(4, 1);
    digitalWrite(0, 0);
  }
void motor_stop(){
    digitalWrite(16, 0);
    digitalWrite(5, 0);
    digitalWrite(4, 0);
    digitalWrite(0, 0);
  }
void motor_back(){
    digitalWrite(16, 0);
    digitalWrite(5, 1);
    digitalWrite(4, 0);
    digitalWrite(0, 1);
  }

void motor_left(){
    digitalWrite(16, 0);
    digitalWrite(5, 1);
    digitalWrite(4, 1);
    digitalWrite(0, 0);
  }
void motor_right(){
    digitalWrite(16, 1);
    digitalWrite(5, 0);
    digitalWrite(4, 0);
    digitalWrite(0, 1);
  }

void setup(void){

  // prepare Motor Output Pins
  pinMode(16, OUTPUT);
  digitalWrite(16, 0);
  
  // prepare GPIO5 relay 1
  pinMode(5, OUTPUT);
  digitalWrite(5, 0);
  
  pinMode(4, OUTPUT);
  digitalWrite(4, 0);
  
  pinMode(0, OUTPUT);
  digitalWrite(0, 0);
  
  
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin("esp8266")) {
    Serial.println("MDNS responder started");
  }

  server.on("/", handleRoot);

  server.on("/fw", []() {
    motor_forward();
    server.send(200, "text/plain", "Forward");
  });
  server.on("/bk", []() {
    motor_back();
    server.send(200, "text/plain", "Back");
  });

  server.on("/st", []() {
    motor_stop();
    server.send(200, "text/plain", "Stop");
  });
  server.on("/lt", []() {
    motor_left();
    server.send(200, "text/plain", "Left");
  });
  server.on("/rt", []() {
    motor_right();
    server.send(200, "text/plain", "Right");
  });

  server.begin();
  Serial.println("HTTP server started");
}

void loop(void){
  server.handleClient();
}

Output-2

After the connection you will copy and paste this code in Arduino IDE. Write the correct ssid and password. Then upload the code. Open the serial monitor.Now, you are connected with your wifi and it display the IP address on  serial monitor.

Then open the app and set the IP address .If you press forward button the left motor will moves  in forward direction and the right motor will moves in backward direction .If you press the backward button, then the right motor moves in forward direction and the left motor moves in backward direction and so on. And you will see that all the given command it will show in status button of the app.

When you press the accelerate controller the button is on .The movement of your mobile phone will rotate the connected motors.

Note:-When you open this app the mobile phone data connection is must be off

If you have any query please write us at support@roboindia.com

Full Assembly and programming instructions of Alpha Robot Kit.

Unboxing of Alpha Robot Kit.

Thanks and Regards
Content Development Team 
Robo India
https://roboindia.com

Leave a Reply