PIC24F Voice Activated Stepper Motor

Last modified by Microchip on 2023/12/20 21:06

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

Software Tools

Exercise Files

Connection Diagram

stepper motor connection diagram

Stepper Functions Operation

Now let's look at how the Stepper functions operate.

Stepper_Initialize()

Call to initialize the Stepper Motor to function properly.

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

Generate_pulse()

Call 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);

Rotate_fixed_number_of_steps(N_Number_Of_Steps)

Call to rotate 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();
}

Stepper_Enable() and Stepper_Disable()

Call to 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();

Rotate_Clockwise() and Rotate_Anticlockwise()

Call to 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();

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.

Graphic of stepper motor divided into eight different slots

One important observation we can make when we see the circle in the figure 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.

Graphic of the stepper motor clockwise and counterclockwise rotation

Following the previous deductions, 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.

Graphic of the stepper motor clockwise and counterclockwise rotation

Denotation:

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

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

  • If ( I < D ) and ( D – I ) < 4,
    • Rotate clockwise for ( D – I ) slots.
  • If ( I < D ) and ( D – I ) >= 4,
    • Rotate anticlockwise for ( N - (D – I) ) slots.
  • If ( I > D ) and ( I – D ) < 4,
    • Rotate anticlockwise for ( D – I ) slots.
  • If ( I > D ) and ( I – D ) >= 4,
    • Rotate clockwise for ( N – ( I – D ) ) slots.
  • 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

Back to Top

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. Set up project resources
  2. Set up the Universal Asynchronous Receiver Transmitter (UART) module
  3. Configuring the Pin Manager
  4. Generate code
  5. Adding functionality
  6. Programming the device

Back to Top

Set Up Project Resources

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

MPLAB® X IDE window

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" page.

Launch MPLAB Code Configurator (MCC)

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

Tools > Embedded menu

Set Up Project Resources

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

System Module in the Project Resources list

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

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.

Internal Oscillator pane

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.

Register CONFIG1 pane

Register CONFIG2 pane

Back to Top


Set Up the UART 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.

Project Resources menu

UART is used for UART communication between the receiver PIC® microcontroller 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.

UART2 pane

Back to Top


Configuring the Pin Manager

Set the Pins

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

Pin Manager window

Pin Manager window

It should look as follows when completed:

Pin Manager window

Custom Name for Pins

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

Project Resources menu

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.

Pin Module pane

PIC24FJ128GA010 graphic

Back to Top


Generate Code

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

Resource Management Generate Code button

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

Projects tab files

Back to Top


Adding Functionality

adding functionality flow graphic

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 your project's working folder.

  • stepper.c
    • Once the file is attached to the project, the stepper motor control functions can be called to utilize its functionality.
  • 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.
  • speakup.c
    • Once the file is attached to the project, the SpeakUp click functions can be called to utilize its functionality.
  • speakup.h
    • This header file has function prototypes of the functions that need to be used in the speakup.c file.

Back to Top


Programming the Device

You can now program your device using the Make and Program Device icon program target button.

Back to Top


Results

Voice-Controlled Stepper Motor Demo

Back to Top