Collecting sensor data from arduino using python

void setup() {
// put your setup code here, to run once:
pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(A3,INPUT);
pinMode(A4,INPUT);
pinMode(A5,INPUT);
pinMode(A6,INPUT);
pinMode(A7,INPUT);
pinMode(A8,INPUT);
pinMode(A9,INPUT);
Serial.begin(9600);
}
String val0,val1,val2,val3,val4,val5,val6,val7,val8,val9;
void loop() {
// put your main code here, to run repeatedly:
val0=(String)analogRead(A0);
val1=(String)analogRead(A1);
val2=(String)analogRead(A2);
val3=(String)analogRead(A3);
val4=(String)analogRead(A4);
val5=(String)analogRead(A5);
val6=(String)analogRead(A6);
val7=(String)analogRead(A7);
val8=(String)analogRead(A8);
val9=(String)analogRead(A9);
String result=val0+" "+val1+" "+val2+" "+val3+" "+val4+" "+val5+" "+val6+" "+val7+" "+val8+" "+val9;
Serial.write(result);
delay(500);
}
def create_new_csv(fileName,col_names):    with open(fileName,'w',newline='') as _file:        csv_writer=writer(_file)        csv_writer.writerow(col_names)
def write_to_csv(fileName,data):    now=datetime.now() #get the current timestamp    dt_string=now.strftime("%d/%m/%Y %H:%M:%S")    with open(fileName,'a+',newline='') as _file:        csv_writer=writer(_file)        print([dt_string]+data)        csv_writer.writerow([dt_string]+data) #write data to csv
def readData(fileName,com_port='Com6',baud_rate=9600):    arduino=serial.Serial(com_port,baud_rate,timeout=0.1)
# the data is read continuously and infinitely form the arduino
#although a stopping criteria could be used to stop execution
while True: data=arduino.readline()[:-2]#reads the data from the arduino data=data.split()# remember we used spaces to separate values if data: write_to_csv(fileName,data)#write data to file print(data)#just for satisfaction

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store