Hello,
I need help figuring out how to achieve the sampling frequency required for my project. I am using the Portenta H7 board with the Portenta Breakout. I am reading data from a piezoelectric sensor through the analog input. Currently, I am working with only one sensor, but eventually, I will need to read data from eight sensors simultaneously.
The sampling frequency I aim to achieve is 200 kHz. However, at the moment, I can only reach 20 kHz. I am using Ethernet communication between the H7 and my computer, where the H7 acts as the server, and my computer (using MATLAB) acts as the client. I capture and process the data in MATLAB, utilizing buffers to improve efficiency.
I am not an expert in this field, but I am passionate about it and working on this project as part of my university studies. Below is the code I am currently using on the H7.
#include <Ethernet.h>
#include <SPI.h>
// Static IP configuration
IPAddress ip(192, 168, 1, 199); // IP address of the Portenta H7
IPAddress subnet(255, 255, 255, 0); // Subnet mask
// Server port on the PC
const int serverPort = 1234;
// Create an Ethernet server
EthernetServer server(serverPort);
// Analog pin for the sensor
const int sensorPin = A0;
// Interval between transmissions in micros
const unsigned long sendInterval = 5; // Send data every specified micros
unsigned long lastSendTime = 0;
unsigned long startTime = 0;
unsigned long elapsedTime = 0;
// Buffer creation and size
const int bufferSize = 131072; // Max bytes per packet
char dataBuffer[bufferSize]; // Buffer to accumulate data
int bufferIndex = 0; // Current position in the buffer
EthernetClient client;
void setup() {
// Start serial communication
Serial.begin(115200);
// Initialize Ethernet
Ethernet.begin(ip, subnet);
server.begin();
Serial.print("Server initiated, IP address: ");
Serial.println(Ethernet.localIP());
Serial.print("Port: ");
Serial.println(serverPort);
// Wait for a client to connect
Serial.println("Waiting for a client connection...");
while (!client) {
client = server.available();
if (client) {
Serial.println("Client connected!");
}
}
delay(5000);
}
void loop() {
// Check if there is a connected client
if (!client || !client.connected()) {
// Look for a new client
client = server.available();
if (client) {
Serial.println("Client connected!");
// Delay to allow the client to stabilize
Serial.println("Waiting for the client stabilization");
delay(5000);
Serial.println("Data sending");
// Reset start time
startTime = micros();
lastSendTime = startTime; // Reset last transmission time
bufferIndex = 0; // Reset the buffer
} else {
// If no clients are connected, stop reading
return; // Exit the loop
}
} else {
// Check if the client has sent a command
if (client.available() > 0) {
char command = client.read(); // Read the command sent by the client
if (command == 'D') { // Command 'D' for disconnection
Serial.println("Disconnection command received. Client connection closed...");
Serial.println("Data reading stopped!");
Serial.println("Waiting for a new connection with the client...");
client.stop(); // Disconnect the client
return; // Exit the loop
}
}
// Client is connected, send data
unsigned long currentTime = micros(); // Current time
if (currentTime - lastSendTime >= sendInterval) {
lastSendTime = currentTime; // Update last transmission time
elapsedTime = currentTime - startTime; // Calculate relative time
uint16_t sensorValue = analogRead(sensorPin); // Read the sensor value
// Append the data to the buffer
int bytesWritten = snprintf(dataBuffer + bufferIndex, bufferSize - bufferIndex, "%.6f %d\n", elapsedTime / 1000000.0, sensorValue);
// Check if the data was written correctly
if (bytesWritten > 0 && (bufferIndex + bytesWritten) < bufferSize) {
bufferIndex += bytesWritten; // Update the buffer position
} else {
// If the buffer is full, send it
client.write(dataBuffer, bufferIndex); // Send the buffer
bufferIndex = 0; // Reset the buffer
// Write the current data into the buffer
bytesWritten = snprintf(dataBuffer, bufferSize, "%.6f %d\n", elapsedTime / 1000000.0, sensorValue);
bufferIndex = bytesWritten; // Update the buffer
}
// If the buffer is full, send it immediately
if (bufferIndex >= bufferSize - 1) {
client.write(dataBuffer, bufferIndex);
bufferIndex = 0; // Reset the buffer
}
}
}
}