NodeMCU - ESP8266 Robot with Stand Alone Library

I have finally found a solution to the controlling a robot using the ESP8266 Solution without the need of an Arduino development board. I am using the NodeMCU board and a cheap two wheel drive platform and a motor driver adapter board that connects in a manor similar to an Arduino shield. I had help some good advice from various members of the Blynk Community and the Blynk staff that is often active in chat. Special thanks to members Psoro, katzworld and Lichtsignaal. Their assistance greatly reduced the time it would have taken to get to this point of having a working prototype.

Later I will add pictures and video.

I used the Blynk ESP8266 Stand Alone Library, the 2-joystick module in merge mode as well as the code snippet from its example within the Blynk library. The driving method was developed with a combination of a previous code I developed for my RomeoBLE Project and the code of a fellow user name Psoro who posted a code to control the same type of robot with an ESP8266 Chip and an Arduino Nano.

Many tweaks and changes had to be made including several adjustments to the mapping values. Some data had to be retrieved by connecting directly to the ESP8266 with Blynk Stand Alone and testing the motor speed at various levels from 0 1000. For this setup, the effect motor control range is from 450 to 1000. The speed adjustment slider is another idea from Psoro that I will be using a lot with kids programs to keep them from breaking the cars lol.

Here is my code:

#define BLYNK_PRINT Serial    // Prints to serial
#include <ESP8266WiFi.h>      //Calls Blynk ESP8266 Wifi Libary
#include <BlynkSimpleEsp8266.h>//Calls Blynk ESP8266 Library

//Variables are declared at this point before void setup to make variables public and thus accessible throughout the entire code if needed.
int motorA ; //Variables for motor speed.
int motorB ; //Variables for motor speed.
int X=0; //Variable to recieve horizontal analog stick movement data.
int Y=0; //Variable to recieve vertical analog stick movement data.
int factor=0; //Decreases the speed of one motor to increase the differential between the two and thus provide differential steering or a steering wheel like control. 
int maximo=0; //Limits the speed based on a slider widget within the Blynk App.

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "authentication code for blynk"; 

void setup()
{
  // Set console baud rate
  Serial.begin(9600);


 Blynk.begin(auth,"WifiName","WifiPassword"); // Signs into home wifi. 

 pinMode(motorA, OUTPUT); //sets any pin used for motorA as output since pins may be inputs by default.
 pinMode(motorB, OUTPUT); //sets any pin used for motorB as output since pins may be inputs by default.
 pinMode(0,OUTPUT); //sets pin 0 to output since pins may be inputs by default.
 pinMode(2,OUTPUT); //sets pin 2 to output since pins may be inputs by default.

}

 BLYNK_WRITE(V1) //Vertual pin used with 2-axis joystick in merge mode to control robot movement. 
{
  int X1 = param[0].asInt();// Variable to recieve input from parameter 0 position array within V1 that tracks horizontal movement.
  X=X1;
  int Y1 = param[1].asInt();// Variable to recieve input from parameter 1 position array within V1 that tracks vertical movement.
 Y=Y1;
 
}

 BLYNK_WRITE(V0)//Virtual pin used with maximo variable to limit upper boundary or top end of speed.
{
 int vel = param.asInt(); 
 maximo=vel;
}

