Intermediate
30 min

Get your brushed DC motor to run steadily with TMC7300 and STM32F415RG

Motor control – Under control!

DC Motor 22 Click with STM32 M4 clicker

Published May 03, 2023

Click board™

DC Motor 22 Click

Dev Board

STM32 M4 clicker

Compiler

NECTO Studio

MCU

STM32F415RG

Ensure efficient and reliable BDC operation

A

A

Hardware Overview

How does it work?

DC Motor 22 Click is based on the TMC7300, a low-voltage driver optimized for DC motor control from Analog Devices. The TMC7300 features high power density using integrated power MOSFETs and a complete integrated DC motor control logic for speed control, torque limits, and torque-controlled operation. It is rated for an operating voltage range from 2V to 11V and covers a broad spectrum of embedded applications with up to 2A (2.4A peak) current per motor terminal. This advanced driver ensures efficient and reliable operation for cost-effective and highly competitive solutions. As mentioned before, the TMC7300 operates via simple UART control, with the default baud rate of 115200bps for the data transfer, which allows the operation of two DC motors or a single motor with a double current. The motor is operated by an internal PWM generator,

where the desired duty cycle is programmed via the UART interface. The PWM acts like a dedicated voltage source for the motor, providing complete control over motor velocity and direction by setting the PWM voltage for each motor. This interface also controls the motor torque limit for both motors by setting a common current limit, allowing overloading to be avoided during acceleration safely or when a motor is blocked. There is an addition to this Click board™'s current sensing capability for channels A and B. Based on the populated sense resistors R4 and R5 and the MAX11645 analog-to-digital converter, the user can obtain information on the current value of the channels, processing this information via the I2C interface. In addition, it also uses several mikroBUS™ pins. The Enable feature through the EN pin of the mikroBUS™ socket offers a switch operation

to turn ON/OFF power delivery to the connected load, while the A0 and A1 pins select the UART interface address. It also has a complete set of diagnostic and protection capabilities that supports robust and reliable operation, like short-to-ground protection, short-to-power supply protection, and undervoltage detection reported through the INT pin of the mikroBUS™ socket. This Click board™ can operate with either 3.3V or 5V logic voltage levels selected via the VIO SEL jumper. This way, both 3.3V and 5V capable MCUs can use the communication lines properly. However, the Click board™ comes equipped with a library containing easy-to-use functions and an example code that can be used, as a reference, for further development.

dc-motor-22-click-hardware-overview

Features overview

Development board

STM32 M4 Clicker is a compact starter development board that brings the flexibility of add-on Click boards™ to your favorite microcontroller, making it a perfect starter kit for implementing your ideas. It comes with an onboard 32-bit ARM Cortex-M4 microcontroller, the STM32F415RG from STMicroelectronics, a USB connector, LED indicators, buttons, a JTAG connector, and a header for interfacing with external electronics. Thanks to its compact design with clear and easy-recognizable silkscreen markings, it provides a fluid and immersive working experience, allowing

access anywhere and under any circumstances. Each part of the STM32 M4 Clicker development kit contains the components necessary for the most efficient operation of the same board. In addition to the possibility of choosing the STM32 M4 Clicker programming method, using USB HID mikroBootloader, or through an external mikroProg connector for the STM32 programmer, the Clicker board also includes a clean and regulated power supply module for the development kit. The USB Mini-B connection can provide up to 500mA of current, which is more than enough to operate all

onboard and additional modules. All communication methods that mikroBUS™ itself supports are on this board, including the well-established mikroBUS™ socket, reset button, and several buttons and LED indicators. STM32 M4 Clicker is an integral part of the Mikroe ecosystem, allowing you to create a new application in minutes. Natively supported by Mikroe software tools, it covers many aspects of prototyping thanks to a considerable number of different Click boards™ (over a thousand boards), the number of which is growing every day.

STM32 M4 Clicker double side image

Microcontroller Overview

MCU Card / MCU

default

Architecture

ARM Cortex-M4

MCU Memory (KB)

1024

Silicon Vendor

STMicroelectronics

Pin count

64

RAM (Bytes)

196608

You complete me!

Accessories

DC Gear Motor - 430RPM (3-6V) represents an all-in-one combination of a motor and gearbox, where the addition of gear leads to a reduction of motor speed while increasing the torque output. This gear motor has a spur gearbox, making it a highly reliable solution for applications with lower torque and speed requirements. The most critical parameters for gear motors are speed, torque, and efficiency, which are, in this case, 520RPM with no load and 430RPM at maximum efficiency, alongside a current of 60mA and a torque of 50g.cm. Rated for a 3-6V operational voltage range and clockwise/counterclockwise rotation direction, this motor represents an excellent solution for many functions initially performed by brushed DC motors in robotics, medical equipment, electric door locks, and much more.

