├── README.md ├── examples ├── motor_base │ └── motor_base.ino ├── motor_base_2 │ └── motor_base_2.ino ├── motor_standby_i2c │ └── motor_standby_i2c.ino └── motor_standby_io │ └── motor_standby_io.ino ├── keywords.txt ├── library.properties └── src ├── WEMOS_Motor.cpp └── WEMOS_Motor.h /README.md: -------------------------------------------------------------------------------- 1 | # WEMOS_Motor_Shield_Arduino_Library 2 | Arduino library for the WEMOS Motor Shiled - a shield for D1 mini, i2c interface, based TB6612 -------------------------------------------------------------------------------- /examples/motor_base/motor_base.ino: -------------------------------------------------------------------------------- 1 | #include "WEMOS_Motor.h" 2 | 3 | int pwm; 4 | 5 | //Motor shiled I2C Address: 0x30 6 | //PWM frequency: 1000Hz(1kHz) 7 | Motor M1(0x30,_MOTOR_A, 1000);//Motor A 8 | Motor M2(0x30,_MOTOR_B, 1000);//Motor B 9 | 10 | 11 | void setup() { 12 | Serial.begin(250000); 13 | } 14 | 15 | void loop() { 16 | 17 | for (pwm = 0; pwm <= 100; pwm++) 18 | { 19 | M1.setmotor( _CW, pwm); 20 | M2.setmotor(_CW, 100-pwm); 21 | Serial.printf("A:%d%, B:%d%, DIR:CW\r\n", pwm,100-pwm); 22 | } 23 | 24 | M1.setmotor(_STOP); 25 | M2.setmotor( _STOP); 26 | Serial.println("Motor A&B STOP"); 27 | delay(200); 28 | 29 | for (pwm = 0; pwm <=100; pwm++) 30 | { 31 | M1.setmotor(_CCW, pwm); 32 | M2.setmotor(_CCW, 100-pwm); 33 | Serial.printf("A:%d%, B:%d%, DIR:CCW\r\n", pwm,100-pwm); 34 | 35 | } 36 | 37 | M1.setmotor(_STOP); 38 | M2.setmotor( _STOP); 39 | delay(200); 40 | Serial.println("Motor A&B STOP"); 41 | 42 | M1.setmotor(_SHORT_BRAKE); 43 | M2.setmotor( _SHORT_BRAKE); 44 | Serial.println("Motor A&B SHORT BRAKE"); 45 | delay(1000); 46 | 47 | M1.setmotor(_STANDBY);//Both Motor standby 48 | //M2.setmotor( _STANDBY); 49 | Serial.println("Motor A&B STANDBY"); 50 | delay(1000); 51 | 52 | } 53 | -------------------------------------------------------------------------------- /examples/motor_base_2/motor_base_2.ino: -------------------------------------------------------------------------------- 1 | #include "WEMOS_Motor.h" 2 | 3 | float pwm; 4 | 5 | //Motor shiled I2C Address: 0x30 6 | //PWM frequency: 1000Hz(1kHz) 7 | Motor M1(0x30,_MOTOR_A, 1000);//Motor A 8 | 9 | 10 | 11 | void setup() { 12 | Serial.begin(250000); 13 | } 14 | 15 | void loop() { 16 | // put your main code here, to run repeatedly: 17 | 18 | Serial.printf("\r\nTest PWM 30 to 100, step 0.1,CW\r\n"); 19 | for (pwm = 30; pwm <= 100; pwm+=0.1) 20 | { 21 | M1.setmotor( _CW, pwm); 22 | Serial.println(pwm); 23 | 24 | } 25 | 26 | M1.setmotor(_STOP); 27 | 28 | Serial.println("Motor A STOP"); 29 | delay(200); 30 | 31 | Serial.printf("Test PWM 30 to 100, step 0.1,CCW\r\n"); 32 | for (pwm = 30; pwm <= 100; pwm+=0.1) 33 | { 34 | M1.setmotor(_CCW, pwm); 35 | Serial.println(pwm); 36 | } 37 | 38 | M1.setmotor(_STOP); 39 | 40 | Serial.println("Motor A STOP"); 41 | delay(200); 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /examples/motor_standby_i2c/motor_standby_i2c.ino: -------------------------------------------------------------------------------- 1 | #include "WEMOS_Motor.h" 2 | 3 | int pwm; 4 | 5 | 6 | //Motor shiled I2C Address: 0x30 7 | //PWM frequency: 1000Hz(1kHz) 8 | Motor M1(0x30, _MOTOR_A, 1000); //Motor A 9 | 10 | 11 | 12 | void setup() { 13 | Serial.begin(250000); 14 | } 15 | 16 | void loop() { 17 | 18 | for (pwm = 40; pwm <= 100; pwm++) 19 | { 20 | M1.setmotor( _CW, pwm); 21 | Serial.printf("A:%d%, B:%d%, DIR:CW\r\n", pwm, 100 - pwm); 22 | delay(500); 23 | M1.setmotor(_STANDBY); 24 | Serial.println("Motor A STANDBY"); 25 | delay(100); 26 | } 27 | 28 | 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /examples/motor_standby_io/motor_standby_io.ino: -------------------------------------------------------------------------------- 1 | #include "WEMOS_Motor.h" 2 | 3 | int pwm; 4 | int standby_io = D3; 5 | 6 | //Motor shiled I2C Address: 0x30 7 | //PWM frequency: 1000Hz(1kHz) 8 | Motor M1(0x30, _MOTOR_A, 1000, standby_io); //Motor A 9 | 10 | 11 | 12 | void setup() { 13 | Serial.begin(250000); 14 | } 15 | 16 | void loop() { 17 | 18 | for (pwm = 40; pwm <= 100; pwm++) 19 | { 20 | M1.setmotor( _CW, pwm); 21 | Serial.printf("A:%d%, B:%d%, DIR:CW\r\n", pwm, 100 - pwm); 22 | delay(500); 23 | M1.setmotor(_STANDBY); 24 | Serial.println("Motor A STANDBY"); 25 | delay(100); 26 | } 27 | 28 | 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For Motor 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | Motor KEYWORD1 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | setfreq KEYWORD2 16 | setmotor KEYWORD2 17 | 18 | ####################################### 19 | # Constants (LITERAL1) 20 | ####################################### 21 | 22 | _SHORT_BRAKE LITERAL1 23 | _CCW LITERAL1 24 | _CW LITERAL1 25 | _STOP LITERAL1 26 | _STANDBY LITERAL1 27 | _MOTOR_A LITERAL1 28 | _MOTOR_B LITERAL1 29 | 30 | 31 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=WEMOS Motor Shield 2 | version=1.0.0 3 | author=WEMOS.CC 4 | maintainer=WEMOS.CC 5 | sentence=Library for the WEMOS Motor Shield. 6 | paragraph=Library for the WEMOS Motor Shield. 7 | category=Device Control 8 | url=https://github.com/wemos/WEMOS_Motor_Shield_Arduino_Library 9 | architectures=* 10 | -------------------------------------------------------------------------------- /src/WEMOS_Motor.cpp: -------------------------------------------------------------------------------- 1 | #include "WEMOS_Motor.h" 2 | 3 | /* Motor() 4 | address: 5 | Shield I2C address 6 | freq: 7 | PWM's frequency 8 | */ 9 | Motor::Motor(uint8_t address, uint8_t motor, uint32_t freq) 10 | { 11 | _use_STBY_IO=false; 12 | 13 | if(motor==_MOTOR_A) 14 | _motor=_MOTOR_A; 15 | else 16 | _motor=_MOTOR_B; 17 | 18 | Wire.begin(); 19 | 20 | _address=address; 21 | 22 | setfreq(freq); 23 | 24 | } 25 | 26 | 27 | Motor::Motor(uint8_t address, uint8_t motor, uint32_t freq, uint8_t STBY_IO) 28 | { 29 | 30 | 31 | 32 | _use_STBY_IO=true; 33 | _STBY_IO=STBY_IO; 34 | 35 | if(motor==_MOTOR_A) 36 | _motor=_MOTOR_A; 37 | else 38 | _motor=_MOTOR_B; 39 | 40 | Wire.begin(); 41 | 42 | _address=address; 43 | 44 | setfreq(freq); 45 | 46 | pinMode(_STBY_IO,OUTPUT); 47 | digitalWrite(_STBY_IO,LOW); 48 | } 49 | 50 | 51 | /* setfreq() -- set PWM's frequency 52 | 53 | freq: 54 | PWM's frequency 55 | 56 | */ 57 | void Motor::setfreq(uint32_t freq) 58 | { 59 | Wire.beginTransmission(_address); 60 | Wire.write(((byte)(freq >> 16)) & (byte)0x0f); 61 | Wire.write((byte)(freq >> 16)); 62 | Wire.write((byte)(freq >> 8)); 63 | Wire.write((byte)freq); 64 | Wire.endTransmission(); // stop transmitting 65 | delay(100); 66 | } 67 | 68 | /* setmotor() -- set motor 69 | 70 | motor: 71 | _MOTOR_A 0 Motor A 72 | _MOTOR_B 1 Motor B 73 | 74 | dir: 75 | _SHORT_BRAKE 0 76 | _CCW 1 77 | _CCW 2 78 | _STOP 3 79 | _STANDBY 4 80 | 81 | pwm_val: 82 | 0.00 - 100.00 (%) 83 | 84 | */ 85 | void Motor::setmotor(uint8_t dir, float pwm_val) 86 | { 87 | uint16_t _pwm_val; 88 | 89 | if(_use_STBY_IO==true){ 90 | 91 | if(dir==_STANDBY) 92 | { 93 | digitalWrite(_STBY_IO,LOW); 94 | return; 95 | } 96 | else 97 | digitalWrite(_STBY_IO,HIGH); 98 | } 99 | 100 | Wire.beginTransmission(_address); 101 | Wire.write(_motor | (byte)0x10); 102 | Wire.write(dir); 103 | 104 | _pwm_val=uint16_t(pwm_val*100); 105 | 106 | if(_pwm_val>10000) 107 | _pwm_val=10000; 108 | 109 | Wire.write((byte)(_pwm_val >> 8)); 110 | Wire.write((byte)_pwm_val); 111 | Wire.endTransmission(); // stop transmitting 112 | 113 | 114 | delay(100); 115 | } 116 | 117 | void Motor::setmotor(uint8_t dir) 118 | { 119 | setmotor(dir,100); 120 | } -------------------------------------------------------------------------------- /src/WEMOS_Motor.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __WEMOS_MOTOR_H 3 | #define __WEMOS_MOTOR_H 4 | 5 | 6 | #if ARDUINO >= 100 7 | #include "Arduino.h" 8 | #else 9 | #include "WProgram.h" 10 | #endif 11 | 12 | #include "Wire.h" 13 | 14 | #define _MOTOR_A 0 15 | #define _MOTOR_B 1 16 | 17 | #define _SHORT_BRAKE 0 18 | #define _CCW 1 19 | #define _CW 2 20 | #define _STOP 3 21 | #define _STANDBY 4 22 | 23 | 24 | class Motor{ 25 | public: 26 | Motor(uint8_t address, uint8_t motor, uint32_t freq); 27 | Motor(uint8_t address, uint8_t motor, uint32_t freq, uint8_t STBY_IO); 28 | void setfreq(uint32_t freq); 29 | void setmotor(uint8_t dir, float pwm_val); 30 | void setmotor(uint8_t dir); 31 | 32 | private: 33 | uint8_t _address; 34 | uint8_t _motor; 35 | bool _use_STBY_IO=false; 36 | uint8_t _STBY_IO; 37 | 38 | }; 39 | 40 | 41 | #endif 42 | 43 | --------------------------------------------------------------------------------