RPM Measurement (Tachometer)
RPM Measurement (Tachometer)
OVERVIEW OF PROJECT:
The program initializes the peripherals, GPIO, UART, I2C, and TIM2 timer. It then captures the input signal
of the timer using TIM2_IRQn interrupt configuration and calculates the frequency and RPM (Revolutions
Per Minute) of the input signal. It displays the RPM value on an I2C LCD screen. Additionally, it uses an
ESPDataLogger library to initialize and connect the microcontroller to a Wi-Fi hotspot.
BLOCK DIAGRAM:
COMPONENTS REQUIRED:
1. STM32-NUCLEO411RE.
2. LM393 IR SENSOR.
3. DC MOTOR 12V.
4. 16X2 LCD DISPLAY.
5. I2C MODULE.
6. CONNECTING WIRES.
7. ESP8266 WIFI MODULE.
8. BREAD BOARD
9. DC MOTOR SPEED CONTROL MODULE
HARDWARE INFORMATION:
LM393 IR SENSOR:
NOTE:
Explanation for white paper pasted on Motor body:
It is universal that black colour absorbs the entire radiation incident on it and white colour
reflects the entire radiation incident on it. The surface of the object can be divided into two types:
reflective surface and non-reflective surface. If the surface of the object is reflective in nature i.e., it
is white or other light colour, most of the radiation incident on it will get reflected back and reaches
the photodiode. Depending on the intensity of the radiation reflected back, current flows in the
photodiode. If the surface of the object is non-reflective in nature i.e., it is black or other dark
colour, it absorbs almost all the radiation incident on it.
CONNECTIONS:
Make connections as per table given below.
NOTE: LCD and I2C are connected together via bread board.
IOC CONFIGURATIONS:
Create a new project, use default configurations.
Now go to pin out configurations enable Timer-2 in Timers section, select channel-1 as Input
capture direct mode.
Now go to parameter settings, select prescaler, counter period (ARR), polarity selection, as follows.
Prescaler = 50000-1
Counter period = 0xffff-1
Polarity selection = Raising Edge
In pin out view enable PA5 pin as Timer-2 channel-1.
Go to clock configuration, select HSE >> HCLK-50MHz, as Timer-2 max-frequency that connected to
APB-2 is 50MHz.
By using reference manual of STM32 and correlate the bus with timer used.
TIM2 is connected to APB1 Bus which Clock frequency Max up to 50MHz.
Click on the NVIC Settings and Enable the TIM2 global interrupt
Now enable I2C-1 by clicking on connectivity, select I2C.
Save the configurations and generate code.
PROGRAM CODE:
EXPLANATION:
The code begins with including the necessary header files for the project, which includes the main header
file, standard input/output library, string library, a custom library for interfacing with the ESP8266 module,
and the I2C LCD library.
After this, some macros are defined to specify the clock and prescalar values of the timer
used to measure the RPM. Then, some global variables are declared to store the values of the captured
input capture (IC) values, the calculated speed and RPM values, and a buffer to store the RPM value for
sending to the ESP8266 module.
Next, a callback function for the input capture interrupt is defined. This function is called
when the timer measures a pulse from the IR sensor. The function reads the values of the input capture
register and calculates the frequency and RPM of the motor. Finally, the calculated RPM value is stored in
the buffer.
In the main function, the peripherals are initialized, including the GPIO, timer, UART, and I2C
peripherals. Then, the timer is started, and the input capture interrupt is enabled. After a delay of 1
second, the LCD is initialized, and the ESP8266 module is initialized with the SSID and password of the Wi-Fi
network to which it will connect.
In an infinite loop, the RPM value is displayed on the LCD by converting the value to ASCII
characters and sending them to the LCD. And also sends to ESP8266 module.
IC_Val1 is captured at first raising edge of pulse generated by sensor, and IC_Val2 is captured at second
raising edge of pulse.
By using “Difference”, frequency is calculated by TIMCLOCK(HSE Timer clock) and PRESCALAR.
refClock(TIM2 clock frequency) = TIMCLOCK / PRESCALAR
frequency = refClock/Difference
RPM calculated using frequency, 60 = time in seconds per 1minute, PPR =1(uses one interrupt per 1
revolution)
RPM = (60 * frequency) / PPR