Robotic Car using WiFi

RC assembled with NodeMCU

A robotic car is an autonomous car which is navigated and maneuvered by a computer without needing any human intervention under the range of driving situations and conditions. It is capable of sensing its environment and moving safely with little or no human input. 

Well, many sensors can be attached to make the car work smarter. Robotic car is abbreviated as RC. Control RC by using the Node microcontroller unit (NodeMCU) also known as ESP8266. It comes in different modules. Therefore the module for programming RC, use NodeMCU 1.0 12E modules to make it WiFi enabled. Other microcontroller boards can perform the same action depending upon their compatibility. 

 

Comparison between NodeMCU and Arduino

ESP8266 provides more power than Arduino UNO. It features an integrated WiFi module and a micro USB port. In addition, it can function with a voltage between 7v – 12v. Both are programmed with Arduino IDE (Integral development environment).

NodeMCU Pinout

How Does a Smart Car Work?

Once connected to ESP8266, it creates a server with the provided SSID and passcode. Furthermore, a signal may be generated/passed using the URL, but it may not appear professional.
Install the app named NodeMCU_Car from the Play store or App Store to your device.

 After the installation is complete, enter the Internet Protocol (IP) address for the application. This IP address will be visible on the serial monitor after code verification and upload.

RC with NodeMCU

Now that we need to understand how the RC works with NodeMCU. DC motors cannot be connected to NodeMCU directly. So use an H-bridge (L298N) for steady connections.

Requirements

  • Chassis of a robot car.
  • 4 DC motors
  • L298N H-bridge
  • NodeMCU 1.0 12E modules
  • Pin to hole jumper wires
  • 9v battery  

Wiring Connections

Now make the connections of L298N with NodeMCU.  Follow the instructions as follows,

ESP8266 Pins                                                                 H-Bridge Pins

         D3                                                                                  input 4

         D4                                                                                  input 3

         D5                                                                                  enable A

         D6                                                                                  enable B

         D7                                                                                   input 2

         D8                                                                                   input 1

        GND                                                                                  GND

 Connect 9v battery to nodeMCU and H – bridge. 

NodeMCU                                                                        9v Battery                                                           H-Bridge

    Gnd                                                                               Black wire

     Vin                                                                                Red wire                                                                 12V 

 

H-Bridge Pin Out

The pin out diagram for H – bridge is as follows, 

Note

Well H-bridge seems to be complex, but it is not. There are 4 OUT connectors (OUT1, OUT2, OUT3, and OUT4). If you are using a 2 wheel drive robotic car, add the wires of the DC motor to the out pins on H-Bridge. Connections of the motors will be done as follows,

  1. Black wire of DC motor 1 to OUT1 and of DC motor 2 to OUT 3
  2. Red wire of motor 1 to OUT2 and of motor 2 to OUT 4

Now, if you are using a 4 wheel drive robotic car, make the connections the same way. Connect wires of similar colors on both the sides together (connecting the red wire with red and black wire with the black). After which insert them to the OUT connectors on H- bridge. Do exactly the same with the right side as well. 

Wire the two motors parallel to each other. The robotic car will not work if you have not connected the ground of H-bridge to the ground of your microcontroller board (NodeMCU). 

Code

Now merge the codes above and then upload it to your board. Open the serial monitor after which note the IP address. Go to the NodeMCU_Car application and enter the IP address. The application will then start to communicate with the robotic car using WiFi as the communication medium. So the buttons on the application will help to move the robot in the right direction.

Not Moving In Right Direction

Let suppose when you press the forward button on the application, it shall move forward but it shows ambiguous movement. The only way to solve it is, by inverting the place of the wiring for your DC motors on the H-bridge (alike this that invert the wirings for OUT pins). 

				
					#define ENA   14          // Enable/speed motors Right        GPIO14(D5)

#define ENB   12          // Enable/speed motors Left         GPIO12(D6)

#define IN_1  15          // L298N in1 motors Rightx          GPIO15(D8)

#define IN_2  13          // L298N in2 motors Right           GPIO13(D7)

#define IN_3  2           // L298N in3 motors Left            GPIO2(D4)

#define IN_4  0           // L298N in4 motors Left            GPIO0(D3)

 

#include <ESP8266WiFi.h>

#include <WiFiClient.h> 

#include <ESP8266WebServer.h>

 

//IPAddress staticIP612_61(192,168,100,70);

//IPAddress gateway612_61(192,168,100,1);

//IPAddress subnet612_61(255,255,255,0);

 

String command;             //String to store app command state.

int speedCar = 800;         // 400 - 1023.

int speed_Coeff = 3;

 

const char* ssid = "NodeMCU";

const char* password = "TheRedMonster79";

ESP8266WebServer server(80);

 

