This example shows you how to measure the frequency of a pulse-width modulation (PWM) signal on an Arduino hardware using Arduino® External Interrupt block. The example uses the same board to generate and detect the PWM signal. The pin 2 is connected to the pin 3 of the Arduino Mega 2560 hardware. The pin 2 generates PWM signal. When there is a rising edge at pin 3, the External Interrupt block executes a function call to calculate the time between two consecutive rising edges of the signal, which in turn is used to calculate the frequency of the signal at pin 3.
Before you start this example, we recommend you complete the Getting Started with Arduino® Hardware example.
Supported Arduino board
PWM Source: The PWM block is configured to generate a PWM signal with a duty cycle of 50% and a frequency of 1000 Hz on pin 2 of the hardware. The model accepts the generated signal to calculate the frequency of the generated signal. This model measures the frequency of the signal generated by the same board. To measure the frequency of the signal generated by some other source, connect the external source to pin 3 of the hardware.
External Interrupt Handler: The External Interrupt block is configured to trigger an Interrupt Service Routine (ISR) at every rising edge of the PWM signal. When there is a rising edge on pin 3, the block executes the function call to calculate the time difference between two consecutive rising edges of the signal. The time difference is calculated using this formula and stored in the Time Difference datastore block.
TimeDiff = CTime - PTime
where: TimeDiff is the time lapse between two consecutive rising edges, PTime is the timestamp of the previous rising edge, and CTime is the timestamp of the current rising edge.
PWM Measurement: At each time step, this subsystem receives the time difference calculated by the External Interrupt Handler subsystem. If the time difference is equal to
0, it means that no signal is generated. If the time difference is greater than
0, the subsystem calculates the frequency using the formula, Frequency =
1e-6 / TimeDiff
Data Store Memory
The subsystem consist of the PreviousTime, CurrentTime, and TimeDifference Data Store Memory blocks.
1. Connect the micro end of the USB cable to the hardware and the regular end of the USB cable to the computer. Wait until the PWR LED on the hardware starts blinking.
2. Connect the pulse generator pin 2 to the external interrupt pin 3 of the Arduino Mega 2650 hardware. If you are using any other supported Arduino hardware, change the pin numbers by referring to Pin Mapping on Arduino Blocks.
1. Open the arduino_measure_frequency model in MATLAB® Command Window. This model is preconfigured to run on the Arduino Mega 2560. If you are using the Arduino Mega 2560, skip the remaining steps in this section.
2. On the Hardware tab, click the Hardware Settings button.
3. In the Configurations Parameters dialog box, select Hardware Implementation.
4. From the Hardware board list, select the type of Arduino hardware that you are using. Do not change any other settings.
5. In the model:
a. Modify the Pin number parameter of the PWM block to the pin that generates the PWM signal. In this example, the Pin Number parameter is set to
b. Modify the Pin number parameter of the External Interrupt block to the pin that generates the external interrupt. In this example, the Pin number parameter is set to
1. On the Hardware tab, click the Monitor & Tune button. This action builds, downloads, and runs the model on the Arduino hardware. The lower-left corner of the model window displays the status while Simulink prepares, downloads, and runs the model. On successfully building the model, pin 2 generates PWM signals. On every rising edge, pin 3 generates an interrupt to calculate the time between the two consecutive rising edges of the signal, which in turn is used to calculate the frequency of the PWM signal at pin 3. The frequency of the signal is displayed on the Display block. The frequency displayed on the Display block is approximately equal to the frequency of the PWM signal generated by the PWM block.
2. To open Inspect and Analyze Simulation Results (Simulink), right-click the logged signal indicator icon and select Open Data Inspector. The SDI displays all the logged PWM signals for the frequency calculated at each time step.