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 |
---|---|---|
| | |
| |
- PIC24FJ128GA010
- Stepper 2 click™ board : MIKROE-1926
- SpeakUp 2 click™ board: MIKROE-1534
- Step Motor 5v : MIKROE-1530
Software Tools
Tool | About | Installers |
Installation
Instructions |
||
---|---|---|---|---|---|
Windows | Linux | Mac OSX | |||
MPLAB® X
Integrated Development Environment |
| | | | |
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
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:
- Setup Project Resources
- Setup the Universal Asynchronous Receiver Transmitter (UART) module
- Configuring the Pin Manager
- Generate code
- Adding functionality
- Programming the device
1
Setup Project Resources
Launch MPLAB® X IDE. The following image shows the startup page.
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.
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.
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.
3
Configuring the Pin Manager
a
Set the pins
There are multiple pins that need to be set in the Pin Manager.
It should look like the image below when completed:
b
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.
5
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 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.
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.
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.
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