Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

How to use an STM32 microcontroller to measure external signal frequencies

By Usman ali Butt April 6, 2021

In this tutorial, we’ll learn how to measure frequency by using microcontrollers. Unfortunately, microcontrollers are not equipped with frequency-measurement functions. So, for this project to be a success, we’ll need to interface multiple microcontroller peripherals before we can sufficiently measure the frequency of an external signal.

Two of the most common methods for measuring the frequency of an external frequency using microcontrollers are:

1. Reciprocal method. The simplest way to carry out this method is to detect the two rising edges of a frequency wave. At each rising edge, you’ll need to take a timestamp to figure out the exact time period in between (1 / time period is the frequency). However, this is not an easy task for a do-it-yourself project.

2. External signal counter. This method involves running a counter on the external frequency signal to detect the number of positive edges within a fixed amount of time. The frequency can be obtained by dividing the number of counts by the time.

However, if we increase the time (say, to  10 seconds), then the frequency = n. of counts / 10.

Using the STM32 microcontroller
For this project, we’ll use the STM32F0 discovery board, which is 3.3 volts tolerant. This means any external signal that’s greater than 3.3v must be decreased to match the microcontroller. Some of the STM32 input/output pins are up to 5 volts tolerant.

It’s possible to use them but, ideally, the signal should be the same tolerance level as the operating microcontroller.

Circuit diagram

 

You’ll require an external frequency generator for this project. We’ll debug the code using Keil’s µVision IDE and view the real-time results on the IDE simulator.

We’ll use STM32 TIMER2 to record the number of counts (by using the counter) from the internal clock between the two rising edges of the external signal. The is similar to the external signal counter method with a slight alteration. Instead of counting the external signal, we’ll use the internal clock, counting between the two rising edges of the external signal.

The code
First, you’ll need to configure the input pin and connect it to the timer. Below is the configuration function.

The STM32 libraries are in the form of structures. We’ll import two structures for this configuration (GPIO and NVIC non-volatile vector interrupt). The TIMER2 clock is enabled first. TIMER2 is connected to the Advanced peripheral bus1.

Next, the GPIO port-A clock must be enabled and the GPIO structure parameters should be defined. The GPIO-A1 is then selected, with the speed set to 50MHz. The pin is pulled up and the last pin mode should be set to the alternate function (AF). AF means the pin is neither input nor output — its function alternates depending on if assigned to the DMA, TIMER, RCC, or CCP, and so on.

The pin AF can be found in the microcontroller’s datasheet. For this project, we’re using GPIOA pin#1 in conjunction with the TIMER2. The statement, GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_2), assigns GPIOA pin#1 AF (which is assigned to the TIMER2). Lastly, the TIMER2 global interrupt is activated with the highest priority.

The TIMER configuration is somewhat complex. First, the TIMER channel is assigned to the TIMER structure. The polarity is set to the rising edge. Essentially, the TIMER is made sensitive to the rising edge. The ICSelection’s TIM Input 1, 2, 3, or 4 are selected and connected to the IC1, IC2, IC3, or IC4, respectively.

The prescaler is set to TIM_ICPSC_DIV1, which is capture performed each time the edge is detected on the capture input. The above diagram should help with the TIMER configurations — follow the TIMx_CH2 path. The TIMER2 CC2 (capture compare 2) module will be activated with the TIMER itself.

The “while (1)” loop above takes control of the system and performs nothing.

Where’s the frequency calculation?
The frequency is calculated in the interrupt handler routine because we defined the interrupt for the TIMER.

The duty cycle is given by:

 

We’ll use the same formula for our routine. The TIMER2 counter will count the number of steps during an active period and store it in the CCP1 register. The number of counts between the two rising edges is stored in the CCP2, which is equivalent to the period of the signal.

The above calculation is carried out in the TIMER2 interrupt handler.

First, we must clear the interrupt bit in the IRQ. Then, using the Tim_GetCapture2(Tim2) function, it’s possible to read the period of the signal from the CCP2 register. But check to ensure the period is valid. If so, extract the count in active time by reading the CCP1 register. 

After you’ve calculated the number of counts in a period, it’s easy to figure out the frequency. Simply divide the TIMER clock with the number of counts in a period. In our case, the IC2Value is variable.

Here’s a basic graphical explanation…

Note the output of the variables (the duty cycle and frequency) in the emulator of Keil’s ARM IDE. The frequency calculation depends on several variables, so it’s ideal to check the values in an emulator first and refine the system if necessary.  

There are certain limitations where it’s not possible to measure a frequency that’s greater than the clock frequency of the timer/counter. Using a prescaler and postscaler somewhat widens this measurement range.

Where to buy the parts?

  • STM32f0 discovery
  • Function generator

You may also like:


  • Clap sensor using STM32 MCU

  • Build affordable computer-vision applications with STM32 microcontrollers

  • STMicroelectronics releases new USB Type-C, PPS reference design

Filed Under: Microcontroller Projects

 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: 5G Technology
This Tech Toolbox covers the basics of 5G technology plus a story about how engineers designed and built a prototype DSL router mostly from old cellphone parts. Download this first 5G/wired/wireless communications Tech Toolbox to learn more!

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • mosfet driver problem in regeneration mode
  • What is the purpose of this relay?
  • Industrial Relay Board Design for Motorcycle Use
  • ADEM III ECM — No CAN Signal & Power Supply Issue
  • Snooping Around is All

RSS Electro-Tech-Online.com Discussions

  • Pic18f25q10 osccon1 settings swordfish basic
  • Sump pit water alarm - Kicad 9
  • Anyone jumped from Easyeda std to Easyeda pro?
  • turbo jet fan - feedback appreciated.
  • More fun with ws2812 this time XC8 and CLC

Featured – LoRa/LoRaWan Series

  • What is the LoRaWAN network and how does it work?
  • Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices

Recent Articles

  • How IoT network topologies work
  • The top five AI startups to watch in 2025
  • STMicroelectronics unveils SoC based on secure MCU
  • Nexperia’s 48 V ESD diodes support higher data rates with ultra-low capacitance design
  • Taoglas releases Patriot antenna with 18 integrated elements covering 600 to 6000 MHz

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe