PIC24F Voice Activated 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
- Explorer 16/32 Development Board (DM240001-2)
- MPLAB® ICD 3 In-Circuit Debugger
- PIC24FJ128GA010
- Stepper 2 click™ board : MIKROE-1926
- SpeakUp 2 click board: MIKROE-1534
- Step Motor 5v : MIKROE-1530
Software Tools
Exercise Files
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.
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.
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.
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
…
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:
- Set up project resources
- Set up the Universal Asynchronous Receiver Transmitter (UART) module
- Configuring the Pin Manager
- Generate code
- Adding functionality
- Programming the device
Set Up Project Resources
Launch MPLAB X IDE. The following image shows the startup page.
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.
Set Up Project Resources
The System Module needs to be set up. Click on 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.
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.
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.
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.
Configuring the Pin Manager
Set the Pins
There are multiple pins that need to be set in the Pin Manager.
It should look as follows when completed:
Custom Name for Pins
Close the Pin Manager and click on Pin Module in the Project Resources section.
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.
Generate Code
Click on the Generate Code button to have MCC create the software libraries for this project.
The project now has both generated header files and source files. It also has a generated main.c file.
Adding Functionality
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.
Programming the Device
You can now program your device using the Make and Program Device icon .