Speak to Rotate a Stepper Motor

 Objective

Voice recognition has been used extensively by smart home industries to provide an easier, hassle-free way of communicating commands to smart devices. This project uses a PIC24F microcontroller to receive voice commands and based on the command, rotate the stepper motor to a new position. The voice command is the name of the color that the arrow should point to. Pill dispensers based on voice commands for the visually impaired can be one of many end-products produced by this project.

 Materials

Hardware Tools

Tool About Purchase
Explorer1632-50px.png
Explorer 16/32
Development Board
ICD3-50px.png
MPLAB® ICD 3
In-Circuit Debugger

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
MPLAB® X
Integrated Development Environment
swtool-28px.png
MPLAB® Code Configurator
Dynamic Code Generation
MPLAB® XC16
C Compiler

Exercise Files

File Download
Installation
Instructions
Windows Linux Mac OSX
Project and Source Files

 Connection Diagram

StepperProjectBlockDiagram.png

 Procedure

The project uses a PIC24Fxxx microcontroller, a Stepper 2 click™ board, and a SpeakUp 2 click board manufactured by MikroElectronika®. The Stepper 2 click behaves as a stepper motor driver. The SpeakUp 2 click is a voice recognition board for sensing the commands provided as audio input.

The major steps to implement this project are as follows:

  1. Setup Project Resources
  2. Setup the Universal Asynchronous Receiver Transmitter (UART) module
  3. Configuring the Pin Manager
  4. Generate code
  5. Adding functionality
  6. Programming the device

1

Setup Project Resources

Launch MPLAB® X IDE. The following image shows the startup page.

LandingPage.png

a

Create new project

Create a new Standalone project in MPLAB X IDE for a PIC24FJ128GA010. If this is your first time using MPLAB X IDE, follow the instructions in the "Create a Standalone Project" article.

b

Launch MPLAB Code Configurator (MCC)

Open the MPLAB Code Configurator (MCC) under the Tools > Embedded menu of MPLAB X IDE as shown in the image below.

MCC.png

c

Setup Project Resources

The System Module needs to be set up. Click on System Module in the Project Resources list.

SystemModule.png

The System section will appear. In this section, the oscillator settings and the configuration settings are selected.

d

Oscillator

Under the Internal Oscillator section, select a frequency of 4000000 Hz and the Primary Oscillator as the source from the drop-down menu. This enables the internal 4 MHz primary oscillator as the system clock.

SystemModuleSettings.png

e

Configuration

Each configuration setting can be changed under the Register tab of the System window.

MCC generates default settings. Verify that the generated settings are set to the default values as shown here.

Registers1.png
Registers2.png

2

Setup the UART module

a

Setup UART2 module

The UART2 needs to be set up. Click on the UART2 label in the Project Resources menu to make the UART2 setup screen appear.

ProjectResources2.png

UART is used for UART communication between the receiver PIC® controller and the transmitter SpeakUp 2 click. Click on the Enable UART checkbox and verify that the Baud Rate is set to 19200 as shown here.

UARTSettings.png

3

Configuring the Pin Manager

a

Set the pins

There are multiple pins that need to be set in the Pin Manager.

Pins1.png
Pins2.png

It should look like the image below when completed:

PinsFinal.png

b

Custom name for pins

Close the Pin Manager and click on Pin Module in the Project Resources section.

PinModule.png

Rename the pins according to the following diagram. To rename a pin, click on the Custom Name for the pin and type in the new name. Make sure all the Analog and WPU boxes are not selected. If selected, click on the box to deselect it.

PinModuleFinal.png
Package.png

4

Generate code

Click on the Generate Code button to have MCC create the software libraries for this project.

Generate.png

The project now has both generated header files and source files. It also has a generated main.c file.

CreatedFiles.png

5

Adding functionality

Functionality.png

There are four files that need to be attached to the project to make use of their functionalities. Download the project ZIP file and extract the contents. Then, move or copy the files (stepper.c, stepper.h, speakup.c, and speakup.h) to the your project's working folder.

a

stepper.c

Once the file is attached to the project, the stepper motor control functions can be called to utilize its functionality.

a. Stepper_Initialize() - initializes the Stepper Motor to function properly.

  • i. Reset input is disabled – RST set low.
  • ii. Enable input is enabled – EN set high.

b. Generate_pulse() - used to generate a pulse.
The function generates a single pulse on the step signal connected to the motor control click board for energizing the motor to rotate by one step.

The following snippet issues the pattern for generating a pulse on the output pin ST:


ST_SetHigh();
__delay_ms(750);
ST_SetLow();
__delay_ms(750);

c. Rotate_fixed_number_of_steps(N_Number_Of_Steps) - rotates the stepper motor by a certain number of steps.