void loop()
{

  if(X == 128  &&  Y == 128)  //  Writes 0 to all motor pins to Stop vehicle when joystick is in centered x and y position or neutral.
  {
   motorA = 0;
   motorB = 0;
   analogWrite(5, motorA);  
   analogWrite(4, motorA);
   analogWrite(0, motorB);  
   analogWrite(2, motorB);
   } 
    
   if(X > 123 && X < 132 && Y >= 129)   //Forward
  {
    motorA = Y;
    motorB = Y;
    
    motorA = map(motorA, 129,255 , 450,maximo);
    analogWrite(5, motorA);
    digitalWrite(0,LOW);
    motorB = map(motorB, 129,255 , 450,maximo);
    analogWrite(4, motorB);
    digitalWrite(2,HIGH);
  }

 else if(X > 123 && X < 132 && Y <= 127)   //Reverse
  {
    motorA = Y;
    motorB = Y;
    
    motorA = map(motorA, 127,0 , 450,maximo);
    analogWrite(5, motorA);
    digitalWrite(0,HIGH);
    motorB = map(motorB, 127,0 , 450,maximo);//something is wrong with HIGH signal
    analogWrite(4, motorB);
    digitalWrite(2,LOW);
  }

  else if(Y > 123 && Y < 132 && X <= 127)   //Left
  {
    motorA = X;
    motorB = X;
    
    motorA = map(motorA, 127,0 , 450,maximo);
    analogWrite(5, motorA);
    digitalWrite(0,HIGH);
    motorB = map(motorB, 127,0 , 450,maximo);//something is wrong with HIGH signal
    analogWrite(4, motorB);
    digitalWrite(2,HIGH);
  }

   else if(Y > 123 && Y < 132 && X >= 127)   //Right
  {
    motorA = X;
    motorB = X;
    
    motorA = map(motorA, 129,255 , 450,maximo);
    analogWrite(5, motorA);
    digitalWrite(0,LOW);
    motorB = map(motorB, 129,255 , 450,maximo);//something is wrong with HIGH signal
    analogWrite(4, motorB);
    digitalWrite(2,LOW);
  }

  else if(X >= 129 && Y >= 129)   //Forward Right Steering
  {
    motorA = Y;
    motorB = Y;
    factor = X;
    factor= map(factor,129,255, 0,200);
    
    motorA = map(motorA, 129,255 , 450,maximo);
    analogWrite(5, motorA);
    digitalWrite(0,LOW);
    motorB = map(motorB, 129,255 , 450,maximo);
    analogWrite(4, (motorB-factor));
    digitalWrite(2,HIGH);
  }

  else if(X <= 127 && Y >=129)   //Forward Left Steering
  {
    motorA = Y;
    motorB = Y;
    factor = X;
    factor= map(factor,127,0, 0,150);
    
    motorA = map(motorA, 129,255 , 450,maximo);
    analogWrite(5, (motorA-factor));
    digitalWrite(0,LOW);
    motorB = map(motorB, 129,255 , 450,maximo);
    analogWrite(4, motorB);
    digitalWrite(2,HIGH);
  }
  
  Blynk.run();
}

Here some pictures:





7 Likes

Cool project! :light_rail:
I like the simplicity and assumably low cost. How much have you spent on it?

Thanks for sharing it with us.

That looks very cool! I can’t wait to see the video! I’m planning on building something myself, but I think I’ll skip the car-part and go straight to something insect-like, lol.

Motors, wheels and platform come in a kit for about $12-15 on Amazon. The nodemcu on Banggood website is about $6-10 depending on us or china for the shipping site. The motor shield is about $5-6 depending on the shipping site. I went the quick route with US Shipping so I came to about $16 for the controller and shield. The battery in the picture is $10 bucks on Amazon but I got some 7.4V for $5 that will work just as fine. Total cost would be $35-40. Obviously, the cost could be shaved down significantly with the right time and minor changes in parts. The cheapest I could see this happening is maybe $20 with long lead times on parts from China.

1 Like

Thanks! It took me a while to wrap my brain around since my background really isn’t in programming. You and a few others helped me out a lot with advice and sample code. I had to make myself sit there and just walk through each part of the code. Writing the comments actually helped with this a lot. I was able to Frankenstein my way to a working car finally.

1 Like

It’s good practice to comment your code, it doesn’t have to be whole pages, but just some guidelines to see what you are trying to accomplish. It always helps me focus on what I’m trying to achieve.

Frankensteining is the first step to writing good code :wink:

2 Likes

hello guys,
I have made Robot which works on Bluetoothenter link description here,


Now, I am working on its modification by adding wiffi connectivity.(IOT)
I need your help guys.
I am using nodemcu 12e.

I have tried above code but i doesn’t understand how the connections are done between nodemcu & motor driver.
can anyone explain??

1 Like

guys please help me to convert my code into Two axial Joystick code