DC Motor 22 Click accessories image

Used MCU Pins

mikroBUS™ mapper

UART Address Selection
PA0
AN
NC
NC
RST
UART Address Selection
PB12
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Enable
PB0
PWM
Diagnostic
PB1
INT
UART TX
PC10
TX
UART RX
PC11
RX
I2C Clock
PB10
SCL
I2C Data
PB11
SDA
Power Supply
5V
5V
Ground
GND
GND
1

Take a closer look

Schematic

DC Motor 22 Click Schematic schematic

Step by step

Project assembly

STM32 M4 clicker front image hardware assembly

Start by selecting your development board and Click board™. Begin with the STM32 M4 clicker as your development board.

STM32 M4 clicker front image hardware assembly
GNSS2 Click front image hardware assembly
Prog-cut hardware assembly
GNSS2 Click complete accessories setup image hardware assembly
STM32 M4 Mini B Connector Access Clicker - upright/background hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
Necto image step 5 hardware assembly
Necto image step 6 hardware assembly
STM32 M4 Clicker HA MCU/Select Step hardware assembly
Necto No Display image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Debug Image Necto Step hardware assembly

Track your results in real time

Application Output via Debug Mode

1. Once the code example is loaded, pressing the "DEBUG" button initiates the build process, programs it on the created setup, and enters Debug mode.

2. After the programming is completed, a header with buttons for various actions within the IDE becomes visible. Clicking the green "PLAY" button starts reading the results achieved with the Click board™. The achieved results are displayed in the Application Output tab.

DEBUG_Application_Output

Software Support

Library Description

This library contains API for DC Motor 22 Click driver.

Key functions:

  • dcmotor22_set_motor_pwm This function sets the PWM duty cycle of the selected motor.

  • dcmotor22_get_motor_current This function reads the current consumption of the selected motor.

  • dcmotor22_reset_device This function resets the device by toggling the EN pin state.

Open Source

Code example

This example can be found in NECTO Studio. Feel free to download the code, or you can copy the code below.

/*!
 * @file main.c
 * @brief DC Motor 22 Click Example.
 *
 * # Description
 * This example demonstrates the use of DC Motor 22 click board by controlling the speed
 * of both motors over PWM duty cycle as well as displaying the motors current consumption.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver and performs the click default configuration.
 *
 * ## Application Task
 * Controls the motor speed by changing the PWM duty cycle every 5 seconds, and displays
 * the motors current consumption. The duty cycle ranges from -100% to +100%. 
 * Each step will be logged on the USB UART where you can track the program flow.
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "dcmotor22.h"

static dcmotor22_t dcmotor22;
static log_t logger;

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    dcmotor22_cfg_t dcmotor22_cfg;  /**< Click config object. */

    /** 
     * Logger initialization.
     * Default baud rate: 115200
     * Default log level: LOG_LEVEL_DEBUG
     * @note If USB_UART_RX and USB_UART_TX 
     * are defined as HAL_PIN_NC, you will 
     * need to define them manually for log to work. 
     * See @b LOG_MAP_USB_UART macro definition for detailed explanation.
     */
    LOG_MAP_USB_UART( log_cfg );
    log_init( &logger, &log_cfg );
    log_info( &logger, " Application Init " );

    // Click initialization.
    dcmotor22_cfg_setup( &dcmotor22_cfg );
    DCMOTOR22_MAP_MIKROBUS( dcmotor22_cfg, MIKROBUS_1 );
    if ( UART_ERROR == dcmotor22_init( &dcmotor22, &dcmotor22_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    if ( DCMOTOR22_ERROR == dcmotor22_default_cfg ( &dcmotor22 ) )
    {
        log_error( &logger, " Default configuration." );
        for ( ; ; );
    }

    log_info( &logger, " Application Task " );
}

void application_task ( void ) 
{
    static int16_t pwm_duty = 0;
    static int8_t pwm_duty_step = 50;
    float current;
    log_printf ( &logger, " Motor A\r\n" );
    if ( DCMOTOR22_OK == dcmotor22_set_motor_pwm ( &dcmotor22, DCMOTOR22_MOTOR_A, pwm_duty ) )
    {
        log_printf ( &logger, " PWM duty: %d\r\n", pwm_duty );
    }
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    if ( DCMOTOR22_OK == dcmotor22_get_motor_current ( &dcmotor22, DCMOTOR22_MOTOR_A, &current ) )
    {
        log_printf ( &logger, " Current: %.3f A\r\n\n", current );
    }
    Delay_ms ( 500 );
    log_printf ( &logger, " Motor B\r\n" );
    if ( DCMOTOR22_OK == dcmotor22_set_motor_pwm ( &dcmotor22, DCMOTOR22_MOTOR_B, pwm_duty ) )
    {
        log_printf ( &logger, " PWM duty: %d\r\n", pwm_duty );
    }
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    if ( DCMOTOR22_OK == dcmotor22_get_motor_current ( &dcmotor22, DCMOTOR22_MOTOR_B, &current ) )
    {
        log_printf ( &logger, " Current: %.3f A\r\n\n", current );
    }
    Delay_ms ( 500 );
    
    if ( ( ( pwm_duty + pwm_duty_step ) > DCMOTOR22_MAX_PWM ) || ( ( pwm_duty + pwm_duty_step ) < DCMOTOR22_MIN_PWM ) ) 
    {
        pwm_duty_step = -pwm_duty_step;
    }
    pwm_duty += pwm_duty_step;
}

int main ( void ) 
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif
    
    application_init( );
    
    for ( ; ; ) 
    {
        application_task( );
    }

    return 0;
}

// ------------------------------------------------------------------------ END
/*!
 * @file main.c
 * @brief DC Motor 22 Click Example.
 *
 * # Description
 * This example demonstrates the use of DC Motor 22 click board by controlling the speed
 * of both motors over PWM duty cycle as well as displaying the motors current consumption.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver and performs the click default configuration.
 *
 * ## Application Task
 * Controls the motor speed by changing the PWM duty cycle every 5 seconds, and displays
 * the motors current consumption. The duty cycle ranges from -100% to +100%. 
 * Each step will be logged on the USB UART where you can track the program flow.
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "dcmotor22.h"

static dcmotor22_t dcmotor22;
static log_t logger;

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    dcmotor22_cfg_t dcmotor22_cfg;  /**< Click config object. */

    /** 
     * Logger initialization.
     * Default baud rate: 115200
     * Default log level: LOG_LEVEL_DEBUG
     * @note If USB_UART_RX and USB_UART_TX 
     * are defined as HAL_PIN_NC, you will 
     * need to define them manually for log to work. 
     * See @b LOG_MAP_USB_UART macro definition for detailed explanation.
     */
    LOG_MAP_USB_UART( log_cfg );
    log_init( &logger, &log_cfg );
    log_info( &logger, " Application Init " );

    // Click initialization.
    dcmotor22_cfg_setup( &dcmotor22_cfg );
    DCMOTOR22_MAP_MIKROBUS( dcmotor22_cfg, MIKROBUS_1 );
    if ( UART_ERROR == dcmotor22_init( &dcmotor22, &dcmotor22_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    if ( DCMOTOR22_ERROR == dcmotor22_default_cfg ( &dcmotor22 ) )
    {
        log_error( &logger, " Default configuration." );
        for ( ; ; );
    }

    log_info( &logger, " Application Task " );
}

void application_task ( void ) 
{
    static int16_t pwm_duty = 0;
    static int8_t pwm_duty_step = 50;
    float current;
    log_printf ( &logger, " Motor A\r\n" );
    if ( DCMOTOR22_OK == dcmotor22_set_motor_pwm ( &dcmotor22, DCMOTOR22_MOTOR_A, pwm_duty ) )
    {
        log_printf ( &logger, " PWM duty: %d\r\n", pwm_duty );
    }
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    if ( DCMOTOR22_OK == dcmotor22_get_motor_current ( &dcmotor22, DCMOTOR22_MOTOR_A, &current ) )
    {
        log_printf ( &logger, " Current: %.3f A\r\n\n", current );
    }
    Delay_ms ( 500 );
    log_printf ( &logger, " Motor B\r\n" );
    if ( DCMOTOR22_OK == dcmotor22_set_motor_pwm ( &dcmotor22, DCMOTOR22_MOTOR_B, pwm_duty ) )
    {
        log_printf ( &logger, " PWM duty: %d\r\n", pwm_duty );
    }
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    if ( DCMOTOR22_OK == dcmotor22_get_motor_current ( &dcmotor22, DCMOTOR22_MOTOR_B, &current ) )
    {
        log_printf ( &logger, " Current: %.3f A\r\n\n", current );
    }
    Delay_ms ( 500 );
    
    if ( ( ( pwm_duty + pwm_duty_step ) > DCMOTOR22_MAX_PWM ) || ( ( pwm_duty + pwm_duty_step ) < DCMOTOR22_MIN_PWM ) ) 
    {
        pwm_duty_step = -pwm_duty_step;
    }
    pwm_duty += pwm_duty_step;
}

int main ( void ) 
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif
    
    application_init( );
    
    for ( ; ; ) 
    {
        application_task( );
    }

    return 0;
}

// ------------------------------------------------------------------------ END

Additional Support

Resources

Love this project?

'Buy This Kit' button takes you directly to the shopping cart where you can easily add or remove products.