The following snippet rotates the stepper motor by a set number of steps given as an input to the function as function parameters:


for(loop=0; loop<(NnumberOfSteps)*250; loop++)
{
Generate_pulse();
}

d. Stepper_Enable() and Stepper_Disable() - enable and disable the stepper motor respectively.

The following snippet enables the stepper motor:


RST_SetHigh();
EN_SetLow();

The following snippet disables the stepper motor:


RST_SetLow();
EN_SetHigh();

e. Rotate_Clockwise() and Rotate_Anticlockwise() - rotate the stepper motor in the respective direction.

The following snippet rotates the stepper motor in the clockwise direction:


DIR_SetLow();

The following snippet rotates the stepper motor in the anticlockwise direction:


DIR_SetHigh();

f. Rotate_Stepper(New_Value_Of_Stepper) - call to rotate the stepper motor to a desired new position.

In the project implemented here, the complete revolution of the stepper motor is divided into eight different slots with each slot numbered starting from one to eight. The logic behind which direction the stepper motor rotates and by how many steps is based on the shortest path to reach the destination slot from the initial slot.

Wheel.png

One important observation we can make when we see the circle in the figure above is that ideally when the destination slot is greater in value than the initial slot, the stepper motor rotates clockwise to reach the destination provided the number of slots it must rotate is less than a value. This value is calculated as:

Number of slots before it is better for the stepper motor to rotate in the opposite direction (i.e. counterclockwise) = Total number of slots / 2 = 8/2 =4.

For example:

  • If the initial slot = 1 and the destination slot = 2, it is most wise to rotate the stepper motor clockwise and the number of slots to rotate = destination slot - initial slot = 2 -1 = 1.
  • If the initial slot = 1 and the destination slot = 8, and as the rule goes that we rotate clockwise, the stepper motor must rotate by the number of slots = 8 – 1 = 7 which is a long path. Suppose, we rotate the stepper motor counterclockwise, the number of slots to rotate = total number of slots – (destination slot – initial slot) = 8 – (8-1) = 8 – 7 = 1, which takes less time than going around the whole circle.
RotatingCircle.png

Following the deductions above, when the destination slot is lesser in value than the initial slot, the stepper motor rotates anti-clockwise to reach the destination provided the number of slots it must rotate is less than a value. This value is calculated as:

Number of slots before it is better for the stepper motor to rotate in the opposite direction than the usual = Total number of slots / 2 = 8/2 =4.

For example:

  • If the initial slot = 2 and destination slot = 1, it is most wise to rotate the stepper motor anti-clockwise and the number of slots to rotate = destination slot – initial slot = 2 – 1 = 1.
  • If the initial slot = 8 and destination slot = 1, and as the rule goes that we rotate anti-clockwise, the stepper motor must rotate by the number of slots = 8 – 1 = 7 which is a long path. Suppose, we rotate the stepper motor clockwise, the number of slots to rotate = total number of slots – (initial slot – destination slot) = 8 – (8-1) = 8 – 7 =1.
Wheel3.png

Denotation:

D = Destination slot
I = Initial slot
N = Total number of slots

There are five conditions that can occur with the stepper motor rotation:

  • i. If ( I < D ) and ( D – I ) < 4,
    • Rotate clockwise for ( D – I ) slots.
  • ii. If ( I < D ) and ( D – I ) >= 4,
    • Rotate anticlockwise for ( N - (D – I) ) slots.
  • iii. If ( I > D ) and ( I – D ) < 4,
    • Rotate anticlockwise for ( D – I ) slots.
  • iv. If ( I > D ) and ( I – D ) >= 4,
    • Rotate clockwise for ( N – ( I – D ) ) slots.
  • v. If ( I = = D )
    • Do not rotate.

The following snippet shows what happens under each circumstance mentioned above:


Stepper_Enable(); //Stepper motor is enabled
Rotate_Clockwise(); //Direction of rotation is chosen
numberOfSteps= 8 - (initialValueOfStepper-newValueOfStepper); //Number of steps to rotate
Rotate_fixed_number_of_steps(numberOfSteps); //Provide control for rotating
Stepper_Disable(); //Disable the stepper motor
initialValueOfStepper=newValueOfStepper; //Change new initial slot as destination slot

b

stepper.h

This header file has function prototypes of the functions that need to be used in the stepper.c file. It also defines the initial position of the stepper motor.

c

speakup.c

Once the file is attached to the project, the SpeakUp click functions can be called to utilize its functionality.

6

Programming the device

You can now program your device using the Make and Program Device icon Main_Program_Target_Project.png

 Results

 Voice-Controlled Stepper Motor Demo

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.