Intermediate
30 min

Achieve versatility in motor control with STSPIN250 and ATmega328P

Unleash the power of brushed DC motor control!

STSPIN250 Click with Arduino UNO Rev3

Published Feb 14, 2024

Click board™

STSPIN250 Click

Dev Board

Arduino UNO Rev3

Compiler

NECTO Studio

MCU

ATmega328P

Experience enhanced motor performance with our brushed DC motor driver, optimizing efficiency, reducing wear, and extending the operational lifespan of your motors

A

A

Hardware Overview

How does it work?

STSPIN250 Click is based on the STSPIN250, a low voltage DC brushed motor driver by STMicroelectronics. This device integrates two full-bridge MOSFET channels in a parallel configuration, sustaining up to 2.6A. This IC targets battery-powered applications, featuring optimizations toward lowered power consumption. It has a PWM current controller with a fixed OFF time, during which the current decay sequence is performed. This effectively limits the current through the connected load (motor). The OFF (decay) time is approximately 40 µs on this Click board™. The PWM current controller compares the voltage across the sense resistor (VSENS) and the VREF voltage, which is adjustable on STSPIN250 click. When VSENS exceeds the VREF voltage, the current limiting is triggered, and the OFF timer starts counting. The decay sequence is performed. Knowing the RSENS, it can be easily calculated how much voltage should be applied to the REF pin of the STSPIN250 to limit the current according to ILOAD. For example, if 0.388V is applied at the VREF pin, the current limit will be maxed out to 2.58A. The onboard

potentiometer allows you to adjust the VREF voltage according to needs simply. The connected motor can be controlled by using these pins: PWM, PH, RST, and EN/INT. The PH pin determines the direction of the current. If set to a HIGH logic level, the current will flow in one direction, and vice-versa: when a LOW logic level is applied, the current will reverse its direction. This pin is routed to the AN pin of the mikroBUS™ and labeled as PH. PWM pin can be used to regulate the speed of the rotation. When this pin has a LOW logic level, the current will start circulating through the low-side (LS) MOSFETs and the motor coil. When there is a HIGH logic level on this pin, the current will flow through the load, depending on the logic state of the PH pin. A higher duty cycle percentage will result in higher angular speed. While the current decay sequence is performed, the logic states on the input pins will be disregarded until the decay timer expires. The decay time is fixed to approximately 40 µs on this Click board™. The STBY/RESET (RST) pin of the STSPIN250 is used to set both bridge outputs in HIGH-Z mode, disconnecting the power supply

from the output stage. This pin allows lower average power consumption as no current can flow from the power supply to the motor. This pin is routed to the RST pin of the mikroBUS™. The EN/FAULT (EN) pin has a double purpose: when set to a high logic level, it acts as a chip enable, allowing the device to operate. In the case of a fault condition on the IC, it will be asserted to a LOW logic level, acting as an interrupt pin. A restart attempt will be made after a timeout period defined by the external capacitor and resistor values. This pin is routed to the CS and INT pin of the mikroBUS™, allowing the host MCU to use both functions. These pins are labeled EN and FLT on the Click board™, respectively. The motor power supply can be connected to the input terminal labeled as VIN and should be within the range of 1.8V to 10V. The motor can be connected at the second terminal, between two poles labeled as A+ and A-. The Click board™ requires an external power supply for the motor to work. However, it also requires 3.3V from the mikroBUS™ rail.

STSPIN250 Click hardware overview image

Features overview

Development board

Arduino UNO is a versatile microcontroller board built around the ATmega328P chip. It offers extensive connectivity options for various projects, featuring 14 digital input/output pins, six of which are PWM-capable, along with six analog inputs. Its core components include a 16MHz ceramic resonator, a USB connection, a power jack, an

ICSP header, and a reset button, providing everything necessary to power and program the board. The Uno is ready to go, whether connected to a computer via USB or powered by an AC-to-DC adapter or battery. As the first USB Arduino board, it serves as the benchmark for the Arduino platform, with "Uno" symbolizing its status as the

first in a series. This name choice, meaning "one" in Italian, commemorates the launch of Arduino Software (IDE) 1.0. Initially introduced alongside version 1.0 of the Arduino Software (IDE), the Uno has since become the foundational model for subsequent Arduino releases, embodying the platform's evolution.

Arduino UNO Rev3 double side image

Microcontroller Overview

MCU Card / MCU

default

Architecture

AVR

MCU Memory (KB)

32

Silicon Vendor

Microchip

Pin count

28

RAM (Bytes)

2048

You complete me!

Accessories

Click Shield for Arduino UNO has two proprietary mikroBUS™ sockets, allowing all the Click board™ devices to be interfaced with the Arduino UNO board without effort. The Arduino Uno, a microcontroller board based on the ATmega328P, provides an affordable and flexible way for users to try out new concepts and build prototypes with the ATmega328P microcontroller from various combinations of performance, power consumption, and features. The Arduino Uno has 14 digital input/output pins (of which six can be used as PWM outputs), six analog inputs, a 16 MHz ceramic resonator (CSTCE16M0V53-R0), a USB connection, a power jack, an ICSP header, and reset button. Most of the ATmega328P microcontroller pins are brought to the IO pins on the left and right edge of the board, which are then connected to two existing mikroBUS™ sockets. This Click Shield also has several switches that perform functions such as selecting the logic levels of analog signals on mikroBUS™ sockets and selecting logic voltage levels of the mikroBUS™ sockets themselves. Besides, the user is offered the possibility of using any Click board™ with the help of existing bidirectional level-shifting voltage translators, regardless of whether the Click board™ operates at a 3.3V or 5V logic voltage level. Once you connect the Arduino UNO board with our Click Shield for Arduino UNO, you can access hundreds of Click boards™, working with 3.3V or 5V logic voltage levels.

