Lego-Car (WeMos Mini D1 and L293D Motor Shield)

Hi Blynkers,

I’ve done a small Car with Legos and before let my daughter play with it, I just wanted to share the monster!
The motor shield is the L293D adapted to the Wemos size. I can share the details if needed.

You can find the code below, it has been prepared to be updated via OTA, based on the one I posted time ago for an Arduino Nano and the ESP-01 as shield (you can find the topic HERE), and improved using pieces of code obtained from @Costas (thanks for your support :slight_smile:).

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ArduinoOTA.h>       // for local OTA updates
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>      // Essential for almost all sketches
SimpleTimer timer;


//********************* COMPLETE WITH YOUR DETAILS *************
// Get Auth Token in the Blynk App.
char auth[] = "Blynk_is_great";  
char cloudserver[16] = "blynk-cloud.com";
char localserver[16] = "xxx.xxx.xxx.xxx";  // IP details for the local server.
char ssid[] = "xxxxxxxx";                   // Your WiFi credentials.
char pass[] = "xxxxxxxxxxxxxx";             // Password.
char OTAhost[] = "WiFi4OTA1";              // Optional.
//****************



int motorA ; // Left motor connected at D2 and D4 WeMos Mini D1
int motorB ; // Right motor connected at D5 and D7 WeMos Mini D1
int X=512;
int Y=512;
int factor=0; // Variable to reduce speed of the motorA or B and turn.
int maximo=0;
bool isFirstConnect = true;


void reconnectBlynk() {                         // reconnect to server if disconnected
  if (!Blynk.connected()) {
    if(Blynk.connect()) {
      BLYNK_LOG("Reconnected");
    } else {
      BLYNK_LOG("Not reconnected");
    }
  }
}


BLYNK_CONNECTED() {
if (isFirstConnect) {
Blynk.syncAll();
Blynk.notify("LET'S GO MATE!!!!");
isFirstConnect = false;
}
}

void setup()
{
  Serial.begin(115200);
  Serial.println("\n Starting");
  //Blynk.begin(auth, ssid, pass);              // normal Blynk Cloud server connection     
  //Blynk.config(auth, cloudserver);            // for Blynk's cloud server if WiFi already connected
  Blynk.begin(auth, ssid, pass, localserver);   // for a local server requiring WiFi connection
  int mytimeout = millis() / 1000;
  while (Blynk.connect(1000) == false) {        // wait here until connected to the server
    if((millis() / 1000) > mytimeout + 8){      // try to connect to the server for less than 9 seconds
      break;                                    // continue with the sketch regardless of connection to the server
    }
  }  
  ArduinoOTA.setHostname(OTAhost);              // for local OTA updates
  ArduinoOTA.begin();                           // for local OTA updates
  timer.setInterval(15000, reconnectBlynk); // check every 15 seconds if we are connected to the server

 pinMode(D3, OUTPUT); 
 pinMode(D4, OUTPUT);
 pinMode(D5, OUTPUT); 
 pinMode(D6, OUTPUT);
}


BLYNK_WRITE(V0) 
{
 int X1 = param.asInt();
 X=X1;
 Blynk.virtualWrite(V3, X); 
}

 BLYNK_WRITE(V1)
{
 int Y1 = param.asInt();
 Y=Y1;
 Blynk.virtualWrite(V4, Y);
}

 BLYNK_WRITE(V2)//      slider  from 600 to 1023!!!!
{
 int vel = param.asInt(); 
 maximo=vel;
}

