Wifi Controlled Robot Car Using Nodemcu and Blynk

Hi guys,In this post i'm going to explain in depth how to control robot car using Nodemcu and blynk.To program nodemcu there is a seperate ide but in this project we are programing it using arduino ide, hence the ide thinks that we are programing for arduino so before programming we have to know about the pinouts of nodemcu corresponding to arduino.Above is the image through which you can understand the pinout .Eg: If we use D0 pin of Nodemcu as output ,we have to declare pin 16 as an output in arduino ide.

After knowing the pinouts now let us jump in to the hardware and coding section

Supplies:

  • 1 x Nodemcu
  • 1 x Lm298 motor driver
  • 2 x Bo motors
  • 3 x wheels
  • jumper wires
  • 9v battery
  • 5v powerbank/battery

Step 1: Hardware Connection:

Getting to Know Your L298N Dual H-Bridge Motor Controller Module .An H-Bridge is a circuit that can drive a current in either polarity and be controlled by Pulse Width Modulation (PWM). What is Pulse Width Modulation? Pulse Width Modulation is a means in controlling the duration of an electronic pulse. Motors will last much longer and be more reliable if controlled through PWM.

  • Specifications:
  • Double H bridge Driver Chip: L298N
  • Logical voltage: 5V
  • Drive voltage: 5V-35V
  • Logical current: 0-36mA
  • Drive current: 2A (MAX single bridge)
  • Max power: 25W
  • Note: Built-in 5v power supply, when the driving voltage is 7v-35v. But i recommend you not to power up your microcontroller with on-board 5v power supply.

1:connect the bo motor to the motor driver as shown in the image above

2: connect the motor driver to nodemcu as given below:

  • GPIO2(D4)- IN3
  • GPIO15(D8)-IN1
  • GPIO0(D3)-IN4
  • GPIO13(D7)-IN2
  • GPIO14(D5)-Enb-A
  • GPIO12(D6)-Enb-B

3:Supply Connections :

  • +12v - Positive terminal of the battery to be connected.
  • GND - Ground terminal of the battery to be connected.
  • +5v - +5v input (unnecessary if your power source is less than +7v to +9v, if the power source is greater than +9v to +35v then it can act as a 5v out)
  • 4:Input Connections :
  • (Motor A) ENA - Enables PWM signal for Motor A
  • IN1 -Enable Motor A
  • IN2 - Enable Motor A
  • (Motor B) ENB - Enables PWM signal for Motor B
  • IN3: Enable Motor B
  • IN4: Enable Motor B

Note : Make sure you have all of your grounds connected together. (NodeMCU, Power Source, and Motor Driver)The PWM Pins are unnecessary if you do not want to control PWM features.

click to know in detail

Step 2: Software Connection

  1. Blynk setup
  2. arduino setup

Blynk setup:

  • Open the blynk app
  • click on new project
  • select the Nodemcu board and wifi connection
  • click on the plus symbol present on the right corner of app and select joystick and the slider
  • now click on the joystick select the virtual pin v1 because i have written the code for v1 and slide the button from split to merge
  • set the value range from 0-1023 because it helps in easy control of robot for larger values
  • now select the slider , select the virtual pin v2,and set values from 0 - 1023.

Arduino IDE setup:

  • Click on the Icon to open the Arduino window
  • Open the File and click on the Preferences
  • Adding ESP8266 Board Manager
  • In the Additional Boards Manager enter below URL. http://arduino.esp8266.com/stable/package_esp8266... As highlighted in the figure and enter OK.
  • Selecting Board

Now open the tools in that select Board: “Arduino/Genuino Uno” and click on the Boards Manager as shown in the figure

  • ESP8266 Board Package

The Boards Manager window opens, scroll the window page to bottom till you see the module with the name ESP8266. Once we get it, select that module and select version and click on the Install button. When it is installed it shows Installed in the module as shown in the figure and then close the window.

  • Selecting ESP8266 Arduino Board

To run the esp8266 with Arduino we have to select the Board: “Arduino/Genuino Uno” and then change it to NodeMCU 1.0 (ESP-12E Module) or other esp8266 modules depending on what you have .This can be done by scrolling down, as shown in the figure

  • Connecting ESP8266 to the PC

