Tim's I2C Dual Motor Driver
by Palingenesis in Circuits > Microcontrollers
729 Views, 6 Favorites, 0 Comments
Tim's I2C Dual Motor Driver
Don't have enough JPIO pins on you Microcontroller? This board is controlled via I2C, over 100 on one bus, each controlling 2 motors.
This is my second I2C Motor Driver, My first worked OK, but The MOSFETS I used where a little temperamental, so I decided to make a more robust version.
This one I decided to control 2 motors from one microcontroller.
- The main reason for having it control two motors at the same time was that I tried my original motor drivers controlling a tracked vehicle. It worked fine with two single motor drivers but because I was using a web interface to control them there was a delay between turning on each motor.
- I thought having a motor driver that controls two motor at the same time was a better approach.
The Motor Driver is controlled via the I2C bus.
- Each board can have its own unique I2C Address.
- Not using the reserved Addresses over 100 in theory can be attached to one I2C bus. (Addresses 10 to 127 probably) 7-bit Address.
- The is a system to reset the I2C default address to 0x30.
- There are two sockets for the I2C to that the boards can be daisy-chained, end to end.
- There is place for pull-up resistors, depending on muti-board setup or single board setup, these can be enabled/disabled.
Update
Since the writing of this Instructable I have made a Version 2.- Version 2 is made the same way as this.
- There are some small improvements.
- I was unable to do the improvements just by updating the firmware.
- There was some pin reallocation so a new PCB needed to be made.
Supplies
I have done a new shared project at for the Version 2.
make it very easy to get all you need to make this PCB.
The video above was a follow up to a live stream I did.
- Now and then, I will do a live stream to show how I do things.
- Treat my live stream as you would a technical document, click through it to find what you are interested in.
Making an I2C Dual Motor Drver (youtube.com)
- 3 hours 45 minutes.
If you would like access to all files of the original version. I have also did a GitHub Page.
- Palingenesis/Tims-I2C-Dual-Motor-Driver: A Motor Driver to control two Motors via I2C (github.com)
- For the new version 2 PCB use the files from my shared Project at PCBway
The Microcontroller Is the STM8S103F3
- According to the Data Sheet, this can run with 2.95 to 5.5 V operating voltage.
- Caution should be taken when programming, The ST-Link is 3.3v.
- I was a little limited for pins and memory, as I am controlling two motors with this. The AI has been reduced, it still has inputs for quadratic encoders and a station, these enable it to know the rotation and position of the motors.
- I was able to use the current sense from the DRV8876, so the load on the motors can be retrieved from communication via I2C.
There are LEDs for diagnostics.
- Two Yellow LEDs, one for Motor Power and one for VCC.
- There is a red LED on the SDA line to show activity.
- For each motor there is a red LED to show a fault, a blue LED to show Motor on and a Green LED to show if motor forward or reverse (on/off).
There are Pads for programming the STM8S103F3
To program the STM8S103F3 will require STMicroelectronics "ST Visual Programmer"
STMicroelectronics "ST Visual Programmer" is part of "ST MCU toolset".
I have done .s19 file for use with the "ST Visual Programmer".
An ST-Link is required.
Programming the STM8S103F3P6
To programme the STM8S103F3P6 you will need:
- ST Visual Develop IDE from STMicroelectronics.
- ST-Link v2 from shop.
This is not an Instructable about ST Visual Develop IDE there is just enough for you to upload the firmware to the STM8S103F3P6.
- Should you wish to install both ST Visual Develop IDE and Cosmic STM8 Compiler.
- I found this Instructable very helpful in getting started with ST Visual Develop IDE: Setup ST Visual Develop With the Cosmic STM8 Compiler : 11 Steps - Instructables
- Note! when getting a free licence for Cosmic Compiler, check your email spam folder.
The following instructions is how to upload my firmware to the Motor Driver.
I have just put pads for programming.
- I have spaced them so that you should be able to hold a row of four 2.54mm male header pins on them.
- Programming only takes seconds.
- I prefer to solder leads to the pads.
To program the STM8S103F we need to use the ST-Link v2 USB device.
The ST-Link has two ways to connect to a microcontroller.
- SWIM (Single Wire Interface Module) This is how we need to connect.
- It also can use SWD (Serial Wire Debug) this uses two communication wires clock and data (SWCLK / SWDIO)
So for us there are four wires to connect from the ST-Link and the STM8S103F.
- 3.3v
- GND
- RST
- SWIM
The connections are same-to-same.
Note!
Only have the ST-Link v2 connected when programming.
- Do not have any other VCC supply connected while the ST-Link v2 is connected.
- As my previous driver there is a pad for resetting the I2C address.
All files are available on my GitHub Page.
Programming [Configuration]
Once you are connected as in Step 2.
- We are ready to start programming.
When you open "ST Visual Programmer", it is important that you have the correct Microcontroller selected.
- Make sure STM8S103F3 is selected in the dropdown box in the toolbar at the top. (A)
- This can also be selected on the configuration dialog window. (B)
Lets use the Configuration window.
- Click Configure on the toolbar at the top. (1)
- Choose: "Configure ST Visual Programmer". (2)
- This will open the "Configuration" Dialog Window.
- Select: "Hardware" to ST-LINK. (3)
- Select: "Port" to USB. (4)
- Select: "Programming Mode" to SWIM. (5)
- Select: "Device" to STM8S103F3 (6)
- Click "OK" when done. (7)
Notes!
I have purchased several STM8S103F Modules off eBay, so far I have had both variants: STM8S103F2 and STM8S103F3. Only the STM8S103F3 work for this project.
- The difference between the two is the size of EEPROM Memory.
Programming [PROGRAM MEMORY]
Then we do PROGRAM MEMORY.
- Select the PROGRAM MEMORY tab. (1)
- Click "File" on the Toolbar. (2)
- Select: "Open". (3)
In the "Open" dialog window:
- Browse to where you downloaded the tims_i2c_duel_motor_driver.s19 file. (4)
- Select it. (5)
- Click "Open". (6)
When ready:
- Click: "Program" on the toolbar.
- Select: "Current tab"
The file should upload to the Microcontroller.
Programming [DATA MEMORY]
Then we do DATA MEMORY.
- Select the DATA MEMORY tab. (1)
- Click "File" on the Toolbar. (2)
- Select: "Open". (3)
In the "Open" dialog window:
- Browse to where you downloaded the DATA_MEMORY.s19 file. (4)
- Select it. (5)
- Click "Open". (6)
When ready:
- Click: "Program" on the toolbar. (7)
- Select: "Current tab" (8)
The file should upload to the Microcontroller.
Cant Program It [Locked]
There is a third tab "OPTIONS BYTE"
- You can do similar to the last step using file OPTION_BYTE.s19
Or
If you get error microcontroller locked you need to do the following:
- Select the OPTIONS BYTE tab. (1)
- Change the "ROP" setting to say "Read Out Protection OFF". (2)
When ready:
- Click: "Program" on the toolbar. (3)
- Select: "Current tab" (4)
You should be able to do Steps 4 and 5 now.
Commands
The Motor Driver Board is controlled by commands on the I2C Bus.
- The Board can be connected to any microcontroller that has I2C.
- All commands start with any byte, this is because I have an ESP8266 and they haven't fixed the first data byte issue. When sending data via the I2C on the ESP8266 the first byte gets 0x80 added to it. So my driver ignores the first byte sent. For simplicity I will use '#' as the first character of all commands sent to the Motor Driver.
- The second byte is the Identifier that tells the driver the type of command. All commands are from the second byte onwards. I should say "char" as all commands are sent as ASCII characters.
Global Command 'M'
- M1 = Motor A is the current motor that will be adjusted.
- M2 = Motor B is the current motor that will be adjusted.
- M3 = Motor A and B be adjusted.
- A global command can be used on its own (preceded with '#') or at the end of other commands (preceded with a space)
'C' Commands (settings):
- C1 = Sets the Current Position to 0 (zero). Just use C1.
- C6 B<value> = Set: Has a Station Position. Boolean = 1 or 0 (true or false).
- C7 P<value> = Set the Forward Station Position value. value = a 16 bit value, Sets the Station Position while moving Forward.
- C8 P<value> = Set the Reverse Station Position value. value = a 16 bit value, Sets the Station Position while moving in Reverse.
- C10 A<value> = Set I2C Address. value = 1 (0x00) to 127 (0x7F). This is to change the I2C Address of the device.
- C13 B<value> = Set I2C Speed. Boolean; Is it 400k. value = 1 or 0 (true or false). 1 = 400000, 0 = 100000.
All setting are saved in non volatile memory.
- no need to apply settings every time power it connected.
'S' Command (Speed):
- S<Value> = Set the Speed 0 to 1000. This is a PWM value. So 500 would be 50% on 50% off square wave.
'F' Commands (Forward):
- F0 = Stop. Can just use 'F' as the buffer will be full of zeros.
- F1 = Forward at a current Speed.
- F1 S<value> = Forward at a set Speed.
'R' Commands (Reverse):
- R0 = Stop. Can just use 'R' as the buffer will be full of zeros.
- R1 = Reverse at a current Speed.
- R1 S<value> = Reverse at a set Speed.
'G' Commands (Go to):
- G0 = Stop. Can just use 'G' as the buffer will be full of zeros.
- G1 P<value> S<value> = Move to an absolute Position Forward (Positive) of zero at a Speed.
- G2 P<value> S<value> = Move to an absolute Position Reverse (Negative) of zero at a Speed.
- Settings that set a position are only valid with motors that have a Quadratic Encoder.
The 'G' code P<value> can be negative values. So why have G1 and G2?
- To keep options open.
- All positions are relative to a zero.
- When writing your own code to control the Motors you may only have the option of Unsigned Integers.
- You may want to only use Unsigned Integers to save memory.
A Typical 4 Motor Setup
- One Microcontroller.
- Two Driver Boards.
- Four Motors With Quadratic Encoders.
- Each Motor has a Station to set the the zero position.
What I Have Used Them on So Far
My GitHub has example Arduino Code for the ESP8266 I have used.
Some video of them being used.