//Motor 1 is connected to pin 5 & 4.
//Motor 2 is connected to pin 0 & 2.
#define BLYNK_PRINT Serial 
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "xxxxxxxxxxxxxx"; 
int forw=0;
int rev=0;
int left=0;
int rig=0;
void myTimerEvent()
{
 
}
void setup()
{
Serial.begin(115200);
  Blynk.begin(auth, "xxxxxxxxxx", "xxxxxxxxxx");
  while (Blynk.connect() == false) {
    // Wait until connected
  }
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(0, OUTPUT);
  pinMode(2, OUTPUT);
}
 BLYNK_WRITE(V0)    //Forward
{
 int forw = param.asInt(); 
 if(forw==1)  
  {
    digitalWrite(5,HIGH);
    digitalWrite(0,HIGH);
    digitalWrite(4,LOW);
    digitalWrite(2,LOW);
  }
   else
  {
    digitalWrite(5,LOW);
    digitalWrite(0,LOW);
    digitalWrite(4,LOW);
    digitalWrite(2,LOW);
  }
}
 BLYNK_WRITE(V1)  //Reverse
{
 int rev = param.asInt();
 if(rev==1)  
  {
    digitalWrite(5,LOW);
    digitalWrite(0,LOW);
    digitalWrite(4,HIGH);
    digitalWrite(2,HIGH);
  } 
   else
  {
    digitalWrite(5,LOW);
    digitalWrite(0,LOW);
    digitalWrite(4,LOW);
    digitalWrite(2,LOW);
  }
}
 BLYNK_WRITE(V2)  //Left
{
 int left = param.asInt(); 
 if(left==1)  
  {
    digitalWrite(5,LOW);
    digitalWrite(0,HIGH);
    digitalWrite(4,LOW);
    digitalWrite(2,LOW);
  }
   else
  {
    digitalWrite(5,LOW);
    digitalWrite(0,LOW);
    digitalWrite(4,LOW);
    digitalWrite(2,LOW);
  }
}
 BLYNK_WRITE(V3) //Right
{
 int rig = param.asInt(); 
 if(rig==1)  
  {
    digitalWrite(5,HIGH);
    digitalWrite(0,LOW);
    digitalWrite(4,LOW);
    digitalWrite(2,LOW);
  }
   else
  {
    digitalWrite(5,LOW);
    digitalWrite(0,LOW);
    digitalWrite(4,LOW);
    digitalWrite(2,LOW);
  }
}
void loop()
{
  Blynk.run(); // Run Blynk
}
1 Like

Hi! Did this code worked? :slight_smile:

Yes,100%
Now,I am working on implementing above code for joystick widget.
But no one is helping.

Maybe I can help you with that… try to read this:
http://circuitdigest.com/microcontroller-projects/arduino-wifi-controlled-robot

I hope it helps. :slight_smile:

1 Like

What’s this part for?

int forw=0; int rev=0; int left=0; int rig=0; void myTimerEvent()

Sorry for the stupid question… i’m a total beginner

Thank you so much.
I think It will work.

int forw=0;
int rev=0;
int left=0;
int rig=0;
This part is for setting all values to 0 initially.

This part is do nothing.
I have done this by mistake.

Aww… thank you Sir :slight_smile:

Hi,

Thanks so much for the tutorial!
I’m a Arduino newbie. I got a problem getting the motors spinning (they work with another basic Uno project without Blynk).
I’ve been looking for answers on forums for over a week now and I’m getting truly desperate :frowning:
i even bought another nodemcu and L298N to try without success.

my setup:

Hardware:
Nodemcu esp8266
h bridge L298N with jumpers on ENA and ENB
4 dc motors
dc power 12v in

Software:
Arduino 1.6.12
board Nodemcu 1.0 selected
9600 baud

My problem is the motors don’t spin at all.
The network, wifi, blynk are working. I’m connected to the nodemcu and the motors are getting the signals every time i move the joystick in Blynk. The problem is they just vibrate and do not rotate. It looks like they are breaking. I also have the light on the nodemcu turning off every time I move the joystick.

My connections:
IN1 to nodemcu pin D5
IN2 to nodemcu pin D4
IN3 to nodemcu pin D0
IN4 to nodemcu pin D2

(only 2 motors connected on the pic)
Does anyone have any idea of the problem or what should i check?

Thanks so much in advance!

1 Like

Please past your code.&serial monitor output.

If they only vibrate it sounds like a power/hardware issue, but the fact they work without Blynk makes it weirder.

Did you try measuring the output to the motor with a multimeter? It may indicate if they get enough power.

Hi Saurabh47
Thanks for your reply.
The code is the exact same one posted by MakerD at the beginning of the thread.
I just changed the Blynk Char Auth code and the wifi name and password.

#define BLYNK_PRINT Serial // Prints to serial
#include <ESP8266WiFi.h> //Calls Blynk ESP8266 Wifi Libary
#include <BlynkSimpleEsp8266.h>//Calls Blynk ESP8266 Library

//Variables are declared at this point before void setup to make variables public and thus accessible throughout the entire code if needed.
int motorA ; //Variables for motor speed.
int motorB ; //Variables for motor speed.
int X=0; //Variable to recieve horizontal analog stick movement data.
int Y=0; //Variable to recieve vertical analog stick movement data.
int factor=0; //Decreases the speed of one motor to increase the differential between the two and thus provide differential steering or a steering wheel like control.
int maximo=0; //Limits the speed based on a slider widget within the Blynk App.

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = “authentication code for blynk”;

