Tim's I2C Dual Motor Driver

by Palingenesis in Circuits > Microcontrollers

749 Views, 6 Favorites, 0 Comments

Tim's I2C Dual Motor Driver

Tims I2C Dual Motor Driver 006.png
Tim's I2C Dual Motor Driver [Live Stream Follow Up]
Tims I2C Dual Motor Driver 007.png

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.

  1. 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.
  2. 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.

  1. Each board can have its own unique I2C Address.
  2. Not using the reserved Addresses over 100 in theory can be attached to one I2C bus. (Addresses 10 to 127 probably) 7-bit Address.
  3. The is a system to reset the I2C default address to 0x30.
  4. There are two sockets for the I2C to that the boards can be daisy-chained, end to end.
  5. 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.
  1. Version 2 is made the same way as this.
  2. There are some small improvements.
  3. I was unable to do the improvements just by updating the firmware.
  4. There was some pin reallocation so a new PCB needed to be made.

Supplies

PCBWay logo 1200x1000.png
Tims I2C Dual Motor Driver 004.png
Tims I2C Dual Motor Driver 005.png
Tims I2C Dual Motor Driver 001.png
Tims I2C Dual Motor Driver 002.png

I have done a new shared project at  for the Version 2.

  1. Tim's I2C Dual Motor Driver [Version 2] - Share Project - PCBWay

 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.

  1. Now and then, I will do a live stream to show how I do things.
  2. 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)

  1. 3 hours 45 minutes.

If you would like access to all files of the original version. I have also did a GitHub Page.

  1. Palingenesis/Tims-I2C-Dual-Motor-Driver: A Motor Driver to control two Motors via I2C (github.com)
  2. For the new version 2 PCB use the files from my shared Project at PCBway

The Microcontroller Is the STM8S103F3

Tims_I2C_Dual_Motor_Driver_Version_2_STM8S103F3_Pins.png
ST-Link v2.png
  1. According to the Data Sheet, this can run with 2.95 to 5.5 V operating voltage.
  2. Caution should be taken when programming, The ST-Link is 3.3v.
  3. 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.
  4. 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.

  1. Two Yellow LEDs, one for Motor Power and one for VCC.
  2. There is a red LED on the SDA line to show activity.
  3. 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

Tims I2C Dual Motor Driver Program Leads.png

To programme the STM8S103F3P6 you will need:

  1. ST Visual Develop IDE from STMicroelectronics.
  2. 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.

  1. Should you wish to install both ST Visual Develop IDE and Cosmic STM8 Compiler.
  2. 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
  3. 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.

  1. I have spaced them so that you should be able to hold a row of four 2.54mm male header pins on them.
  2. Programming only takes seconds.
  3. 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.

  1. SWIM (Single Wire Interface Module) This is how we need to connect.
  2. 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.

  1. 3.3v
  2. GND
  3. RST
  4. SWIM

The connections are same-to-same.

Note!

Only have the ST-Link v2 connected when programming.

  1. Do not have any other VCC supply connected while the ST-Link v2 is connected.
  2. As my previous driver there is a pad for resetting the I2C address.

All files are available on my GitHub Page.

  1. Palingenesis/Tims-I2C-Dual-Motor-Driver: A Motor Driver to control two Motors via I2C (github.com)

Programming [Configuration]

01 Configuration 001.png
01 Configuration 002.png
01 Configuration 003.png

Once you are connected as in Step 2.

  1. We are ready to start programming.

When you open "ST Visual Programmer", it is important that you have the correct Microcontroller selected.

  1. Make sure STM8S103F3 is selected in the dropdown box in the toolbar at the top. (A)
  2. This can also be selected on the configuration dialog window. (B)

Lets use the Configuration window.

  1. Click Configure on the toolbar at the top. (1)
  2. Choose: "Configure ST Visual Programmer". (2)
  3. This will open the "Configuration" Dialog Window.
  4. Select: "Hardware" to ST-LINK. (3)
  5. Select: "Port" to USB. (4)
  6. Select: "Programming Mode" to SWIM. (5)
  7. Select: "Device" to STM8S103F3 (6)
  8. 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.

  1. The difference between the two is the size of EEPROM Memory.