void loop(){
  
  if(X == 512  &&  Y == 512)  //  Both Motors stopped.
  {
   motorA = 0;
   motorB = 0;
   analogWrite(D3, motorA);  //Entrada del L293D
   analogWrite(D4, motorA);  //Entrada del L293D
   analogWrite(D5, motorB); //Entrada del L293D
   analogWrite(D6, motorB); //Entrada del L293D
   } 
   
   if(X >= 513 && Y == 512)   // Both Motors Moving forward
  {
    motorA = X;
    motorB = X;
    
    motorA = map(motorA, 513,1023,400,maximo);
    analogWrite(D3, motorA);
    analogWrite(D4,0);
    motorB = map(motorB, 513,1023,400,maximo);
    analogWrite(D6, motorB);
    analogWrite(D5,0);
  }
   if(X >= 513 && Y <= 511)   //Turning Right Forward
  {
    motorA = X;
    motorB = X;
    factor = Y;
    factor= map(factor,0,511,400,0);
    
    motorA = map(motorA, 513,1023,400,maximo);
    analogWrite(D3, motorA);
    analogWrite(D4,0);
    motorB = map(motorB, 513,1023,400,maximo);
    analogWrite(D6, (motorB-factor));
    analogWrite(D5,0);
  }
   if(X >= 513 && Y >= 513)   //Turning Left Forward
  {
    motorA = X;
    motorB = X;
    factor = Y;
    factor= map(factor,513,1013, 0,400);
    
    motorA = map(motorA, 513,1023 , 400,maximo);
    analogWrite(D3, (motorA-factor));
    analogWrite(D4,0);
    motorB = map(motorB, 513,1023 , 400,maximo);
    analogWrite(D6, motorB);
    analogWrite(D5,0);
  }
  if(X <= 511 && Y ==512)   //Backwards
  {
    motorA = X;
    motorB = X;
    
    motorA = map(motorA, 0,511 , maximo,400);
    analogWrite(D4, motorA);
    analogWrite(D3,0);
    motorB = map(motorB, 0,511 , maximo,400);
    analogWrite(D5, motorB);
    analogWrite(D6,0);
  }
  if(X <= 511 && Y <=511)   //Turning Right Backwards
  {
    motorA = X;
    motorB = X;
    factor = Y;
    factor= map(factor,0,511, 400,0);
    
    motorA = map(motorA, 0,511 , maximo,400);
    analogWrite(D4, motorA);
    analogWrite(D3,0);
    motorB = map(motorB, 0,511 , maximo,400);
    analogWrite(D5, (motorB-factor));
    analogWrite(D6,0);
  }
  if(X <= 511 && Y >=513)   //Turning Left Backwards
  {
    motorA = X;
    motorB = X;
    factor = Y;
    factor= map(factor,513,1023, 0,400);
    
    motorA = map(motorA, 0,511 , maximo,400);
    analogWrite(D4, (motorA-factor));
    analogWrite(D3,0);
    motorB = map(motorB, 0,511 , maximo,400);
    analogWrite(D5, motorB);
    analogWrite(D6,0);
  }

if (Blynk.connected()) {   // to ensure that Blynk.run() function is only called if we are still connected to the server
    Blynk.run();
  }
  timer.run();
  ArduinoOTA.handle();       // for local OTA updates
}

At the moment I have no video done but if it survives till tomorrow night I’ll post it.

EDIT:

The video is already here: :slight_smile:

Kind regards!!!

13 Likes

hanging out for video

Electric Lego car!!! Suck on that Tesla! Haha. Super cool :slight_smile:

1 Like

@Dave1829, I’ll keep the Car out of my daughter’s hands and upload the video.:wink:

1 Like

Video ready at the edited first post!

Hope you like it!

@psoro Is that local server? Or Blynk cloud? I see no delay.

Local Server @Dmitriy, no delays!

1 Like

Super cool! I’m ordering right away that motor shield…
Thank you

Glad you like it @gusgonnet, this shield is homemade, you won’t find it anyway…

Kind regards!

2 Likes

Do you have a schematic or some sort of guide how you made this shield? I usually order them, but I really don’t like all the LED’s they put on there. How much power can this handle?

Cool car . which batteries you have used??

@Lichtsignaal you have to design shield according to your need .
Use basic digram of ic to design it .
I had simply made motor driver for this my this project

Hi @Lichtsignaal,
The L293D can handle 600mA OUTPUT CURRENT CAPABILITY
PER CHANNEL and 1.2A PEAK OUTPUT CURRENT (non repetitive) PER CHANNEL.

You can find below schematic:

The Vin line is 6.5V min and 12V max. (you can feed directly 5V removing the ASM1117-5V)
The Vmt line is 4.5V min and 36V max.

You can find HERE the Motor Shield made by WeMos as alternative.
But… It seems (as per WeMos forum) the board doesn’t like delays above 10 seconds (https://forum.wemos.cc/topic/317/d1-mini-motor-shield/29)

Regards!

Hi @saurabh47,
I’m using 2 x 18650 3.7V 2200mAh Li-ion Rechargeable Batteries.

Regards!

How much do these cost at your local store?

@Costas,
About 7 or 8€ each. I don’t remember… It was a long time ago…:blush:

1 Like

Thanks very much for the info! I assume I can use this to drive stepper motors too? Yeah probably, lol. Al right China, watch out, here I come :smiley:

For sure, you can use the L293D to drive stepper motors, but if that’s your idea, consider using the A4988 driver, it’s really easy to use with the AccelStepper library and works fine with the WeMos mini D1

BR

Hello psoro,

Can you guide me how to use Wemos D1 R2 and L293D motor shield?
I don’t know if it can work. And what pins to control?
Thanks you,
An.

Hi @AnTran,
Are you talking about the Adafruit Motor Shield for Arduino V 1.2 or something like this?
You can’t use this shield. This motor shield is controlled via the 74HC595 serial-to-parallel latch.
The pins for Dir_Ser and Dir_Lach are the same (D6) at the Wemos D1.

HTH

Kind regards