WeMos Motor Shield Examples

"I feel the need... The need for speed!" And depending on the size of the motor and battery supply in your project you can too. This is all well and fun until you realize.."Hey! A little control over where all this speed is going is going to be needed." And control can be more fun anyways. 

   (The video above will show how the example code below should work.)

Shopping List

Motor Shield for WeMos D1 MiniWeMos D1 Mini 9V Battery Shield for WeMos D1 Mini

 

The motor shield is a great component that will help you control your bots speed and direction. As opposed to most RC cars which use a servo to operate the steering on let's say a RC car, the standard robot configuration would use two opposing motors. What the shield does is allow you control the speed and direction of both motors, which yes, also gives you the power to steer it.

Getting Connected

On the motor shield there is a line of pins dedicated specifically for motor use. These pins include the GND, VM, A1,A2,B1 and B2. 

 

 The GND and VM pins will be where you attach your battery connection to power the motors as simply using the supply from stacking the motor shield on a powered shield will not power the motors. GND will be the attachment to the negative side of the power source and VM the positive side.

The A1 and A2 are the pins for the connection to the first motor. The connections are interchangeable since as you switch the terminals on the power supply it will spin the motor in the opposite direction. So once you make your program run and you notice the motors aren't going in the direction you were expecting you could either change your code or swap the connections at pins A1 and A2. Pins B1 and B2 are the same and will be used if your project has a second motor involved.

Coding the Motor Shield 

Before you can start programming the motor shield you will need to ensure you have the correct libraries in place. These are the files built specifically to control the shield that you will need to import into your Arduino IDE. The libraries necessary to run the shield can be found on Github at: https://github.com/wemos/WEMOS_Motor_Shield_Arduino_Library. You can download and extract the zip files for the library and place them in your ~/Documents/Arduino/Libraries/ folder. 

After restarting the Arduino IDE you should see the folder name you added to the libraries folder under "Sketch" then "Include Library".

Now as a simple test to make sure everything works we have supplied the same motor example sketch provided by the Motor Shield Library below. You can upload this code to your WeMos D1 Mini and see if you get your motor going.

#include "WEMOS_Motor.h"

float pwm;
//PWM frequency: 1000Hz(1kHz)
Motor M1(0x30,_MOTOR_A, 1000);//Motor A

void setup(){
  Serial.begin(250000);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.printf("\r\nTest PWM 30 to 100, step 0.1,CW\r\n");
  for (pwm = 30; pwm <= 100; pwm+=0.1){
    M1.setmotor( _CW, pwm);
    Serial.println(pwm);
  }
  M1.setmotor(_STOP);
  Serial.println("Motor A STOP");
  delay(200);
  Serial.printf("Test PWM 30 to 100, step 0.1,CCW\r\n");
  for (pwm = 30; pwm <= 100; pwm+=0.1){
    M1.setmotor(_CCW, pwm);
    Serial.println(pwm);
  }  
  M1.setmotor(_STOP);
  Serial.println("Motor A STOP");
  delay(200);
}

The above example code will begin to rotate the motor connected to the Motor A pins clockwise until it reaches full power. At full power the motor will stop and being to rotate counter clockwise till again at full power and repeat the entire process.

There are a few pieces of code to make note of here, the first is: Motor M1(0x30,_MOTOR_A, 1000) .  This line instantiates the motor object called M1. Once instantiated you can call upon the newly created M1 motor and its functions.

the next line M1.setmotor(_CCW, pwm) makes use of the setmotor function. The setmotor function has two parameters the first is a uint8_t (which is an alias for the byte type) that controls the direction of rotation and the second a float that controls the speed of rotation. The defined range of values for these parameters are listed below. 

uint8_t direction value:
_SHORT_BRAKE  0
_CCW 1
_CCW 2
_STOP 3
_STANDBY 4
float speed value:
0.00 - 100.00 (%)

 

We have also made some small modification to the example code below that will display how to make use of two motors using the WeMos Motor Shield.

#include "WEMOS_Motor.h"

float pwm;
//PWM frequency: 1000Hz(1kHz)
Motor M1(0x30,_MOTOR_A, 1000);//Motor A
Motor M2(0x30,_MOTOR_B, 1000);//Motor B

void setup(){
  Serial.begin(250000);
}

void loop(){
  // put your main code here, to run repeatedly:
  Serial.printf("\r\nTest PWM 30 to 100, step 5.1,CW\r\n");
  for (pwm = 30; pwm <= 100; pwm+=5.1){
    M1.setmotor( _CW, pwm);
    M2.setmotor( _CW, pwm);
    Serial.println(pwm);
  }
  M1.setmotor(_STOP);
  M2.setmotor(_STOP);
  Serial.println("Motor A STOP");
  delay(200);
  Serial.printf("Test PWM 30 to 100, step 5.1,CCW\r\n");
  for (pwm = 30; pwm <= 100; pwm+=5.1){
    M1.setmotor(_CCW, pwm);
    M2.setmotor(_CCW, pwm);
    Serial.println(pwm);
  }  
  M1.setmotor(_STOP);
  M2.setmotor(_STOP);
  Serial.println("Motor A STOP");
  delay(200);
}

Need any other WeMos boards or shields for your project? Visit GadgetsThatGo.com


3 comments

  • Aaron Schnetzer

    Thanks for letting me know Paul, You’re right, I’ll update the diagram when I can. But to be clear you will need the DC power shield unless you want to power the Wemos D1 mini separately. Connecting the power through the motor shield will not power D1 mini at the same time. But connecting the motor shield to the DC power Supply will supply power to the Wemos D1 mini and motor shield with one power source from DC power shield.

  • Paul

    Damm auto correct – ‘moneyed’ should have been ‘need’

  • Paul

    Why do you say I moneyed a DC power shield and then don’t use it? Also the video shows the power connected to a DC power shield. Things are not consistent.

Leave a comment

Please note, comments must be approved before they are published