diff --git a/README.md b/README.md index 2da31d2..da351c1 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,77 @@ -# WEMOS_Motor_Shield_Arduino_Library -Arduino library for the WEMOS Motor Shiled - a shield for D1 mini, i2c interface, based TB6612 \ No newline at end of file +# Wemos_D1_Motor_Shield +Arduino library for the Wemos D1 mini Motor Shield (i2c interface, TB6612 based). + +## Firmware Warning + +WARNING: The shipped firmware with the Motor Shield is bugged. You need to update it. You can update it from Windows with a D1 Mini. You can find the update instructions below. + +### Sources + +* https://www.letscontrolit.com/wiki/index.php/WemosMotorshield +* https://github.com/pbugalski/wemos_motor_shield +* https://hackaday.io/project/18439-motor-shield-reprogramming + +## Update the Firmware (from Windows) + +### Step 1 + +Upload an empty sketch to you D1 Mini. This will allow serial data to pass from the USB port to the Motor Shield. Basically, we are telling the D1 Mini to ignore all serial data that will pass through to the Motor Shield. + +Sketch: +``` +void setup() { + // put your setup code here, to run once: + +} + +void loop() { + // put your main code here, to run repeatedly: + +} +``` + +### Step 2 + +Solder RTS and 3V on the Motor Shield. This activates the reprogramming mode of the Motor Shield. Then build this circuit: +![Image of circuit to update the firmware](https://github.com/thomasfredericks/wemos_motor_shield/blob/master/doc/wemos_motor_update_firmware.png?raw=true) + +### Step 3 + +Download STM32Flash from here https://sourceforge.net/projects/stm32flash/files/ + +Extract in a directory. + +### Step 4 + +Download the motor_shield.bin: https://cdn.hackaday.io/files/18439788894176/motor_shield.bin + +Extract and put in the same directory as ```stm32flash.exe``` downloaded in the step above. + +### Step 5 + +Execute the following three commands. IMPORTANT: Replace COM8 with the port of your Wemos D1 Mini. + + +1) ```stm32flash.exe COM8``` + +Replace COM8 with the port of your Wemos D1 Mini. + +![image of stm32flash.exe result](https://github.com/thomasfredericks/wemos_motor_shield/blob/master/doc/update_command_a.png?raw=true) + +2) ```stm32flash.exe -k COM8``` + +This will unlock your shield, replace COM8 with the port of your Wemos D1 Mini + +![image of stm32flash.exe -k result](https://github.com/thomasfredericks/wemos_motor_shield/blob/master/doc/update_command_b.png?raw=true) + +3) ```stm32flash.exe -f -v -w motor_shield.bin COM8``` + +This will flash the bin and re-lock the shield, replace COM8 with the port of your Wemos D1 Mini. + +![image of stm32flash.exe -f -v -w motor_shield.bin result](https://github.com/thomasfredericks/wemos_motor_shield/blob/master/doc/update_command_c.png?raw=true) + +### Step 6 + +Disconnect everything and unsolder RTS and 3V on the Wemos Motor Shield. + + diff --git a/doc/update_command_a.png b/doc/update_command_a.png new file mode 100644 index 0000000..4a2595a Binary files /dev/null and b/doc/update_command_a.png differ diff --git a/doc/update_command_b.png b/doc/update_command_b.png new file mode 100644 index 0000000..0a47f42 Binary files /dev/null and b/doc/update_command_b.png differ diff --git a/doc/update_command_c.png b/doc/update_command_c.png new file mode 100644 index 0000000..5a91079 Binary files /dev/null and b/doc/update_command_c.png differ diff --git a/doc/wemos_motor_update_firmware.png b/doc/wemos_motor_update_firmware.png new file mode 100644 index 0000000..f14f5f2 Binary files /dev/null and b/doc/wemos_motor_update_firmware.png differ diff --git a/examples/motor_base_2/motor_base_2.ino b/examples/more/motor_base_2/motor_base_2.ino similarity index 100% rename from examples/motor_base_2/motor_base_2.ino rename to examples/more/motor_base_2/motor_base_2.ino diff --git a/examples/motor_standby_i2c/motor_standby_i2c.ino b/examples/more/motor_standby_i2c/motor_standby_i2c.ino similarity index 100% rename from examples/motor_standby_i2c/motor_standby_i2c.ino rename to examples/more/motor_standby_i2c/motor_standby_i2c.ino diff --git a/examples/motor_standby_io/motor_standby_io.ino b/examples/more/motor_standby_io/motor_standby_io.ino similarity index 100% rename from examples/motor_standby_io/motor_standby_io.ino rename to examples/more/motor_standby_io/motor_standby_io.ino diff --git a/examples/motor_base/motor_base.ino b/examples/motor_base/motor_base.ino deleted file mode 100644 index 9c253d6..0000000 --- a/examples/motor_base/motor_base.ino +++ /dev/null @@ -1,52 +0,0 @@ -#include "WEMOS_Motor.h" - -int pwm; - -//Motor shiled I2C Address: 0x30 -//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() { - - for (pwm = 0; pwm <= 100; pwm++) - { - M1.setmotor( _CW, pwm); - M2.setmotor(_CW, 100-pwm); - Serial.printf("A:%d%, B:%d%, DIR:CW\r\n", pwm,100-pwm); - } - - M1.setmotor(_STOP); - M2.setmotor( _STOP); - Serial.println("Motor A&B STOP"); - delay(200); - - for (pwm = 0; pwm <=100; pwm++) - { - M1.setmotor(_CCW, pwm); - M2.setmotor(_CCW, 100-pwm); - Serial.printf("A:%d%, B:%d%, DIR:CCW\r\n", pwm,100-pwm); - - } - - M1.setmotor(_STOP); - M2.setmotor( _STOP); - delay(200); - Serial.println("Motor A&B STOP"); - - M1.setmotor(_SHORT_BRAKE); - M2.setmotor( _SHORT_BRAKE); - Serial.println("Motor A&B SHORT BRAKE"); - delay(1000); - - M1.setmotor(_STANDBY);//Both Motor standby - //M2.setmotor( _STANDBY); - Serial.println("Motor A&B STANDBY"); - delay(1000); - -} diff --git a/examples/wemos_motor_shield_demo/wemos_motor_shield_demo.ino b/examples/wemos_motor_shield_demo/wemos_motor_shield_demo.ino new file mode 100644 index 0000000..53c075d --- /dev/null +++ b/examples/wemos_motor_shield_demo/wemos_motor_shield_demo.ino @@ -0,0 +1,59 @@ +#include + + +#include "WEMOS_Motor.h" + + + +//Motor shield default I2C Address: 0x30 +//PWM frequency: 1000Hz(1kHz) +Motor M1(0x30, _MOTOR_A, 1000); //Motor A +Motor M2(0x30, _MOTOR_B, 1000); //Motor B + + +void setup() { + + + Serial.begin(57600); + Serial.println("Starting demo"); + + +} + +void loop() { + + int pwm; + + for (pwm = 0; pwm <= 100; pwm++) + { + M1.setmotor( _CW, pwm); + M2.setmotor(_CW, pwm); + Serial.print("Clockwise PWM: "); + Serial.println(pwm); + delay(100); + } + + Serial.println("Motor STOP"); + M1.setmotor(_STOP); + M2.setmotor( _STOP); + + delay(1000); + + + for (pwm = 0; pwm <= 100; pwm++) + { + M1.setmotor(_CCW, pwm); + //delay(1); + M2.setmotor(_CCW, pwm); + Serial.print("Counterclockwise PWM: "); + Serial.println(pwm); + delay(100); + + } + + Serial.println("Motor A&B STANDBY"); + M1.setmotor(_STANDBY); + M2.setmotor( _STANDBY); + delay(1000); + +} diff --git a/src/WEMOS_Motor.cpp b/src/WEMOS_Motor.cpp index ca6eaff..6f2d9a8 100644 --- a/src/WEMOS_Motor.cpp +++ b/src/WEMOS_Motor.cpp @@ -8,6 +8,9 @@ */ Motor::Motor(uint8_t address, uint8_t motor, uint32_t freq) { + + Wire.begin(); + _use_STBY_IO=false; if(motor==_MOTOR_A) @@ -15,7 +18,7 @@ Motor::Motor(uint8_t address, uint8_t motor, uint32_t freq) else _motor=_MOTOR_B; - Wire.begin(); + _address=address; @@ -57,12 +60,12 @@ Motor::Motor(uint8_t address, uint8_t motor, uint32_t freq, uint8_t STBY_IO) void Motor::setfreq(uint32_t freq) { Wire.beginTransmission(_address); - Wire.write(((byte)(freq >> 16)) & (byte)0x0f); + Wire.write(((byte)(freq >> 24)) & (byte)0x0f); Wire.write((byte)(freq >> 16)); Wire.write((byte)(freq >> 8)); Wire.write((byte)freq); Wire.endTransmission(); // stop transmitting - delay(100); + //delay(100); } /* setmotor() -- set motor @@ -109,12 +112,10 @@ void Motor::setmotor(uint8_t dir, float pwm_val) Wire.write((byte)(_pwm_val >> 8)); Wire.write((byte)_pwm_val); Wire.endTransmission(); // stop transmitting - - - delay(100); + //delay(1); } void Motor::setmotor(uint8_t dir) { setmotor(dir,100); -} \ No newline at end of file +}