Ok, first of all, you need to install Python and its Jupyter Notebook.
I suggest to download the Anaconda distribution ( Anaconda | Anaconda Distribution ), which is “easy” to handle for new users. Moreover, Anaconda already embeds some fundamental libraries you will need for your Arduino project (jupyter, matplotlib, etc.).
I recommend downloading Anaconda’s Python 3 version (32- or 64-bit is up to you).
When you have installed Anaconda, the Jupyter icon will be at your disposal. So, just double-click on it or run “jupyter notebook” in your console/terminal and then wait the opening of the Jupyter Notebook in the default web browser.
Now you are ready to develop your own codes (i.e. Arduino sketch and python script).
Regarding the Arduino sketch, it is a simple piece of code. It sets the Arduino always ready to listen and answer to your commands through the serial port. Off course, Arduino will respond only to commands defined into the sketch and thus unknown inputs will be ignored.
Here an example of sketch:
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()){
char c = Serial.read();
if (c == 'X'){
// your code here
Serial.println(your X variable here);
}
if (c == 'Y'){
// your code here
Serial.println(your Y variable here);
}
}
}
The Python code is a little bit more complicated, but it relatively easy to understand and thus to modify, if you have time to spend on it. I cut the script in several parts for your convenience. However, if you need help, just ask.
- Importing libraries
import time
import serial
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from IPython import display
- Variable settings
x = y = []
counter = 1
cycle = 10
step = 1 # seconds
- Plotting data
%matplotlib inline
arduino = serial.Serial('COM3',9600) # set your COM port here
time.sleep(1)
plt.ion()
plt.show()
while counter <= cycle:
time.sleep(step)
plt.figure(figsize=(20, 10), dpi= 20, facecolor='w', edgecolor='k')
arduino.write('X'.encode())
espeed = float(arduino.readline())
x = np.append(x, espeed)
arduino.write('Y'.encode())
power = float(arduino.readline())
y = np.append(y, power)
plt.plot(x, y, '--or')
plt.xlabel('your X label', fontsize = 13)
plt.ylabel('your Y label', fontsize = 13)
plt.grid(True)
counter += 1
arduino.close()
Hope in helping you. Good luck with your project.