Click Shield for Arduino UNO accessories 1 image

Used MCU Pins

mikroBUS™ mapper

Direction Control
PC0
AN
Chip Standby/Reset
PD2
RST
Chip Enable
PB2
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
PWM Speed Control
PD6
PWM
Flaut Reporting
PC3
INT
NC
NC
TX
NC
NC
RX
NC
NC
SCL
NC
NC
SDA
Power Supply
5V
5V
Ground
GND
GND
1

Take a closer look

Schematic

STSPIN250 Click Schematic schematic

Step by step

Project assembly

Click Shield for Arduino UNO front image hardware assembly

Start by selecting your development board and Click board™. Begin with the Arduino UNO Rev3 as your development board.

Click Shield for Arduino UNO front image hardware assembly
Arduino UNO Rev3 front image hardware assembly
Charger 27 Click front image hardware assembly
Prog-cut hardware assembly
Charger 27 Click complete accessories setup image hardware assembly
Arduino UNO Rev3 Access MB 1 - 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
Arduino UNO MCU 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

After loading the code example, pressing the "DEBUG" button builds and programs it on the selected setup.

Application Output Step 1

After programming is completed, a header with buttons for various actions available in the IDE appears. By clicking the green "PLAY "button, we start reading the results achieved with Click board™.

Application Output Step 3

Upon completion of programming, the Application Output tab is automatically opened, where the achieved result can be read. In case of an inability to perform the Debug function, check if a proper connection between the MCU used by the setup and the CODEGRIP programmer has been established. A detailed explanation of the CODEGRIP-board connection can be found in the CODEGRIP User Manual. Please find it in the RESOURCES section.

Application Output Step 4

Software Support

Library Description

This library contains API for STSPIN250 Click driver.

Key functions:

  • stspin250_set_ph - This function regulates Direction control pin state. It controls direction of the current

  • stspin250_enable - This function regulates enable pin state

  • stspin250_reset - This function regulates reset 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 
 * @brief Stspin250 Click example
 * 
 * # Description
 * This application enables usage of brushed DC motor driver with
 * the current limiting and current sensing.
 *
 * The demo application is composed of two sections :
 * 
 * ## Application Init 
 * Initialization driver init, PWM init and enable device
 * 
 * ## Application Task  
 * This is a example which demonstrates the use of Stspin250 Click board.
 * Stspin250 Click communicates with register via PWM interface.
 * It shows moving in the left direction from slow to fast speed
 * and from fast to slow speed.
 * Results are being sent to the Usart Terminal where you can track their changes.
 * 
 * @author Nikola Peric
 *
 */
// ------------------------------------------------------------------- INCLUDES

#include "board.h"
#include "log.h"
#include "stspin250.h"

// ------------------------------------------------------------------ VARIABLES

static stspin250_t stspin250;
static log_t logger;
uint8_t motor_direction = 1;

// ------------------------------------------------------ APPLICATION FUNCTIONS

void application_init ( void )
{
    log_cfg_t log_cfg;
    stspin250_cfg_t cfg;

    /** 
     * 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.

    stspin250_cfg_setup( &cfg );
    STSPIN250_MAP_MIKROBUS( cfg, MIKROBUS_1 );
    stspin250_init( &stspin250, &cfg );

    stspin250_enable( &stspin250, STSPIN250_DEVICE_ENABLE );
    stspin250_set_duty_cycle ( &stspin250, 0.0 );

    stspin250_pwm_start( &stspin250 );
    log_info( &logger, "---- Application Task ----" );
    Delay_ms( 500 );
}

void application_task ( void )
{
    static int8_t duty_cnt = 1;
    static int8_t duty_inc = 1;
    float duty = duty_cnt / 10.0;

    if ( motor_direction == 1 )
    {
        stspin250_set_ph( &stspin250, 1 );
        log_printf( &logger, "> CLOCKWISE <\r\n" );
    }
    else
    {
        stspin250_set_ph( &stspin250, 0 );
        log_printf( &logger, "> COUNTER CLOCKWISE <\r\n" );
    }

    stspin250_set_duty_cycle ( &stspin250, duty );
    log_printf( &logger, "Duty: %d%%\r\n", ( uint16_t )( duty_cnt * 10 ) );
    Delay_ms( 500 );

    if ( 10 == duty_cnt ) 
    {
        duty_inc = -1;
    }
    else if ( 0 == duty_cnt ) 
    {
        duty_inc = 1;
        if ( motor_direction == 1 )
        {
            motor_direction = 0;
        }
        else if ( motor_direction == 0 )
        {
            motor_direction = 1;
        }
    }
    duty_cnt += duty_inc;
}

void main ( void )
{
    application_init( );

    for ( ; ; )
    {
        application_task( );
    }
}


// ------------------------------------------------------------------------ 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.