Now Let’s connect the ESP8266 module to your computer through USB cable as shown in the figure. When module is connected to the USB, COM port is detected eg: here COM5 is shown in the figure.

Step 3: THE CODE:

#include<ESP8266WiFi.h>
#include<BlynkSimpleEsp8266.h>
/* define L298N or L293D motor control pins */
int leftMotorForward = 2;     /* GPIO2(D4) -> IN3   */
int rightMotorForward = 15;   /* GPIO15(D8) -> IN1  */
int leftMotorBackward = 0;    /* GPIO0(D3) -> IN4   */
int rightMotorBackward = 13;  /* GPIO13(D7) -> IN2  */
int z;
* define L298N or L293D enable pins */
int rightMotorENB = 14; /* GPIO14(D5) -> Motor-A Enable */
int leftMotorENB = 12;  /* GPIO12(D6) -> Motor-B Enable */
// You should get Auth Token in the Blynk App
// Go to the Project Settings (nut icon).
// Use your own WiFi settings
char auth[] = "enter auth token sent to email";
char ssid[] = "enter network name";
char pass[] = "enter network password";
// neutral zone settings for x and y
// joystick must move outside these boundary numbers to activate the motors
// makes it a little easier to control the wifi car
int minRange = 312;
int maxRange = 712;
// analog speeds from 0 (lowest) - 1023 (highest)
// 3 speeds used -- 0 (noSpeed), 350 (minSpeed), 850 (maxSpeed).
// use whatever speeds you want...too fast made it a pain in the ass to control
int minSpeed = 450;
int maxSpeed = 1023;
int noSpeed = 0;
void moveControl(int x, int y)
{
  // movement logic
  // move forward   // 
  if(y >= maxRange && x >= minRange && x <= maxRange) 
  {
  analogWrite(leftMotorENB,z);
  analogWrite(rightMotorENB,z);  
  digitalWrite(leftMotorForward,HIGH);
  digitalWrite(rightMotorForward,HIGH);
  digitalWrite(leftMotorBackward,LOW);
  digitalWrite(rightMotorBackward,LOW);
  }
//MOVE FORWARD RIGHT
 else if(x <= minRange && y >= maxRange )
  {
 
  digitalWrite(leftMotorENB,maxSpeed);
  digitalWrite(rightMotorENB,minSpeed); 
  digitalWrite(leftMotorForward,HIGH);
  digitalWrite(rightMotorForward,HIGH);
  digitalWrite(rightMotorBackward,LOW);
  digitalWrite(leftMotorBackward,LOW);  
}
// move  right
  else if(x >= maxRange && y >= minRange && y<=maxRange)   
  { analogWrite(leftMotorENB,z);
   analogWrite(rightMotorENB,z);  
  digitalWrite(leftMotorForward,HIGH);
  digitalWrite(rightMotorForward,LOW);
  digitalWrite(rightMotorBackward,HIGH);
  digitalWrite(leftMotorBackward,LOW);
 
  }
// move forward LEFT
 
  else if(x <= minRange && y >= maxRange )
  { 
  analogWrite(leftMotorENB,minSpeed);
  analogWrite(rightMotorENB,maxSpeed); 
  digitalWrite(leftMotorForward,HIGH);
  digitalWrite(rightMotorForward,HIGH);
  digitalWrite(rightMotorBackward,LOW);
  digitalWrite(leftMotorBackward,LOW);  
}
// moveleft
  else if(x <= minRange && y >= minRange && y <= maxRange)
  {
 
   analogWrite(leftMotorENB,z);
analogWrite(rightMotorENB,z);   
  digitalWrite(leftMotorForward,LOW);
  digitalWrite(rightMotorForward,HIGH);
  digitalWrite(rightMotorBackward,LOW);
  digitalWrite(leftMotorBackward,HIGH);  
}
// neutral zone
  else if(y < maxRange && y > minRange && x < maxRange && x > minRange)
  {
  digitalWrite(leftMotorENB,LOW);
  digitalWrite(rightMotorENB,LOW);
  digitalWrite(leftMotorForward,LOW);
  digitalWrite(leftMotorBackward,LOW);
  digitalWrite(rightMotorForward,LOW);
  digitalWrite(rightMotorBackward,LOW);
  }
// move back
  else if(y <= minRange && x >= minRange && x <= maxRange)
  {
    analogWrite(leftMotorENB,z);
analogWrite(rightMotorENB,z);  
  digitalWrite(leftMotorBackward,HIGH);
  digitalWrite(rightMotorBackward,HIGH);
  digitalWrite(leftMotorForward,LOW);
  digitalWrite(rightMotorForward,LOW);
  }
// move back and right
 else if(y <= minRange && x <= minRange)
  {
  analogWrite(leftMotorENB,maxSpeed);
analogWrite(rightMotorENB,minSpeed); 
  digitalWrite(leftMotorForward,LOW);
  digitalWrite(rightMotorForward,LOW);
  digitalWrite(rightMotorBackward,HIGH);
  digitalWrite(leftMotorBackward,HIGH);  
  }
// move back and left
  else if(y <= minRange && x >= maxRange)
 {
 analogWrite(leftMotorENB,minSpeed);
 analogWrite(rightMotorENB,maxSpeed); 
  digitalWrite(leftMotorForward,LOW);
  digitalWrite(rightMotorForward,LOW);
  digitalWrite(rightMotorBackward,HIGH);
  digitalWrite(leftMotorBackward,HIGH);  
  }
}
void setup()
{
  // initial settings for motors off and direction forward
  pinMode(leftMotorForward, OUTPUT);
  pinMode(rightMotorForward, OUTPUT); 
  pinMode(leftMotorBackward, OUTPUT);  
  pinMode(rightMotorBackward, OUTPUT);
 /* initialize motor enable pins as output */
  pinMode(leftMotorENB, OUTPUT); 
  pinMode(rightMotorENB, OUTPUT);
Blynk.begin(auth, ssid, pass);
}
 
 
void loop()
{
   Blynk.run();
}
BLYNK_WRITE(V1)
{
    
  int x = param[0].asInt();
  int y = param[1].asInt();
  
  moveControl(x,y); 
 }