void setup() {

 

 pinMode(ENA, OUTPUT);

 pinMode(ENB, OUTPUT);  

 pinMode(IN_1, OUTPUT);

 pinMode(IN_2, OUTPUT);

 pinMode(IN_3, OUTPUT);

 pinMode(IN_4, OUTPUT); 

  

  Serial.begin(115200);

  

// Connecting WiFi

// WiFi.config(staticIP612_61, gateway612_61, subnet612_61);

  WiFi.mode(WIFI_AP);

  WiFi.softAP(ssid);

 

 IPAddress myIP = WiFi.softAPIP();

  Serial.print("AP IP address: ");

  //Serial.println(WiFi.localIP());

  Serial.println(myIP);

  

 

 // Starting WEB-server 

     server.on ( "/", HTTP_handleRoot );

     server.onNotFound ( HTTP_handleRoot );

     server.begin();    

}

 

void loop() {

    server.handleClient();

    

      command = server.arg("State");

      if (command == "F") goAhead();

      else if (command == "B") goBack();

      else if (command == "L") goLeft();

      else if (command == "R") goRight();

      else if (command == "I") goAheadRight();

      else if (command == "G") goAheadLeft();

      else if (command == "J") goBackRight();

      else if (command == "H") goBackLeft();

      else if (command == "0") speedCar = 400;

      else if (command == "1") speedCar = 470;

      else if (command == "2") speedCar = 540;

      else if (command == "3") speedCar = 610;

      else if (command == "4") speedCar = 680;

      else if (command == "5") speedCar = 750;

      else if (command == "6") speedCar = 820;

      else if (command == "7") speedCar = 890;

      else if (command == "8") speedCar = 960;

      else if (command == "9") speedCar = 1023;

      else if (command == "S") stopRobot();

}

 

void HTTP_handleRoot(void) {

 

if( server.hasArg("State") ){

       Serial.println(server.arg("State"));

  }

  server.send ( 200, "text/html", "" );

  delay(1);

}




void goAhead(){ 

 

      digitalWrite(IN_1, LOW);

      digitalWrite(IN_2, HIGH);

      analogWrite(ENA, speedCar);

 

      digitalWrite(IN_3, LOW);

      digitalWrite(IN_4, HIGH);

      analogWrite(ENB, speedCar);

  }

 

void goBack(){ 

 

      digitalWrite(IN_1, HIGH);

      digitalWrite(IN_2, LOW);

      analogWrite(ENA, speedCar);

 

      digitalWrite(IN_3, HIGH);

      digitalWrite(IN_4, LOW);

      analogWrite(ENB, speedCar);

  }

 

void goRight(){ 

 

      digitalWrite(IN_1, HIGH);

      digitalWrite(IN_2, LOW);

      analogWrite(ENA, speedCar);

 

      digitalWrite(IN_3, LOW);

      digitalWrite(IN_4, HIGH);

      analogWrite(ENB, speedCar);

  }

 

void goLeft(){

 

      digitalWrite(IN_1, LOW);

      digitalWrite(IN_2, HIGH);

      analogWrite(ENA, speedCar);

 

      digitalWrite(IN_3, HIGH);

      digitalWrite(IN_4, LOW);

      analogWrite(ENB, speedCar);

  }

 

void goAheadRight(){

      

      digitalWrite(IN_1, LOW);

      digitalWrite(IN_2, HIGH);

      analogWrite(ENA, speedCar/speed_Coeff);

 

      digitalWrite(IN_3, LOW);

      digitalWrite(IN_4, HIGH);

      analogWrite(ENB, speedCar);

   }

 

void goAheadLeft(){

      

      digitalWrite(IN_1, LOW);

      digitalWrite(IN_2, HIGH);

      analogWrite(ENA, speedCar);

 

      digitalWrite(IN_3, LOW);

      digitalWrite(IN_4, HIGH);

      analogWrite(ENB, speedCar/speed_Coeff);

  }

 

void goBackRight(){ 

 

      digitalWrite(IN_1, HIGH);

      digitalWrite(IN_2, LOW);

      analogWrite(ENA, speedCar/speed_Coeff);

 

      digitalWrite(IN_3, HIGH);

      digitalWrite(IN_4, LOW);

      analogWrite(ENB, speedCar);

  }

 

void goBackLeft(){ 

 

      digitalWrite(IN_1, HIGH);

      digitalWrite(IN_2, LOW);

      analogWrite(ENA, speedCar);

 

      digitalWrite(IN_3, HIGH);

      digitalWrite(IN_4, LOW);

      analogWrite(ENB, speedCar/speed_Coeff);

  }

 

void stopRobot(){  

 

      digitalWrite(IN_1, LOW);

      digitalWrite(IN_2, LOW);

      analogWrite(ENA, speedCar);

 

      digitalWrite(IN_3, LOW);

      digitalWrite(IN_4, LOW);

      analogWrite(ENB, speedCar);

 }