I tried to connect the function generator probe to the ADC, A0 of the teensy4.1 with setting the input frequency to 10kHz. because I am planning to connect an analog accelerometer ADXL1002 to the ADC A0 pin. Even when I twerk the input frequency , I couldn't get a pure sine wave.
I also got a funny output at the Arduino monitor, click on the attached images to see the fluatuations.
I have attached the codes and images
I also got a funny output at the Arduino monitor, click on the attached images to see the fluatuations.
I have attached the codes and images
Code:
#include <Arduino.h>
#include <ADC.h> // Include the ADC library
ADC *adc = new ADC(); // adc object
const int adcPin = A0;
void setup() {
// Initialize serial communication
Serial.begin(2000000);
// Set ADC resolution and averaging
analogReadResolution(12);
analogReadAveraging(4);
// Configure ADC settings
adc->adc0->setAveraging(4); // set number of averages for ADC0
adc->adc0->setResolution(16); // set bits of resolution for ADC0
adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED); // set conversion speed for ADC0
adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::HIGH_SPEED); // set sampling speed for ADC0
}
void loop() {
// Get the current timestamp in microseconds
unsigned long timestamp = micros();
// Read the analog value from the specified pin
int adcValue = adc->adc0->analogRead(adcPin); // Use adc0 instance to read
// Print timestamp and ADC value separated by a comma
Serial.print(timestamp);
Serial.print(",");
Serial.println(adcValue);
delay(1);
}
Code:
% MATLAB Code to Read Data from USB Port and Plot in Real-Time
% Clear any existing serialport connections
availablePorts = serialportlist("available");
for i = 1:length(availablePorts)
try
% Try closing each port if open
portObj = serialport(availablePorts(i), 9600); % Dummy baud rate
configureCallback(portObj, "off");
clear portObj;
catch
% If port is not accessible or already cleared, ignore
end
end
% Define the serial port and baud rate
serialPort = '/dev/tty.usbmodem131678501'; % Replace with your actual serial port
baudRate = 2000000;
% Create a serialport object
try
s = serialport(serialPort, baudRate);
% Display connection info
disp(['Connected to ', serialPort]);
catch ME
disp('Failed to connect to the serial port.');
disp(ME.message);
return;
end
% Initialize variables for plotting
h = animatedline('MaximumNumPoints', 5000);
ax = gca;
ax.YGrid = 'on';
ax.XGrid = 'on';
xlabel('Time (s)');
ylabel('ADC Value');
title('Real-Time ADC Data Plot');
% Store the animated line object in appdata
setappdata(gcf, 'PlotLine', h);
% Configure the callback function to read data
configureCallback(s, "terminator", @readSerialData);
% Callback function to read serial data
function readSerialData(src, ~)
try
% Retrieve the animated line object
h = getappdata(gcf, 'PlotLine');
% Read the data from the serial port
data = readline(src);
% Display raw data for debugging
disp(['Raw Data: ', data]);
% Parse the data
dataArray = split(data, ',');
if length(dataArray) == 2
timestamp = str2double(dataArray{1}) / 1e6; % Convert micros to seconds
adcValue = str2double(dataArray{2});
% Validate parsed data
if ~isnan(timestamp) && ~isnan(adcValue)
% Display parsed data for debugging
disp(['Parsed Data - Time: ', num2str(timestamp), ', ADC: ', num2str(adcValue)]);
% Add data to the plot
addpoints(h, timestamp, adcValue);
drawnow limitrate; % Update the plot with rate limiting
else
disp('Error: Parsed data contains NaN values.');
end
else
disp('Error: Data does not have exactly two elements.');
end
catch ME
% Display error message in case of issues
disp('Error reading serial data:');
disp(ME.message);
end
end
Attachments
Last edited: