Professional Documents
Culture Documents
Rohit Edsp Lab 8b
Rohit Edsp Lab 8b
No :8(b) Date:
Aim:
To acquire continuous time signals in real time and plot the DFT of the signal.
Theory:
Real-time data acquisition is an essential tool in many different fields. It allows us to
monitor and control processes, systems, and phenomena in real-time, which can lead to
improved safety, efficiency, and performance. Applications include Process control, Robotics
and control systems, medical diagnostics, Scientific research, environmental monitoring,
security systems, and automotive systems.
Data acquisition of alternating current requires a sensor like ACS712. The sensor gives
an analog output equivalent to the instantaneous current value. A suitable microcontroller with
ADC and serial communication peripherals can be used for acquisition and transmission of the
sensor output to a computer for data logging.
For the study purpose, PIC16F877A microcontroller was used to sample the sensor
output at a sampling frequency of 200Hz. Crystal frequency of 20MHz powers the
microcontroller clock pulse. The sampled data is then transmitted serially using USART at a
baud rate of 115200. Serial terminal tool can be used for viewing the data in the computer. The
received data can be exported and stored in a *.csv file for further processing.
Program:
#include<xc.h>
unsigned int i=0;
void interrupt() isr() // Interrupt Service Routine – Timer0
{
GIE=0; // Disable global interrupt
T0IF=0; // Reset timer flag
TMR0 = 0xB0; // Reset timer register for 5ms
i = 1;
GO = 1; // ADC start of conversion
GIE=1; // Enable global interrupt
}
void main()
{
OPTION_REG = 7; // prescalar 1:256
TMR0 = 0xB0; // 5 ms --> 200Hz sampling frequency
INTCON=0xE0; // Configuring Timer0 interrupt
TRISA=0xFF;
ADCON0=0x81; // 32*Tosc, RA0, ADON=1
ADCON1=0x00; // left justification
SPBRG = 10; // Serial com: 115200 baud rate
TXSTA = 0x04; // Configuring USART peropheral
SPEN = 1; // Serial Port Enable
TXEN = 1; // Transmission Enable
unsigned char dat;
while(1)
{
if(i)
{
while(GO); // Polling for End of Conversion
dat = ADRESH; // Recording sampled data
TXREG = dat; // Send data via UART
while(!TXIF); // Polling for end of transmission
TXIF = 0; // Wait for transmission to complete
i=0;
}
}
}
Task:
1. To obtain the sampled signal.
2. To plot DFT of the sampled signal.
CODE:
140 ma:
%ROHIT KUMAR SINGH
%CB.EN.U4ELC21048
% Sample input data in csv file
input_data = csvread('rohitsingh2.csv');
% Length of the input sequence
N = length(input_data);
% Initialize the DFT result
X = zeros(1, N);
% Calculate DFT
for k = 1:N
for n = 1:N
X(k) = X(k) + input_data(n) * exp(-1i * 2 * pi * (n-1) * (k-1) / N);
end
end
% Plot the result
figure;
stem(X);
title('DFT Result - 160ma');
xlabel('Frequency (k)');
ylabel('Amplitude');
% Display the result
disp('DFT Result - 160ma:');
disp(X);
420 ma:
%ROHIT KUMAR SINGH
%CB.EN.U4ELC21048
% Sample input data in csv file
input_data = csvread('rohitsingh.csv');
% Length of the input sequence
N = length(input_data);
% Initialize the DFT result
X = zeros(1, N);
% Calculate DFT
for k = 1:N
for n = 1:N
X(k) = X(k) + input_data(n) * exp(-1i * 2 * pi * (n-1) * (k-1) / N);
end
end
% Plot the result
figure;
stem(X);
title('DFT Result - 420ma');
xlabel('Frequency (k)');
ylabel('Amplitude');
% Display the result
disp('DFT Result - 420ma:');
disp(X);
Plot of 160ma
3
4
Output of 160 ma
Plot of 420ma
5
Output of 160 ma
6
Result & Inference:
• Real-time data from the ACS712 sensor, providing analog output proportional to
instantaneous current, is acquired using a PIC16F877A microcontroller. The
microcontroller, equipped with ADC and serial communication peripherals, samples
the sensor output at a frequency of 200Hz, driven by a 20MHz crystal.
• The acquired data is transmitted serially through USART at a baud rate of 115200,
allowing for real-time communication with a computer for data logging. Users can view
the data in a serial terminal tool, export it to a Notepad file, and subsequently convert the
Notepad file to a CSV format.
• To analyze the data, the CSV file is read using the csvread() command. The next step
involves performing a Discrete Fourier Transform (DFT) operation on the loaded sample
inputs from the CSV file. DFT is a mathematical operation that transforms a discrete set
of time-domain samples into a corresponding set of frequency-domain samples.
• This entire process is executed for two different loads: 140 mA and 420 mA. The DFT
operations for these varying loads are conducted by continuously acquiring real-time data
of alternating currents. The results are then observed and plotted, providing insights into
the frequency components of the acquired current data for different loads.
• In summary, the system captures real-time AC current data, utilizes a microcontroller for
acquisition and transmission, performs DFT analysis on the sampled data for different
loads, and visualizes the results through observation and plotting.
7
8
9
10
11
12