frequency, time-period and duty-cycle using arduino;

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Experiment 7

Objective:
“To measure frequency, time-period and duty-cycle using arduino; both through
simulation and practical implementation”

Apparatus required:
 Arduino UNO
 Proteus
 Connecting wires
 Breadboard
 Oscilloscope
 Resistors
 Signal generator

Theory:
Arduino:
Arduino is an open-source electronics platform based on easy-to-use hardware and
software. It consists of a microcontroller (typically an Atmel ATmega series) and provides a
simple programming environment (Arduino IDE). It is used in a wide range of applications
like robotics, home automation, and educational projects due to its affordability and ease of
use. . It is widely used in embedded systems projects due to its simplicity, affordability, and
versatility. Designed as an open-source hardware platform, it allows users to easily connect
sensors, actuators, and other components to create interactive devices.

 Arduino hardware:
It typically includes digital and analog input/output pins, power supply pins, and
interfaces for communication protocols like SPI, I2C, and UART.

 Arduino software (IDE):


This platform uses a simplified version of C++ and includes a vast collection of
libraries for performing various tasks, such as reading sensors and controlling motors.

Fig 1: Arduino

1
Arduino as a measurement tool:
Arduino is a versatile microcontroller platform that allows accurate signal
measurement using its digital I/O pins and built-in timer functionalities. Using functions like
pulseIn(), the Arduino can measure the HIGH and LOW durations of a signal.

 pulseIn() function:
Measures the duration of a pulse on a digital pin and can detect either HIGH or LOW
pulse

 Interrupts:
Can be used to detect the rising and falling edges of a signal for precise timing

Proteus:
Proteus is popular software for simulating electronic circuits and embedded systems,
including microcontrollers like Arduino. It allows you to design and test circuits virtually
before implementing them in hardware. By simulating circuits in Proteus, you can verify the
functionality and make adjustments without needing physical components.

 Circuit design:
It allows for the simulation of complete electronic circuits, including power supplies,
sensors, actuators, and microcontroller-based systems.

 Microcontroller simulation:
It includes models for microcontrollers and their peripherals, making it possible to
simulate firmware and hardware interaction.

Fig 2: Proteus interface

Signal characteristics:

1. Frequency:
Frequency is the number of cycles of a periodic signal occurring per second,
measured in Hertz (Hz). For a digital pulse signal, it can be calculated as:

f = 1/T

2
2. Time-period:
The time-period is the duration of one complete cycle of a periodic signal. It is
typically measured in seconds and is the reciprocal of the frequency:

T = 1/f

3. Duty-cycle:
Duty-cycle is the percentage of time a signal stays in the HIGH state during a
complete cycle. It is expressed as:

Duty-cycle = Total Time of One Cycle/ Time in HIGH State x 100

For a pulse-width modulated (PWM) signal, the duty-cycle determines the proportion
of ON time to the total cycle time.

Procedure:
 Simulation:
1. Design a circuit where a digital signal (e.g., PWM) is fed into one of the digital input
pins of the Arduino.
2. Write the Arduino code to:
3. Use pulseIn() to measure HIGH and LOW pulse durations.
4. Calculate frequency, time-period, and duty-cycle.
5. Upload the code to the virtual Arduino board and run the simulation.
6. Verify the measurements in the simulation environment.

Code:
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

volatile unsigned long startTime, endTime;

volatile unsigned long onTime, offTime;

volatile int pulseCount = 0;

void setup() {

lcd.begin(16, 4);

pinMode(7, INPUT); // Input pin for signal

attachInterrupt(digitalPinToInterrupt(7), measurePulse, RISING);

Serial.begin(9600);

void loop() {

3
if (pulseCount > 0) {

unsigned long totalTime = endTime - startTime;

float frequency = 1000000.0 / totalTime;

float dutyCycle = (onTime * 100.0) / totalTime;

lcd.clear();

lcd.print("Freq: ");

lcd.print(frequency);

lcd.print(" Hz");

lcd.setCursor(0, 1);

lcd.print("Duty: ");

lcd.print(dutyCycle);

lcd.print(" %");

pulseCount = 0;

delay(1000);

void measurePulse() {

if (pulseCount == 0) {

startTime = micros();

} else if (pulseCount == 1) {

onTime = micros() - startTime;

} else if (pulseCount == 2) {

endTime = micros();

pulseCount++;

if (pulseCount > 2) pulseCount = 0;

4
Fig 3: Simulation

 Practical implementation:
1. Connect the output of the signal generator or a PWM source to a digital input pin of
the Arduino (e.g., pin 2).
2. Ground the signal generator and Arduino together.
3. Write the Arduino program using the pulseIn() function.
4. Measure the HIGH duration of the signal
5. Measure the LOW duration
6. Calculate frequency
7. Compute the duty-cycle
8. Connect the Arduino to a laptop/PC via a USB cable.
9. Upload the code using the Arduino IDE.
10.Generate a test signal of known frequency and duty-cycle.

Fig 4: Practical implementation

Code explanation:
Here’s a detailed line-by-line explanation of the Arduino code provided:

1. #include <LiquidCrystal.h>
This line includes the LiquidCrystal library, which allows you to control an LCD
display. This library provides functions to interact with the LCD.

2. LiquidCrystal lcd(12, 11, 5, 4, 3, 2):

5
This initializes the LCD object with the pins that the Arduino uses to interface with
the LCD. In this case, pin 12 is the RS (Register Select), pin 11 is the Enable pin, and pins 5,
4, 3, and 2 are the data pins (D4-D7) of the LCD.

3. lcd.begin(16, 2):
This function sets the LCD to a 16x2 configuration, meaning it has 16 columns and 2
rows.

4. void loop() { }:
The loop() function is empty because we only need to display the text once, which
happens in the setup() function.

5. delay:
This produces delay in output generation.

Summary:
The provided Arduino code measures the frequency and duty cycle of a PWM signal
using interrupts. It initializes an LCD to display the results and counts pulses to determine the
duration of high and low states. The frequency is calculated as the reciprocal of the total time
for one cycle, while the duty cycle is computed as a percentage of the time the signal is high.
The results are updated on the LCD every second, providing real-time feedback on the input
signal characteristics.

Safety Precautions:
 Ensure all connections are made properly before powering up the Arduino to avoid
short circuits.
 Use resistors in series with LEDs and seven-segment displays to limit current and
prevent damage.

Conclusion:
The experiment successfully demonstrated the measurement of frequency, time-
period, and duty-cycle of a digital signal using Arduino. The results obtained were accurate
and validated against known test signals. This lab experiment highlights the utility of Arduino
as a low-cost, flexible solution for signal analysis in embedded systems and electronics. The
practical implementation corroborated the simulation results, underscoring the reliability of
the methodology and the effectiveness of the pulseIn() function for such measurements.

You might also like