BLYNK_WRITE(V2)
{
    
  z = param[0].asInt();
}



Step 4: CODE BREAKDOWN:

If you are not familiar with coding its ok i'm going to explain you guys in detail how the code works.

  • Before jumping into the coding we have to understand how the joystick works.
  • joystick sends 2 values, one when the joystick is moved along xaxis and the second one is when the joystick is moved along yaxis
  • The values which joystick sends range between 0 -1023
  • eg: when the joystick is moved forward it sends the value 1023 ,and when joystick is moved backward it send the value 0 , and when the joystick is at the centre it sends the value 512( ie.. half of 1023)
  • The same procedure follows for z axis but instead of forward and backward it sends the value when the joystick is moved right or left (ie.. 0 when moved left ,1023 when moved right and 512 when present in center)

The main idea of this code is as follows:

  1. when the joystick is moved forward( y =1023) the wifi module sends signal to motor driver which drives both the motors in clockwise (forward) direction
  2. when the joystick is moved backward(y=0) it drives the motors in counter clockwise making the robocar to move in backward direction
  3. The same repeats for right and left

For better understanding i have explained how the code works in the code section.

For more details visit this link -click to understand the code in detail

1 Hour Challenge

Participated in the
1 Hour Challenge

Share

    Recommendations

    • Growing Beyond Earth Maker Contest

      Growing Beyond Earth Maker Contest
    • Barbecue Challenge

      Barbecue Challenge
    • Sensors Contest

      Sensors Contest

    4 Discussions

    0
    None
    daxpatel

    4 weeks ago

    hii
    great project.
    just 1 question , this code is work in ESP32 ?

    2 replies
    0
    None
    Manitesladaxpatel

    Reply 21 days ago

    It may not work because we need different libraries for esp32

    0
    None
    daxpatelManitesla

    Reply 21 days ago

    Thanks for reply.i will try with different one.

    0
    None
    seamster

    4 weeks ago

    Thank you for sharing! : )