The main objective of this project is to carry out a study of a small-power DC motor. This study involves measuring the motor’s speed, acceleration, response time, and braking under different waveform types.
First, an encoder is designed using a plastic disc, black cardboard, and aluminum foil. This disc is divided into twelve sectors that, when rotating, allow the measurement of the motor’s rotational frequency through an infrared optical sensor (CNY70).
To ensure a cleaner signal from the encoder, an inverter is also used (74HC14). This results in sharper signal edges, making them easier to detect. To allow waveform selection generated by the micro-controller, a 4x3 keypad is added to the project. Different operating modes are assigned based on the pressed buttons.
A controller is needed to supply power to the motor; for this purpose, the L298N driver is selected. This device allows us to control the effective voltage received by the motor by sending a PWM signal generated by the micro-controller. The selected motor operates at 5V, so the driver is powered with the same voltage from one of the board’s pins.
Additionally, the application must be capable of measuring the motor’s current consumption. For this, a module with a current sensor is used (ACS712), which allows current values to be recorded via a voltage output. Due to the sensor’s low resolution, a non-inverting operational amplifier (LM358N) is used to increase the measurement variation range.
These voltage values are converted to digital values through the micro-controller’s built-in ADC. Once converted and the corresponding current values are calculated, they are sent via serial port to MATLAB along with the other variables of interest. MATLAB is responsible for logging all the data and graphing them appropriately to enable the study of the motor. Additionally, the obtained data is saved in an Excel file for potential future processing.
As an extra feature, the design includes an OLED display that continuously shows the current, speed, and acceleration values of the motor, as well as the user's selected settings. With these features, the developed application can be useful in the field of education and for electronics enthusiasts. It offers a highly visual and educational way to teach basic motor control concepts while evaluating certain technical characteristics of motors.
It is worth noting that the developed design can analyze any DC motor with a supply voltage between 5 and 35 volts.
REAL EXECUTION OF THE IMPLEMENTATIONMICRO-CONTROLLER RESOURCEUSEAND SWEXPLANATION
The following section details the different configurations of the micro-controller’s resources that made the development of the project possible.
First, it was necessary to generate a PWM signal to control the motor output. This micro-controller model includes several timers with configurations specifically designed for that purpose. The design, therefore, involved determining an appropriate frequency for this signal. Initially, a value of 20 kHz was chosen, but it proved to be too high considering the motor’s large time constant. Due to this time constant, a frequency of 200 Hz was ultimately selected, with a minimum duty cycle of 20%. This ensures that the motor can follow all waveform types and has enough torque to start with the attached disc. The timer responsible for generating this signal is TIM3.
Next, a peripheral was needed to count the signal edges generated by the encoder. To address this, a pin was configured as an external interrupt source. This way, each time an edge occurs, the corresponding interrupt is triggered, and all edges are recorded in a variable named flancos
.
To iterate through the different values in the array containing the PWM information for the sine wave signal, another timing source was required. TIM6 was programmed with an overflow frequency of 20 Hz. Considering that the vector consists of 100 values, it was decided that the motor should complete one full cycle every 5 seconds to clearly observe the different signals.
However, to sample the signal carrying current information, a much higher sampling frequency was required than those previously selected. This sampling frequency was calculated—like the others—based on the Nyquist criterion. Since the motor's response time was to be analyzed, the inverse of this frequency had to be at least half of the motor’s time constant. This time constant is 283 microseconds, so, exceeding the minimum requirement, a value of 20 kHz was chosen (minimum required value: 14.13 kHz). The resource used for this is TIM2, and its overflow is configured as the ADC sampling trigger.
To calculate speed and acceleration, the Systick timer—by default configured to 1 ms—is used. The actual calculation is performed at 40 Hz with the help of an accumulator variable, since the speed changes at the same frequency as TIM6.
Additionally, another external interrupt is configured to sample the PWM signal sent to the motor for the purpose of analyzing the response time.
Regarding communications, two ports are configured: USART2 and I2C1. The former is used for communication with the computer and to send data to MATLAB, while the latter is used to transmit data to the OLED display.
main.cAn infinite loop waits for the reception of a signal (send = 1
). When this condition is met, the previously mentioned variables are sent, along with an additional variable called mode
. This variable is useful in the MATLAB program to select between different operating modes.
Next, within another loop, the functions used to communicate with the display and print data on it are included. These functions are defined in the various libraries attached to this report. The file names are: fonts.c
(character definitions), ssd1306.c
(functions for communication and configuration of the display controller), and their corresponding headers fonts.h
and ssd1306.h
.
It can be observed that the display information is refreshed 500 times slower than the data transmission over the serial port. This measure prevents communication failure within the application. If the display is refreshed at a higher rate, it becomes a bottleneck and causes problems with the execution of the MATLAB program.
In addition to the aforementioned libraries, the program also uses the files i2c-lcd.h
(communication) and waves.h
(sine wave).
Peripheralsand interfaces
The USART2 serial communication module is configured with a baud rate of 9600. Higher speeds caused communication errors. The word length is 8 bits, there is no parity bit, and a single stop bit is used.
For the configuration of the I2C1 port, fast mode communication is used. The clock frequency is set to 400 kHz, which is the appropriate value according to the manufacturer's specifications. The remaining parameters are left at their default values.
Interrupt Service RoutinesThe application consists of six different interrupt service routines. Most of them are very simple, and they are briefly described below:
- Systick (Appendix 1.8.2): As previously mentioned, this routine calculates the speed and acceleration variable
- EXTI0 (Appendix 1.8.6): Responsible for recording the signal edges produced by the encoder.
- EXTI4 (Appendix 1.8.5): Samples the PWM signal. If the edge is rising (
counter = 1
), the variable is set to a high level (value 1000 for better graph visibility), and if it is falling, it is set to - ADC1_IRQ: Triggers the ADC to convert the current value.
- TIM2_IRQ (Appendix 1.8.3): Since it determines the sampling frequency, it also calculates the current. Additionally, it updates the keypad values using a row-and-column-based algorithm. This update is performed with delays so that the microcontroller can detect the changes. Based on button changes, the
mode
andwave
variables (which select the PWM signal type) are updated. Finally, it triggers the transmission of data via the serial port. - TIM6_IRQ (Appendix 1.8.4): Using the
wave
variable, it selects the different motor operating modes. It also uses thenum
andvar
variables to generate all waveforms except the sine wave. This approach significantly reduces the microcontroller’s memory usage.
It was necessary to develop a MATLAB script to process the data and plot it appropriately. This program is specifically designed to work with and visualize large amounts of data. The code used in the project is included in the project's repo under the name RespuestaMotor.m. The code is structured as follows:
Initially, all existing variables are cleared, and the serial port is flushed to avoid potential execution errors. Then, a port
variable is created to enable communication, and its configuration parameters are defined. The port is opened, and the program enters its infinite loop.
First, the mode
variable is read, and using intermediate buffer variables that check whether a new button has been pressed or not, the program enters one of two different execution blocks. If the user stops the motor, no action is taken except to continuously re-read the port.
The first block corresponds to the motor response time analysis. In this block, memory is first reserved for the three vectors used during acquisition: current, PWM signal, and time. Then, using MATLAB’s built-in real-time timing functions (tic
and toc
), these vectors are filled with data coming from the serial port. Later, unused memory previously allocated is released (note that both acquisition time and port speed can vary, increasing the number of data points). The time elapsed between samples is calculated, and from this, the actual sampling frequency of the program is derived. Finally, the data is plotted.
The second block corresponds to the analysis of speed, acceleration, and current for each waveform type. At the start of this block, three "animated lines" are created. This type of function allows for live plotting as new values are added using the addpoints
function. A time variable is also declared for display on the graph. Then, within a loop limited to 20 seconds, all the mentioned variables are read and plotted in real-time (black: current, blue: speed, red: acceleration). Once the loop ends, all collected data is stored in vectors. A Gaussian moving average filter is applied to smooth out high-frequency noise, making the variations in the data easier to observe on the plots. Finally, all the data is plotted again and saved in an Excel file named Data.xlsx.
Comments