void setup()
{
// Set console baud rate
Serial.begin(9600);

Blynk.begin(auth,“WifiName”,“WifiPassword”); // Signs into home wifi.

pinMode(motorA, OUTPUT); //sets any pin used for motorA as output since pins may be inputs by default.
pinMode(motorB, OUTPUT); //sets any pin used for motorB as output since pins may be inputs by default.
pinMode(0,OUTPUT); //sets pin 0 to output since pins may be inputs by default.
pinMode(2,OUTPUT); //sets pin 2 to output since pins may be inputs by default.

}

BLYNK_WRITE(V1) //Vertual pin used with 2-axis joystick in merge mode to control robot movement.
{
int X1 = param[0].asInt();// Variable to recieve input from parameter 0 position array within V1 that tracks horizontal movement.
X=X1;
int Y1 = param[1].asInt();// Variable to recieve input from parameter 1 position array within V1 that tracks vertical movement.
Y=Y1;

}

BLYNK_WRITE(V0)//Virtual pin used with maximo variable to limit upper boundary or top end of speed.
{
int vel = param.asInt();
maximo=vel;
}

void loop()
{

if(X == 128 && Y == 128) // Writes 0 to all motor pins to Stop vehicle when joystick is in centered x and y position or neutral.
{
motorA = 0;
motorB = 0;
analogWrite(5, motorA);
analogWrite(4, motorA);
analogWrite(0, motorB);
analogWrite(2, motorB);
}

if(X > 123 && X < 132 && Y >= 129) //Forward
{
motorA = Y;
motorB = Y;

motorA = map(motorA, 129,255 , 450,maximo);
analogWrite(5, motorA);
digitalWrite(0,LOW);
motorB = map(motorB, 129,255 , 450,maximo);
analogWrite(4, motorB);
digitalWrite(2,HIGH);

}

else if(X > 123 && X < 132 && Y <= 127) //Reverse
{
motorA = Y;
motorB = Y;

motorA = map(motorA, 127,0 , 450,maximo);
analogWrite(5, motorA);
digitalWrite(0,HIGH);
motorB = map(motorB, 127,0 , 450,maximo);//something is wrong with HIGH signal
analogWrite(4, motorB);
digitalWrite(2,LOW);

}

else if(Y > 123 && Y < 132 && X <= 127) //Left
{
motorA = X;
motorB = X;

motorA = map(motorA, 127,0 , 450,maximo);
analogWrite(5, motorA);
digitalWrite(0,HIGH);
motorB = map(motorB, 127,0 , 450,maximo);//something is wrong with HIGH signal
analogWrite(4, motorB);
digitalWrite(2,HIGH);

}

else if(Y > 123 && Y < 132 && X >= 127) //Right
{
motorA = X;
motorB = X;

motorA = map(motorA, 129,255 , 450,maximo);
analogWrite(5, motorA);
digitalWrite(0,LOW);
motorB = map(motorB, 129,255 , 450,maximo);//something is wrong with HIGH signal
analogWrite(4, motorB);
digitalWrite(2,LOW);

}

else if(X >= 129 && Y >= 129) //Forward Right Steering
{
motorA = Y;
motorB = Y;
factor = X;
factor= map(factor,129,255, 0,200);

motorA = map(motorA, 129,255 , 450,maximo);
analogWrite(5, motorA);
digitalWrite(0,LOW);
motorB = map(motorB, 129,255 , 450,maximo);
analogWrite(4, (motorB-factor));
digitalWrite(2,HIGH);

}

else if(X <= 127 && Y >=129) //Forward Left Steering
{
motorA = Y;
motorB = Y;
factor = X;
factor= map(factor,127,0, 0,150);

motorA = map(motorA, 129,255 , 450,maximo);
analogWrite(5, (motorA-factor));
digitalWrite(0,LOW);
motorB = map(motorB, 129,255 , 450,maximo);
analogWrite(4, motorB);
digitalWrite(2,HIGH);

}

Blynk.run();
}

The Serial output is:

2dO4`lMEØ<B8Cß[241] Connecting to belkin.7e0
[2243] Connected to WiFi
[2243] IP: 192.168.1.31
[2243] Blynk v0.4.0 on NodeMCU
[5001] Connecting to blynk-cloud.com:8442
[5243] Ready (ping: 2ms).

Does the board work at 3.3V signal?