ad space

Two wheels WiFi control NodeMCU Car

 To get more experience with robotics, I have started to make a two wheels WiFi control NodeMCU car. This two wheel car is quite popular among Arduino users and there are lots of tutorials and codes online and in YouTube. I had always wanted to make these four and two wheels remotely controllable cars. Since I have already made a Bluetooth Arduino RC Car and an obstruction avoid self driving cars which used four wheels, I thought it would make sense to make a two wheels WiFi controllable car. For this I will use my old NodeMCU 8266 module that I have used in many tutorials on it, for example, NodeMCU LED brightness with Potentiometer is a good starting point to learn it. Doing this project, I think I will have added experience of using WiFi with two wheels cars. So here I will record here how to make a WiFi controlled car with NodeMCU

So I bought a two wheels chassis and wheels online, but unfortunately the package did not contain the spacers. I complained to the seller but I don't have so I thought I will make the spacer on my own using some pen or dot pen materials or find some other solutions for the spacers. Below is the three wheels parts that I have, NodeMCU module, battery holder and batteries, L298N motor driver module which are also required. 

Materials required for Wifi Car Using NodeMCU

This is optional but if you want to build into the car a recharging system then you will also need the recharging circuit. I have opted to use LM317 adjustable voltage regulator. Below is shown LM317 lithium ion battery charger materials which includes, LM317 IC, 200Ohm resistor, 10KOhm potentiomter, three way switch, 0.1uF and 1uF capacitors, PCB power jack and pref-board.

LM317 lithium ion batery charger materials

 Next comes the assembly. I first soldered the wires to the DC motors.

Then I assembled the wheels with the chassis.

Next I have to put power supply, NodeMCU module, onto the chassis. Shown below are some pictures taken during the assembling of the parts.

car pictures

The lithium ion battery charger circuit in the above pictures was a DIY build. I don't know if this is working. My initial finding is that it seems working. In my four wheel Arduino Bluetooth car I bought and used lithium ion charger module. Here I relied on LM317 Lithium Ion Battery Charger for the robot car.

LM317 Lithium Ion Battery Charger For Robot Car

I have written a separate blog post on how I made this LM317 Lithium Ion Battery Charger For Robot Car which contains the circuit diagram for the LM317 lithium ion batteries charger.

I have recorded video of the whole assembling work of the WiFi robot car.

Two wheels WiFi control Arduino Car Circuit Schematic

I have drawn the following NodeMCU WiFi controlled car circuit schematic using the Proteus Professional electronics design software. For me this software has become my favorite because it is very easy to use and has lots of features. Some of the components shown in the circuit diagram are not available and I simply made those parts.

NodeMCU Mobile WiFi controlled Car Circuit Diagram
In the above WiFi control NodeMCU car circuit diagram, the when the power is applied to the car via the power jack and the switch is in state of charging, the batteries gets charged using the DIY Lithium ion battery charger circuit. We can rotate the potentiometer to output required voltage to charge the batteries in the battery holder. Since the total battery voltage is 4.8V, the 10K potentiometer should be rotated to give output of 4.8V or 5V. The motors are connected to the L298N motor driver module as shown. The 6 control signals to drive and control the motors via the L298N are connected to the ESP8266 NodeMCU pins as shown in the circuit diagram.

ENA to D5
ENB to D6
IN1 to D8
IN2 to D7
IN3 to D4
IN4 to D3

The power supply to the NodeMCU can be directly connected to the +12V pin because our power supply has maxing rating of 4.8V with four 1.2V lithium ion batteries. I have plan to change the battery to use larger battery with more voltages and current. 
 
About the basic dc motor control with NodeMCU see how to control a DC motor using NodeMCU 8266.

Three wheels WiFi control Arduino Car Program

Next we have to write program for the NodeMCU that connects and transfer data from NodeMCU via WiFi. The following the WiFi controllable Arduino car program code that I used.


#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 Right           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>

String command;             //String to store app command state.
int speedCar = 800;         // 400 - 1023.
int speed_Coeff = 3;

const char* ssid = "NodeMCU Car";
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.mode(WIFI_AP);
  WiFi.softAP(ssid);

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
 
 // Starting WEB-server 
     server.on ( "/", HTTP_handleRoot );
     server.onNotFound ( HTTP_handleRoot );
     server.begin();    
}

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);
 }

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);
}

The above code is for controlling a robotic car using an ESP8266-based NodeMCU board. It sets up NodeMCU(ESP8266) WiFi web server that listens for commands sent by a web client (e.g., a mobile app or web browser) to control the movement of the robotic car. Here's a breakdown of the code:

Pin Definitions:

The code defines several constants to specify the GPIO pins connected to various components of the robotic car, including motors and motor drivers:

  • ENA: This is the GPIO pin connected to the enable/speed control of the right motors.
  • ENB: This is the GPIO pin connected to the enable/speed control of the left motors.
  • IN_1, IN_2, IN_3, IN_4: These are GPIO pins connected to various input pins of an L298N motor driver module. These pins are used to control the direction of the motors.
Libraries:

The code includes necessary libraries for Wi-Fi and web server functionality:

  • ESP8266WiFi.h: Library for connecting to Wi-Fi networks with the ESP8266.
  • WiFiClient.h: Library for handling client connections over Wi-Fi.
  • ESP8266WebServer.h: Library for creating a web server on the ESP8266.
Variables and Constants:
  • command: A string variable used to store the received command from the web client.
  • speedCar: An integer variable representing the speed of the car's motors (ranging from 400 to 1023).
  • speed_Coeff: An integer variable representing a speed coefficient used for adjusting the speed of one motor relative to the other.
Wi-Fi Setup:

The setup function initializes the GPIO pins as outputs and sets up Wi-Fi in Access Point (AP) mode with the SSID "NodeMCU Car." It also starts the web server on port 80.

Movement Functions:

There are several functions defined for different movements of the car, such as going forward, backward, turning left or right, and combinations of these movements.

Loop Function:

The main loop function constantly checks for incoming client requests using server.handleClient(). It reads the "State" parameter from the HTTP request to determine the desired action. Depending on the received command, it calls the corresponding movement function or adjusts the speed of the motors.

  • The commands "F," "B," "L," "R," "I," "G," "J," and "H" correspond to forward, backward, left, right, forward-right, forward-left, backward-right, and backward-left movements, respectively.
  • The commands "0" to "9" represent different speed levels for the motors.
  • The command "S" stops the robot.
HTTP_handleRoot Function:

This function is called when the root URL ("/") is accessed. It checks if the "State" parameter is present in the HTTP request and prints its value to the serial monitor. It then sends a response with an empty HTML body.

In summary, this code turns the ESP8266 into a web server that listens for commands to control the movements and speed of a robotic car using a web interface. It provides a straightforward way to control the car's actions remotely via a web browser or a mobile app.

2 Comments

Previous Post Next Post