1 电路连接示意图
2 实物连接照片
3 Python 程序 import timeimport matplotlib.pyplot as pltfrom drawnow import *import serialval = [ ]cnt = 0port = serial.Serial('COM3', 115200, timeout=0.5)plt.ion()#create the figure functiondef makeFig(): plt.ylim(-1023,1023) plt.title('Osciloscope') plt.grid(True) plt.ylabel('ADC outputs') plt.plot(val, 'ro-', label='Channel 0') plt.legend(loc='lower right')while (True): port.write(b's') #handshake with Arduino if (port.inWaiting()):# if the arduino replies value = port.readline()# read the reply print(value)#print so we can monitor it number = float(value) #convert received data to integer print('Channel 0: {0}'.format(number)) # Sleep for half a second. time.sleep(0.01) val.append(int(number)) drawnow(makeFig)#update plot to reflect new data input plt.pause(.000001) cnt = cnt+1 if(cnt>50): val.pop(0)#keep the plot fresh by deleting the data at position 0
4 Arduino程序 int sensorpin = A0;void setup() { // initialize serial communication at 115200 bits per second to match that of the python script: Serial.begin(115200);}void loop() { // read the input on analog pin 0 float sensorValue = analogRead(sensorpin); byte data = Serial.read(); if (data == 's') { Serial.println(sensorValue); delay(10); // delay in between reads for stability }}
5 效果
6 视频效果 Ardrino借助Python做一个示波器 - 西瓜视频 (ixigua.com) |