Programming [PROGRAM MEMORY]

02 Program 001.png
02 Program 002.png
02 Program 003.png
02 Program 004.png

Then we do PROGRAM MEMORY.

  1. Select the PROGRAM MEMORY tab. (1)
  2. Click "File" on the Toolbar. (2)
  3. Select: "Open". (3)

In the "Open" dialog window:

  1. Browse to where you downloaded the tims_i2c_duel_motor_driver.s19 file. (4)
  2. Select it. (5)
  3. Click "Open". (6)

When ready:

  1. Click: "Program" on the toolbar.
  2. Select: "Current tab"

The file should upload to the Microcontroller.

Programming [DATA MEMORY]

03 Data 001.png
03 Data 002.png
03 Data 003.png
03 Data 004.png

Then we do DATA MEMORY.

  1. Select the DATA MEMORY tab. (1)
  2. Click "File" on the Toolbar. (2)
  3. Select: "Open". (3)

In the "Open" dialog window:

  1. Browse to where you downloaded the DATA_MEMORY.s19 file. (4)
  2. Select it. (5)
  3. Click "Open". (6)

When ready:

  1. Click: "Program" on the toolbar. (7)
  2. Select: "Current tab" (8)

The file should upload to the Microcontroller.

Cant Program It [Locked]

04 Options 001.png
04 Options 002.png

There is a third tab "OPTIONS BYTE"

  1. 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:

  1. Select the OPTIONS BYTE tab. (1)
  2. Change the "ROP" setting to say "Read Out Protection OFF". (2)

When ready:

  1. Click: "Program" on the toolbar. (3)
  2. Select: "Current tab" (4)

You should be able to do Steps 4 and 5 now.

Commands

Tims I2C Dual Motor Driver 006.png

The Motor Driver Board is controlled by commands on the I2C Bus.

  1. The Board can be connected to any microcontroller that has I2C.
  2. 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.
  3. 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'

  1. M1 = Motor A is the current motor that will be adjusted.
  2. M2 = Motor B is the current motor that will be adjusted.
  3. M3 = Motor A and B be adjusted.
  4. 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):

  1. C1 = Sets the Current Position to 0 (zero). Just use C1.
  2. C6 B<value> = Set: Has a Station Position. Boolean = 1 or 0 (true or false).
  3. C7 P<value> = Set the Forward Station Position value. value = a 16 bit value, Sets the Station Position while moving Forward.
  4. C8 P<value> = Set the Reverse Station Position value. value = a 16 bit value, Sets the Station Position while moving in Reverse.
  5. C10 A<value> = Set I2C Address. value = 1 (0x00) to 127 (0x7F). This is to change the I2C Address of the device.
  6. 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.

  1. no need to apply settings every time power it connected.

'S' Command (Speed):

  1. 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):

  1. F0 Stop. Can just use 'F' as the buffer will be full of zeros.
  2. F1 = Forward at a current Speed.
  3. F1 S<value> = Forward at a set Speed.

'R' Commands (Reverse):

  1. R0 = Stop. Can just use 'R' as the buffer will be full of zeros.
  2. R1 = Reverse at a current Speed.
  3. R1 S<value> = Reverse at a set Speed.

'G' Commands (Go to):

  1. G0 = Stop. Can just use 'G' as the buffer will be full of zeros.
  2. G1 P<value> S<value> = Move to an absolute Position Forward (Positive) of zero at a Speed.
  3. G2 P<value> S<value> = Move to an absolute Position Reverse (Negative) of zero at a Speed.
  4. 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?

  1. To keep options open.
  2. All positions are relative to a zero.
  3. When writing your own code to control the Motors you may only have the option of Unsigned Integers.
  4. You may want to only use Unsigned Integers to save memory.

A Typical 4 Motor Setup

Tims_I2C_Dual_Motor_Driver_bb.png
  1. One Microcontroller.
  2. Two Driver Boards.
  3. Four Motors With Quadratic Encoders.
  4. Each Motor has a Station to set the the zero position.

What I Have Used Them on So Far

Tim's plays with motorized track [New Motor Driver]
Tim's Large Robot Arm [Part 3]

My GitHub has example Arduino Code for the ESP8266 I have used.

Some video of them being used.