├── Part1_GettingStarted ├── ArduinoSerialCom.py └── arduinoFreeRunning │ └── arduinoFreeRunning.ino ├── Part2_AddingUserInput ├── ArduinoSerialCom.py └── ardunioCharInput │ └── arduinoCharInput.ino ├── Part3_InitializationErrorHandling ├── ArduinoSerialCom.py └── arduinoCharInput │ └── arduinoCharInput.ino ├── Part4_CollectingMultipleDataPoints ├── ArduinoSerialCom.py └── arduinoCharInput │ └── arduinoCharInput.ino ├── Part5_UsingNumbersAndComputingAverage ├── ArduinoSerialCom.py └── arduinoCharInput │ └── arduinoCharInput.ino ├── Part6_WritingToTextFile ├── ArduinoSerialCom.py └── arduinoCharInput │ └── arduinoCharInput.ino ├── Part7_MutipleDataRowCollection ├── ArduinoSerialCom.py └── arduinoCharInput │ └── arduinoCharInput.ino ├── Part8_AddingInitializationFile ├── ArduinoSerialComm.py ├── dataSetup.ini └── ini_read.py ├── Part9_AddingTimestamp ├── ArduinoSerialComm.py ├── arduinoCharInput │ └── arduinoCharInput.ino ├── dataSetup.ini └── ini_read.py └── README.md /Part1_GettingStarted/ArduinoSerialCom.py: -------------------------------------------------------------------------------- 1 | import serial 2 | 3 | ser = serial.Serial('COM3', baudrate = 9600, timeout=1) 4 | 5 | while 1: 6 | 7 | arduinoData = ser.readline().decode('ascii') 8 | print(arduinoData) 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Part1_GettingStarted/arduinoFreeRunning/arduinoFreeRunning.ino: -------------------------------------------------------------------------------- 1 | 2 | int analogPin = 3; 3 | int data = 0; 4 | char userInput; 5 | 6 | void setup(){ 7 | 8 | Serial.begin(9600); // setup serial 9 | 10 | } 11 | 12 | void loop(){ 13 | 14 | 15 | data = analogRead(analogPin); // read the input pin 16 | Serial.println(data); // print the data to the serial bus 17 | 18 | } // Void Loop 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /Part2_AddingUserInput/ArduinoSerialCom.py: -------------------------------------------------------------------------------- 1 | import serial 2 | 3 | ser = serial.Serial('COM3', baudrate = 9600, timeout = 1) 4 | 5 | def getValues(): 6 | 7 | ser.write(b'g') 8 | arduinoData = ser.readline().decode('ascii') 9 | return arduinoData 10 | 11 | 12 | while(1): 13 | 14 | userInput = input('Get data point?') 15 | 16 | if userInput == 'y': 17 | print(getValues()) 18 | 19 | 20 | -------------------------------------------------------------------------------- /Part2_AddingUserInput/ardunioCharInput/arduinoCharInput.ino: -------------------------------------------------------------------------------- 1 | 2 | int analogPin = 3; 3 | int data = 0; 4 | char userInput; 5 | 6 | void setup(){ 7 | 8 | Serial.begin(9600); // setup serial 9 | 10 | } 11 | 12 | void loop(){ 13 | 14 | if(Serial.available()> 0){ 15 | 16 | userInput = Serial.read(); // read user input 17 | 18 | if(userInput == 'g'){ // if we get expected value 19 | 20 | data = analogRead(analogPin); // read the input pin 21 | Serial.println(data); 22 | 23 | } // if user input 'g' 24 | } // Serial.available 25 | } // Void Loop 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /Part3_InitializationErrorHandling/ArduinoSerialCom.py: -------------------------------------------------------------------------------- 1 | import serial 2 | import time 3 | 4 | ser = serial.Serial('COM3', baudrate = 9600, timeout = 1) 5 | time.sleep(3) 6 | 7 | def getValues(): 8 | 9 | ser.write(b'g') 10 | arduinoData = ser.readline().decode('ascii') 11 | return arduinoData 12 | 13 | 14 | while(1): 15 | 16 | userInput = input('Get data point?') 17 | 18 | if userInput == 'y': 19 | print(getValues()) 20 | 21 | 22 | -------------------------------------------------------------------------------- /Part3_InitializationErrorHandling/arduinoCharInput/arduinoCharInput.ino: -------------------------------------------------------------------------------- 1 | 2 | int analogPin = 3; 3 | int data = 0; 4 | char userInput; 5 | 6 | void setup(){ 7 | 8 | Serial.begin(9600); // setup serial 9 | 10 | } 11 | 12 | void loop(){ 13 | 14 | if(Serial.available()> 0){ 15 | 16 | userInput = Serial.read(); // read user input 17 | 18 | if(userInput == 'g'){ // if we get expected value 19 | 20 | data = analogRead(analogPin); // read the input pin 21 | Serial.println(data); 22 | 23 | } // if user input 'g' 24 | } // Serial.available 25 | } // Void Loop 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /Part4_CollectingMultipleDataPoints/ArduinoSerialCom.py: -------------------------------------------------------------------------------- 1 | import serial 2 | import time 3 | 4 | ser = serial.Serial('COM3', baudrate = 9600, timeout = 1) 5 | time.sleep(3) 6 | numPoints = 5 7 | dataList = [0]*numPoints 8 | 9 | def getValues(): 10 | 11 | ser.write(b'g') 12 | arduinoData = ser.readline().decode('ascii') 13 | 14 | return arduinoData 15 | 16 | 17 | while(1): 18 | 19 | userInput = input('Get data points?') 20 | 21 | if userInput == 'y': 22 | for i in range(0,numPoints): 23 | data = getValues() 24 | dataList[i] = data 25 | 26 | print(dataList) 27 | 28 | 29 | -------------------------------------------------------------------------------- /Part4_CollectingMultipleDataPoints/arduinoCharInput/arduinoCharInput.ino: -------------------------------------------------------------------------------- 1 | 2 | int analogPin = 3; 3 | int data = 0; 4 | char userInput; 5 | 6 | void setup(){ 7 | 8 | Serial.begin(9600); // setup serial 9 | 10 | } 11 | 12 | void loop(){ 13 | 14 | if(Serial.available()> 0){ 15 | 16 | userInput = Serial.read(); // read user input 17 | 18 | if(userInput == 'g'){ // if we get expected value 19 | 20 | data = analogRead(analogPin); // read the input pin 21 | Serial.println(data); 22 | 23 | } // if user input 'g' 24 | } // Serial.available 25 | } // Void Loop 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /Part5_UsingNumbersAndComputingAverage/ArduinoSerialCom.py: -------------------------------------------------------------------------------- 1 | import serial 2 | import time 3 | 4 | ser = serial.Serial('COM3', baudrate = 9600, timeout = 1) 5 | time.sleep(3) 6 | numPoints = 10 7 | dataList = [0]*numPoints 8 | 9 | def getValues(): 10 | 11 | ser.write(b'g') 12 | arduinoData = ser.readline().decode().split('\r\n') 13 | 14 | return arduinoData[0] 15 | 16 | 17 | while(1): 18 | 19 | userInput = input('Get data points?') 20 | 21 | if userInput == 'y': 22 | for i in range(0,numPoints): 23 | data = getValues() 24 | data = int(data) 25 | dataList[i] = data 26 | 27 | dataAvg = sum(dataList)/numPoints 28 | print(dataAvg) 29 | print(dataList) 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Part5_UsingNumbersAndComputingAverage/arduinoCharInput/arduinoCharInput.ino: -------------------------------------------------------------------------------- 1 | 2 | int analogPin = 3; 3 | int data = 0; 4 | char userInput; 5 | 6 | void setup(){ 7 | 8 | Serial.begin(9600); // setup serial 9 | 10 | } 11 | 12 | void loop(){ 13 | 14 | if(Serial.available()> 0){ 15 | 16 | userInput = Serial.read(); // read user input 17 | 18 | if(userInput == 'g'){ // if we get expected value 19 | 20 | data = analogRead(analogPin); // read the input pin 21 | Serial.println(data); 22 | 23 | } // if user input 'g' 24 | } // Serial.available 25 | } // Void Loop 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /Part6_WritingToTextFile/ArduinoSerialCom.py: -------------------------------------------------------------------------------- 1 | import serial 2 | import time 3 | 4 | ser = serial.Serial('COM3', baudrate = 9600, timeout = 1) 5 | time.sleep(3) 6 | numPoints = 10 7 | dataList = [0]*numPoints 8 | dataFile = open('dataFile.txt', 'w') 9 | 10 | def getValues(): 11 | 12 | ser.write(b'g') 13 | arduinoData = ser.readline().decode().split('\r\n') 14 | 15 | return arduinoData[0] 16 | 17 | 18 | while(1): 19 | 20 | userInput = input('Get data points?') 21 | 22 | if userInput == 'y': 23 | for i in range(0,numPoints): 24 | data = getValues() 25 | dataFile.write(data + ' ') 26 | data = int(data) 27 | dataList[i] = data 28 | 29 | dataAvg = sum(dataList)/numPoints 30 | print(dataAvg) 31 | print(dataList) 32 | 33 | dataFile.close() 34 | break 35 | 36 | -------------------------------------------------------------------------------- /Part6_WritingToTextFile/arduinoCharInput/arduinoCharInput.ino: -------------------------------------------------------------------------------- 1 | 2 | int analogPin = 3; 3 | int data = 0; 4 | char userInput; 5 | 6 | void setup(){ 7 | 8 | Serial.begin(9600); // setup serial 9 | 10 | } 11 | 12 | void loop(){ 13 | 14 | if(Serial.available()> 0){ 15 | 16 | userInput = Serial.read(); // read user input 17 | 18 | if(userInput == 'g'){ // if we get expected value 19 | 20 | data = analogRead(analogPin); // read the input pin 21 | Serial.println(data); 22 | 23 | } // if user input 'g' 24 | } // Serial.available 25 | } // Void Loop 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /Part7_MutipleDataRowCollection/ArduinoSerialCom.py: -------------------------------------------------------------------------------- 1 | # WaveShapePlay 2 | # Check out for a full tutorial for the code below: https://youtu.be/qg9sO4AD8-A 3 | 4 | import serial 5 | import time 6 | 7 | ser = serial.Serial('COM3', baudrate = 9600, timeout = 1) 8 | time.sleep(3) 9 | numPoints = 20 10 | dataList = [0]*numPoints 11 | dataFile = open('dataFile.txt', 'w') 12 | numRowsCollect = 100 13 | 14 | def getValues(): 15 | 16 | ser.write(b'g') 17 | arduinoData = ser.readline().decode().split('\r\n') 18 | 19 | return arduinoData[0] 20 | 21 | def printToFile(data,index): 22 | 23 | dataFile.write(data) 24 | if index != (numPoints - 1): 25 | dataFile.write(',') 26 | else: 27 | dataFile.write('\n') 28 | 29 | def getAverage(dataSet,row): 30 | 31 | dataAvg = sum(dataSet) / len(dataSet) 32 | print('Average for ' + str(row) + ' is: ' + str(dataAvg)) 33 | 34 | 35 | 36 | while(1): 37 | 38 | userInput = input('Get data points?') 39 | 40 | if userInput == 'y': 41 | for row in range(0,numRowsCollect): 42 | for i in range(0,numPoints): 43 | data = getValues() 44 | printToFile(data,i) 45 | dataInt = int(data) 46 | dataList[i] = dataInt 47 | 48 | getAverage(dataList,row) 49 | 50 | dataFile.close() 51 | 52 | break 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /Part7_MutipleDataRowCollection/arduinoCharInput/arduinoCharInput.ino: -------------------------------------------------------------------------------- 1 | 2 | int analogPin = 3; 3 | int data = 0; 4 | char userInput; 5 | 6 | void setup(){ 7 | 8 | Serial.begin(9600); // setup serial 9 | 10 | } 11 | 12 | void loop(){ 13 | 14 | if(Serial.available()> 0){ 15 | 16 | userInput = Serial.read(); // read user input 17 | 18 | if(userInput == 'g'){ // if we get expected value 19 | 20 | data = analogRead(analogPin); // read the input pin 21 | Serial.println(data); 22 | 23 | } // if user input 'g' 24 | } // Serial.available 25 | } // Void Loop 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /Part8_AddingInitializationFile/ArduinoSerialComm.py: -------------------------------------------------------------------------------- 1 | # WaveShapePlay 2 | # For a detailed YouTube tutorial visit: https://youtu.be/T-s-2tBUfLM 3 | 4 | import serial 5 | import time 6 | from ini_read import getINI 7 | 8 | iniData = getINI() 9 | numRowsCollect = int(iniData['numRowsCollect']) 10 | numPoints = int(iniData['numPoints']) 11 | 12 | ser = serial.Serial('COM3', baudrate = 9600, timeout = 1) 13 | time.sleep(3) 14 | 15 | dataFile = open('dataFile.txt', 'w') 16 | dataList = [0]*numPoints 17 | 18 | 19 | def getValues(): 20 | 21 | ser.write(b'g') 22 | arduinoData = ser.readline().decode().split('\r\n') 23 | 24 | return arduinoData[0] 25 | 26 | def printToFile(data,index): 27 | 28 | dataFile.write(data) 29 | if index != (numPoints - 1): 30 | dataFile.write(',') 31 | else: 32 | dataFile.write('\n') 33 | 34 | def getAverage(dataSet,row): 35 | 36 | dataAvg = sum(dataSet) / len(dataSet) 37 | print('Average for ' + str(row) + ' is: ' + str(dataAvg)) 38 | 39 | 40 | 41 | while(1): 42 | 43 | userInput = input('Get data points?') 44 | 45 | if userInput == 'y': 46 | for row in range(0,numRowsCollect): 47 | for i in range(0,numPoints): 48 | data = getValues() 49 | printToFile(data,i) 50 | dataInt = int(data) 51 | dataList[i] = dataInt 52 | 53 | getAverage(dataList,row) 54 | 55 | dataFile.close() 56 | 57 | break 58 | -------------------------------------------------------------------------------- /Part8_AddingInitializationFile/dataSetup.ini: -------------------------------------------------------------------------------- 1 | numVariable:4 2 | numPoints:5 3 | numRowsCollect:50 4 | operator:Bob 5 | Type:Voltage 6 | -------------------------------------------------------------------------------- /Part8_AddingInitializationFile/ini_read.py: -------------------------------------------------------------------------------- 1 | dataFile_ini = open('dataSetup.ini', 'r') 2 | ini_data_result = {} 3 | 4 | def getINI(): 5 | 6 | iniHeader = dataFile_ini.readline().split(':') 7 | print(iniHeader[0] + ' ' + iniHeader[1].rstrip() ) 8 | iniReadLines = int(iniHeader[1].rstrip()) 9 | 10 | for i in range(0,iniReadLines): 11 | 12 | variable = dataFile_ini.readline().split(':') 13 | print(variable) 14 | ini_data_result[variable[0]] = variable[1].rstrip() 15 | 16 | print(ini_data_result) 17 | 18 | return ini_data_result 19 | -------------------------------------------------------------------------------- /Part9_AddingTimestamp/ArduinoSerialComm.py: -------------------------------------------------------------------------------- 1 | import serial 2 | import time 3 | from ini_read import getINI 4 | 5 | iniData = getINI() 6 | numRowsCollect = int(iniData['numRowsCollect']) 7 | numPoints = int(iniData['numPoints']) 8 | 9 | ser = serial.Serial('COM3', baudrate = 9600, timeout = 1) 10 | time.sleep(3) 11 | 12 | dataFile = open('dataFile.txt', 'w') 13 | dataList = [0]*numPoints 14 | 15 | def getValues(timeStamp): 16 | 17 | if timeStamp == 'false': 18 | ser.write(b'g') 19 | arduinoData = ser.readline().decode().split('\r\n') 20 | arduinoOutput = arduinoData[0] 21 | 22 | if timeStamp == 'true': 23 | ser.write(b'c') 24 | arduinoOutput = ser.readline().decode().split('-') 25 | 26 | return arduinoOutput 27 | 28 | def printToFile(data,index): 29 | 30 | dataFile.write(data) 31 | if index != (numPoints - 1): 32 | dataFile.write(',') 33 | else: 34 | dataFile.write('\n') 35 | 36 | def getAverage(dataSet,row): 37 | 38 | dataAvg = sum(dataSet) / len(dataSet) 39 | print('Average for ' + str(row) + ' is: ' + str(dataAvg)) 40 | 41 | 42 | while(1): 43 | 44 | userInput = input('Get data points?') 45 | 46 | if userInput == 'y': 47 | timeStamp = 'false' 48 | for row in range(0,numRowsCollect): 49 | for i in range(0,numPoints): 50 | data = getValues(timeStamp) 51 | printToFile(data,i) 52 | dataInt = int(data) 53 | dataList[i] = dataInt 54 | 55 | getAverage(dataList,row) 56 | 57 | if userInput == 'c': 58 | timeStamp = 'true' 59 | data = getValues(timeStamp) 60 | dataPoint = data[0] 61 | timeCollected = data[1] 62 | timeCollected = timeCollected.strip() 63 | unitTime = timeCollected[-2:] 64 | deltaTime = timeCollected[:-2] 65 | 66 | if unitTime == 'ms': 67 | timeValue = int(deltaTime) 68 | timeValue = timeValue/1000 69 | 70 | if unitTime == 'us': 71 | timeValue = int(deltaTime) 72 | timeValue = timeValue/1000000 73 | 74 | print('Data Point: ' + dataPoint + 75 | ' Time from last point: ' + str(timeValue) + 's') 76 | 77 | if userInput == 'n': 78 | dataFile.close() 79 | -------------------------------------------------------------------------------- /Part9_AddingTimestamp/arduinoCharInput/arduinoCharInput.ino: -------------------------------------------------------------------------------- 1 | 2 | int analogPin = 3; 3 | int data = 0; 4 | char userInput; 5 | unsigned long currentTime = 0; 6 | unsigned long previousTime = 0; 7 | 8 | void setup(){ 9 | 10 | Serial.begin(9600); // setup serial 11 | 12 | } 13 | 14 | void loop(){ 15 | 16 | if(Serial.available()> 0){ 17 | 18 | userInput = Serial.read(); // read user input 19 | 20 | if(userInput == 'g'){ // if we get expected value 21 | data = analogRead(analogPin); // read the input pin 22 | Serial.println(data); 23 | 24 | } // if user input 'g' 25 | 26 | if(userInput == 'c'){ 27 | previousTime = currentTime; 28 | currentTime = millis(); 29 | 30 | data = analogRead(analogPin); 31 | Serial.print(data); 32 | Serial.print('-'); 33 | Serial.print(currentTime-previousTime); 34 | Serial.println("ms"); 35 | 36 | } // if user input 'c' 37 | } // Serial.available 38 | } // Void Loop 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /Part9_AddingTimestamp/dataSetup.ini: -------------------------------------------------------------------------------- 1 | numVariable:4 2 | numPoints:5 3 | numRowsCollect:10 4 | operator:Bob 5 | Type:Voltage 6 | -------------------------------------------------------------------------------- /Part9_AddingTimestamp/ini_read.py: -------------------------------------------------------------------------------- 1 | dataFile_ini = open('dataSetup.ini', 'r') 2 | ini_data_result = {} 3 | 4 | def getINI(): 5 | 6 | iniHeader = dataFile_ini.readline().split(':') 7 | #print(iniHeader[0] + ' ' + iniHeader[1].rstrip() ) 8 | iniReadLines = int(iniHeader[1].rstrip()) 9 | 10 | for i in range(0,iniReadLines): 11 | 12 | variable = dataFile_ini.readline().split(':') 13 | #print(variable) 14 | ini_data_result[variable[0]] = variable[1].rstrip() 15 | 16 | #print(ini_data_result) 17 | 18 | return ini_data_result 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ArduinoPySerial_LearningSeries 2 | 3 | This repository is for those who would like to learn how to use the Python module PySerial and Arduino. 4 | There will be a corresponding Arduino (.ino) and Python (.py) file for each part. Each part builds upon the previous. 5 | View the YouTube Play List for step by step instruction and detailed instructions: 6 | https://www.youtube.com/playlist?list=PLb1SYTph-GZJb1CFM7ioVY9XJYlPVUBQy 7 | 8 | WaveShapePlay 9 | --------------------------------------------------------------------------------