Beginner
10 min

Control stepper motors in challenging environments with STSPIN220 and ATmega328P

Silent and highly accurate stepper motor driving solution capable of providing up to 1.5A per bridge

STSPIN820 Click with Arduino UNO Rev3

Published Mar 15, 2024

Click board™

STSPIN820 Click

Dev. board

Arduino UNO Rev3

Compiler

NECTO Studio

MCU

ATmega328P

Advanced 256 microsteps integrated motor driver with step-clock and direction interface

A

A

Hardware Overview

How does it work?

STSPIN820 Click is based on the STSPIN820, an advanced stepper motor driver from STMicroelectronics. The monolithic IC incorporates both the power MOSFETs and the logic circuitry necessary for simplified control and reliable functioning of the connected bipolar stepper motor. Featuring a microstepping sequencer that supports up to 256 microsteps, this IC can perform very smooth and silent movements. The step sequencer also controls the VREF voltage, allowing the current through coils to be optimal during the microstepping. In full-step mode, the maximum current through the coils is controlled by the VREF. As the sequencer propagates through the microsteps, the VREF is further reduced following a circular pattern, ensuring maximum power efficiency for each step. The STSPIN820 has two PWM current controllers with a fixed OFF time for each H-Bridge, during which the current decay sequence is performed. This effectively limits the maximum current through the connected motor phase. The OFF (decay) time is approximately 25 µs on this Click board™. The DECAY pin determines the decay mode. In mixed decay mode (DECAY pin at the LOW logic level), the decay period is divided into slow-decaying and

fast-decaying segments. The slow decay segment lasts 5/8 of the total OFF time, while the fast decay segment lasts 3/8 of the total OFF time. When the DECAY pin is at the HIGH logic level, the slow decay mode lasts for the entire OFF time. The PWM current controller compares the voltage across two sense resistors (VSENS1 and VSENS2) and the VREF voltage, which a potentiometer can adjust. When VSENS exceeds the VREF voltage, the current limiting is triggered, and the OFF timer starts counting. The STSPIN820 contains two independent H-Bridges, each controlling one phase of the bipolar stepper motor. The motor can be controlled by using these pins: DIR, STCK, RST, EN, and FAULT. The DIR pin determines the direction of the rotation. If set to a HIGH logic level, the internal microstepping counter will increase its value with each pulse coming through the STCK pin. The LOW logic level on this pin will cause the microstepping sequencer to decrease its counter. DIR pin is routed both to the mikroBUS™ pin AN (labeled as DIR). SW1, SW2, and SW3 switches on the Click board™ are used to determine the step size. They have MODE 1, MODE 2, and MODE 3 pins routed to them, respectively. All these switches can set the mode at any moment during the

operation. The STBY/RESET (RST) pin of the STSPIN820 is used to set both bridge outputs in HIGH-Z mode, disconnecting the power supply from the H-Bridges. 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 control logic circuitry will be reset when the standby mode is left. 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 both the CS and INT pins 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 7V to 45V. Stepper motor coils can be connected to A1, B2, B1, and A2 terminals. The Click board™ requires an external power supply for the motor to work. However, it also requires 3.3V from the mikroBUS™ rail.

STSPIN820 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

The 28BYJ-48 is an adaptable 5VDC stepper motor with a compact design, ideal for various applications. It features four phases, a speed variation ratio of 1/64, and a stride angle of 5.625°/64 steps, allowing precise control. The motor operates at a frequency of 100Hz and has a DC resistance of 50Ω ±7% at 25°C. It boasts an idle in-traction frequency greater than 600Hz and an idle out-traction frequency exceeding 1000Hz, ensuring reliability in different scenarios. With a self-positioning torque and in-traction torque both exceeding 34.3mN.m at 120Hz, the 28BYJ-48 offers robust performance. Its friction torque ranges from 600 to 1200 gf.cm, while the pull-in torque is 300 gf.cm. This motor makes a reliable and efficient choice for your stepper motor needs.

STSPIN820 Click accessories 1 image

Used MCU Pins

mikroBUS™ mapper

Direction Control
PC0
AN
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
Step Control
PD6
PWM
Fault Interrupt
PC3
INT
NC
NC
TX
NC
NC
RX
NC
NC
SCL
NC
NC
SDA
NC
NC
5V
Ground
GND
GND
1

Take a closer look

Click board™ Schematic

STSPIN820 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

1. Application Output - In Debug mode, the 'Application Output' window enables real-time data monitoring, offering direct insight into execution results. Ensure proper data display by configuring the environment correctly using the provided tutorial.

2. UART Terminal - Use the UART Terminal to monitor data transmission via a USB to UART converter, allowing direct communication between the Click board™ and your development system. Configure the baud rate and other serial settings according to your project's requirements to ensure proper functionality. For step-by-step setup instructions, refer to the provided tutorial.

3. Plot Output - The Plot feature offers a powerful way to visualize real-time sensor data, enabling trend analysis, debugging, and comparison of multiple data points. To set it up correctly, follow the provided tutorial, which includes a step-by-step example of using the Plot feature to display Click board™ readings. To use the Plot feature in your code, use the function: plot(*insert_graph_name*, variable_name);. This is a general format, and it is up to the user to replace 'insert_graph_name' with the actual graph name and 'variable_name' with the parameter to be displayed.

Software Support

Library Description

This library contains API for STSPIN820 Click driver.

Key functions:

  • stspin820_set_direction - This function sets the motor direction by setting the DIR pin logic state

  • stspin820_drive_motor - This function drives the motor for the specific number of steps at the selected speed

  • stspin820_reset_device - This function resets the device by toggling the RST pin

Open Source

Code example

The complete application code and a ready-to-use project are available through the NECTO Studio Package Manager for direct installation in the NECTO Studio. The application code can also be found on the MIKROE GitHub account.

/*!
 * @file main.c
 * @brief STSPIN820 Click Example.
 *
 * # Description
 * This example demonstrates the use of the STSPIN820 click board by driving the 
 * motor in both directions for a desired number of steps.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver and performs the click default configuration.
 *
 * ## Application Task
 * Drives the motor clockwise for 200 steps and then counter-clockwise with a 2 seconds
 * delay delay on driving mode change. All data is being logged on the USB UART where
 * you can track the program flow.
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "stspin820.h"

static stspin820_t stspin820;   /**< STSPIN820 Click driver object. */
static log_t logger;    /**< Logger object. */

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    stspin820_cfg_t stspin820_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.
    stspin820_cfg_setup( &stspin820_cfg );
    STSPIN820_MAP_MIKROBUS( stspin820_cfg, MIKROBUS_1 );
    if ( DIGITAL_OUT_UNSUPPORTED_PIN == stspin820_init( &stspin820, &stspin820_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    stspin820_default_cfg ( &stspin820 );
    
    log_info( &logger, " Application Task " );
}

void application_task ( void ) 
{
    log_printf ( &logger, " Move 200 steps clockwise, speed: slow\r\n\n" );
    stspin820_set_direction ( &stspin820, STSPIN820_DIR_CW );
    stspin820_drive_motor ( &stspin820, 200, STSPIN820_SPEED_SLOW );
    Delay_ms ( 2000 );
    
    log_printf ( &logger, " Move 200 steps counter-clockwise, speed: fast\r\n\n" );
    stspin820_set_direction ( &stspin820, STSPIN820_DIR_CCW );
    stspin820_drive_motor ( &stspin820, 200, STSPIN820_SPEED_FAST );
    Delay_ms ( 2000 );
}

int main ( void ) 
{
    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.