Clear Filters
Clear Filters

I'm desperately trying to run an Arduino program and IN THE SAME TIME to plot something in Matlab.

6 views (last 30 days)
Hello. I'm desperately trying to run an Arduino program ( to control the speed of a DC Motor using a potentiometer with the L298N H_bridge ) and IN THE SAME TIME to plot in Matlab the voltage from analog input pin that is connected to the potentiometer. The purpose is to spin the potentiometer WHILE the motor is working and WHILE visualising the graph to the voltage from that analog input pin in Matlab. And I can only do that individual. That means that the code in Arduino is working, the plot funtion for the voltage to the potentiometer in Matlab is also working, but not in the same time for a LIVE visualisation. The problem, from what could I see, is that the program from Arduino and the program from Matlab are working on the same port (COM4) and thus, they just can't work together in the same time.
What can I do ? Please help me. I only want a LIVE visualisation of my voltage to the potentiometer, while modifying this from the potentiometer and while the DC Motor is working accordingly to the Arduino code. Thank you.

Answers (1)

Shreshth
Shreshth on 9 Jan 2024
Hello RAZVAN,
Your question demands a solution for live visualization of potentiometer voltage in MATLAB while controlling the DC motor with your Arduino.This is the standard case where you can use the serial communication capability of the Arduino to send the potentiometer data to MATLAB. Then, in MATLAB, you can read this data and plot it in real-time.
Here's a step-by-step guide to set this up:
  1. Modify your Arduino code to read the potentiometer value.
  2. Use the ‘Serial.print()’ or ‘Serial.println()’ function to send the potentiometer value over the serial port.
  3. Ensure that the Arduino code doesn't block the loop with delays that could affect the communication.
To better understand the process take a look at the dummy code below:
For Arduino:
// Define pins for the motor and potentiometer
int potPin = A0;
int enablePin = 9; // PWM control (speed)
int motorPin1 = 3; // Direction
int motorPin2 = 4; // Direction
void setup() {
pinMode(potPin, INPUT);
pinMode(enablePin, OUTPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
Serial.begin(9600); // Start serial communication
}
void loop() {
int potValue = analogRead(potPin); // Read potentiometer
int motorSpeed = map(potValue, 0, 1023, 0, 255); // Map to PWM range
analogWrite(enablePin, motorSpeed); // Control motor speed
digitalWrite(motorPin1, HIGH); // Set motor direction
digitalWrite(motorPin2, LOW);
Serial.println(potValue); // Send potentiometer value to serial
delay(10); // Short delay to prevent serial buffer overflow
}
For MATLAB:
% Set up serial connection
s = serial('COM4', 'BaudRate', 9600);
fopen(s);
% Create a figure for the plot
figure;
h = animatedline;
ax = gca;
ax.YGrid = 'on';
ax.YLim = [0 1023];
% Read and plot data in real-time
while true
if s.BytesAvailable > 0
data = fscanf(s, '%d'); % Read data from serial
addpoints(h, datetime('now'), data); % Add points to plot
drawnow; % Update plot
end
end
% Remember to include cleanup code to close serial port when done
This setup should allow you to visualize the potentiometer voltage live in MATLAB while the motor speed is being controlled by the Arduino. Adjust the delay in the Arduino code and the while loop timing in MATLAB as needed to achieve the desired responsiveness without overloading the serial buffer.
Hope it helps.
Thank you,
Shubham Shreshth.

Categories

Find more on Arduino Hardware in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!