Read Duty Cycle of a PWM Signal Using Visuino

by RonFrtek in Circuits > Arduino

104 Views, 2 Favorites, 0 Comments

Read Duty Cycle of a PWM Signal Using Visuino

pwm read

In this tutorial we are going to read the Duty Cycle of a PWM signal using Visuino. For a PWM source we are going to use a PWM generator connected to the Arduino interrupt digital pin [2],

We are going to measure the pulse width of the HIGH and LOW then use this formula

TotalDuration = highDuration + lowDuration;

DutyCycle = 100.0 * highDuration / totalDuration

to get the Duty Cycle in percentage.

Watch the video!

What You Will Need

VisuinoAdvrtisemenArduinoGiga_1 (1).jpg
UNOr4.png
XY-LPWM.jpeg
jumper-wires.jpg
  • Arduino UNO or Arduino Mega (or any other board)
  • Some PWM signal source like  XY-LPWM
  • Visuino program: Download Visuino
  • Jumper wires



The Circuit

2024-05-31_9-29-03.jpg
2024-05-31_9-33-30.jpg
  • Connect Arduino pin [5V] to the PWM Module pin [VIN+]
  • Connect Arduino pin [GND] to the PWM Module pin [VIN-]
  • Connect PWM Module pin [PWM] to Arduino digital pin [2]
  • Connect PWM Module pin [GND] to Arduino pin [GND]


Start Visuino, and Select the Arduino UNO Board Type

select-board-uno.jpg
Visuino-Select-Board-UNO.jpg

Start Visuino as shown in the first picture Click on the "Tools" button on the Arduino component (Picture 1) in Visuino When the dialog appears, select "Arduino UNO" as shown on Picture 2

In Visuino Add & Set Components

2024-05-31_9-53-07.jpg
2024-05-31_9-55-05.jpg
2024-05-31_9-56-49.jpg
2024-05-31_9-57-26.jpg
2024-05-31_9-58-55.jpg
2024-05-31_9-59-48.jpg
2024-05-31_10-00-53.jpg
2024-05-31_10-03-52.jpg
2024-05-31_10-01-39.jpg
2024-05-31_10-04-11.jpg
  • Add "Digital Multi Source" component
  • Add "Digital (Boolean) Inverter (Not)" component
  • Add 2X "Pulse Meter(Measure Period)" component
  • Add "Unsigned To Analog" component
  • Add "Analog Multi Source" component
  • Add "Add Analog" component
  • Add "Divide Analog By Value" component
  • Add "Multiply Analog By Value" component


  • Select "DivideByValue1" and in the properties window select "Value" and click on the pin icon and select "Float SinkPin"
  • Select "MultiplyByValue1" and in the properties window set "Value" to 100

In Visuino Connect Components

2024-05-31_15-02-25.jpg
read-pwm.png
  • Connect Arduino digital pin [2] to "MultiSource1" pin [In]
  • Connect "MultiSource1" pin [0] to "PulseMeter1" pin [In]
  • Connect "MultiSource1" pin [1] to "Inverter1" pin [In]
  • Connect "Inverter1" pin [Out] to "PulseMeter2" pin [In]
  • Connect "PulseMeter1" pin [Out] to "UnsignedToAnalog1" pin [In]
  • Connect "PulseMeter2" pin [Out] to "UnsignedToAnalog2" pin [In]
  • Connect "UnsignedToAnalog1" pin [Out] to "MultiSource2" pin [In]
  • Connect "MultiSource2" pin [0] to "Add1" pin [0]
  • Connect "MultiSource2" pin [1] to "DivideByValue1" pin [In]
  • Connect "UnsignedToAnalog2" pin [Out] to "Add1" pin [1]
  • Connect "Add1" pin [Out] to "DivideByValue1" pin [Value]
  • Connect "DivideByValue1" pin [Out] to "MultiplyByValue1" pin [In]
  • Connect "MultiplyByValue1" pin [Out] to Arduino Serial pin [In]

Duty Cycle Formula Explained

2024-05-31_9-34-29.jpg

We are going to measure the pulse width of the HIGH and LOW then use this formula

TotalDuration = highDuration + lowDuration;

DutyCycle = 100.0 * highDuration / totalDuration

to get the Duty Cycle in percentage.


On the schematic you can see that we split the digital pulses using "MultiSource1", First signal we take it directly to "PulseMeter1" to get the pulse period when it is HIGH, Second signal we invert it using "Inverter1" and we take it to "PulseMeter2" to get a pulse period when its LOW, we convert both periods to analog values and we sum them to get the total length using "Add1", then we use "DivideByValue1" to divide HIGH pulse period / Sum of both periods, and we multiply all at the end by 100 using "MultiplyByValue1"

Generate, Compile, and Upload the Arduino Code

Visuino-Compile-UNO.jpg

In Visuino, at the bottom click on the "Build" Tab, make sure the correct port is selected, then click on the "Compile/Build and Upload" button.

Play

2024-05-31_15-05-26.jpg

If you power the Arduino module and click connect in the "Serial" tab, you will be able to see the Duty Cycle of the Input PWM signal.

Congratulations! You have completed your project with Visuino. Also attached is the Visuino project, that I created for this tutorial, you can download it and open it in Visuino: https://www.visuino.com

Downloads