Arduino & Python3 Temperature Data Visualizer
This is an Arduino based project using an NTC thermistor to collect temperature data and Python 3 to save and visualize it.
Components and supplies
Arduino UNO
LED (generic)
ntc thermistor
resistor 110 ohm
Tools and machines
computer
Apps and platforms
Arduino IDE
python 3 IDEL
Project description
Code
live plot.py
python
this is the python script for plotting the data you have to install mathplotlib library
1import matplotlib.pyplot as plt 2import matplotlib.animation as animation 3from matplotlib import style 4import datetime as dta 5'''configuring the plot''' 6style.use('ggplot') 7fig = plt.figure() 8ax1 = fig.add_subplot(1,1,1) 9 10def animate(i): 11 dateconv = dta.datetime.fromtimestamp 12 '''define the function that gonna convert the unix time to date''' 13 graph_data = open('example.txt','r').read() 14 '''read the data from the txt file''' 15 lines = graph_data.split('\n') 16 ''' split the lines as a list''' 17 xs = [] 18 ''' x axis the tempurature''' 19 ys = [] 20 ''' y axis the date''' 21 for line in lines: 22 ''' read the lines and append the data to ys ans xs''' 23 if len(line) > 1: 24 '''get only the non-empty line''' 25 x, y = line.split(',') 26 x=float(x) 27 y=float(y) 28 xs.append(x) 29 y = dateconv(y) 30 ''' converte the unix time''' 31 ys.append(y) 32 '''plot the data''' 33 ax1.clear() 34 ax1.plot(ys, xs,label='temperature') 35 for label in ax1.xaxis.get_ticklabels(): 36 '''set the rotation of the date into 45 degree in order to be readable''' 37 label.set_rotation(45) 38 plt.xlabel('date') 39 plt.ylabel('temperature') 40 plt.legend() 41ani = animation.FuncAnimation(fig, animate, interval=1000) 42'''animate the plot and refresh evry 1 second''' 43plt.subplots_adjust(left=0.09, bottom=0.23, right=0.93, top=0.90, wspace=0.2, hspace=0) 44'''just configurations''' 45plt.show() 46 47
arduino_serial_temperature.ino
arduino
this is the arduino code
1 /* 2 3 4 The circuit: 5 6 7 * LCD RS pin to digital pin 12 8 * LCD Enable pin to digital pin 11 9 * LCD D4 pin to digital pin 5 10 * LCD D5 pin to digital pin 4 11 * LCD D6 pin to digital pin 3 12 * LCD D7 pin to digital pin 2 13 * LCD R/W pin to ground 14 * LCD VSS pin to ground 15 * LCD VCC pin to 5V 16 * LCD K pin to ground 17 * LCD A pin to 5V 18 * LCD V0 pin to digital pin 6 19 * LED to pin 13 20 21 22 23 put a 110 ohm resitance in series with the thermistor, the side of 110 resitance to 5V 24 the side of the thermistor to GND and the point where the thermistor and the resitence meet 25 to analog input A0 26 27 28 */ 29 30 31 32 33 34#include <LiquidCrystal.h> 35 36LiquidCrystal lcd(12,11,5,4,3,2);//configure the lcd 37 38#define a A0 // analog input to read the voltage of the thermistor 39double b;// an intermediate variable 40double Vt; 41double Rt; 42int R=110; 43int B=2800; 44int R0=5; 45int T0=25; 46double T; 47void setup() { 48analogWrite(6,120);// configure the contrast of the lcd 49lcd.begin(16,2); 50Serial.begin(115200);// using serial to send data to the computer 51} 52 53void loop() { 54 b=analogRead(a);// get the value in the A0 55 Vt= (b/1024)*5;// convert it to voltage 56 Rt=(R*Vt)/(5-Vt);// measure the resistance of the thermistor 57 T=1/((log(Rt/R0)/B)+(1/(T0+273.15))); // get the temperature in kelvin 58 Serial.println(T-273.15);// send the temperature in using serial 59 60 // desplay the temperature in the lcd 61 lcd.clear(); 62 lcd.print("temp is: "); 63 lcd.print(T-273.15); 64 lcd.print(" D"); 65 66 // trigger the security alarm if the temperature is higher than 60 degree 67 if ((T-273.15)>60){ 68 digitalWrite(13,HIGH); 69 delay(500); 70 digitalWrite(13,LOW); 71 delay(500); 72 } 73 else{ 74 delay(1000); 75 } 76 // in the both cases there will be a delay of 1s between sending the temperature to the computer and display in in the lcd 77 } 78
serial colector.py
python
this is the python script to save the data to a txt file ,you have to install serial library
1from serial import Serial 2import time 3'''set the serial to connect to the arduino (you should change the port)''' 4ser =Serial( 5 port='COM8', 6 baudrate = 115200, 7 timeout=None) 8def adddata(data): 9 '''a function to add the data to the text file''' 10 date=time.time() 11 h=str(data)+','+str(date)+'\n' 12 fh = open('example.txt', 'a') 13 fh.write(h) 14 fh.close 15while 1: 16 ''' infinit loop''' 17 while(ser.inWaiting()==0): 18 '''wait for the data from serial''' 19 pass 20 a=float(ser.readline().decode('utf-8')) 21 '''read and decode the data''' 22 adddata(a) 23 '''add the data to the txt file''' 24
live plot.py
python
this is the python script for plotting the data you have to install mathplotlib library
1import matplotlib.pyplot as plt 2import matplotlib.animation as animation 3from 4 matplotlib import style 5import datetime as dta 6'''configuring the plot''' 7style.use('ggplot') 8fig 9 = plt.figure() 10ax1 = fig.add_subplot(1,1,1) 11 12def animate(i): 13 dateconv 14 = dta.datetime.fromtimestamp 15 '''define the function that gonna convert the 16 unix time to date''' 17 graph_data = open('example.txt','r').read() 18 '''read 19 the data from the txt file''' 20 lines = graph_data.split('\ 21') 22 ''' 23 split the lines as a list''' 24 xs = [] 25 ''' x axis the tempurature''' 26 27 ys = [] 28 ''' y axis the date''' 29 for line in lines: 30 ''' 31 read the lines and append the data to ys ans xs''' 32 if len(line) > 1: 33 34 '''get only the non-empty line''' 35 x, y = line.split(',') 36 37 x=float(x) 38 y=float(y) 39 xs.append(x) 40 41 y = dateconv(y) 42 ''' converte the unix time''' 43 ys.append(y) 44 45 '''plot the data''' 46 ax1.clear() 47 ax1.plot(ys, xs,label='temperature') 48 49 for label in ax1.xaxis.get_ticklabels(): 50 '''set the rotation of the 51 date into 45 degree in order to be readable''' 52 label.set_rotation(45) 53 54 plt.xlabel('date') 55 plt.ylabel('temperature') 56 plt.legend() 57ani 58 = animation.FuncAnimation(fig, animate, interval=1000) 59'''animate the plot and 60 refresh evry 1 second''' 61plt.subplots_adjust(left=0.09, bottom=0.23, right=0.93, 62 top=0.90, wspace=0.2, hspace=0) 63'''just configurations''' 64plt.show() 65 66
arduino_serial_temperature.ino
arduino
this is the arduino code
1 /* 2 3 4 The circuit: 5 6 7 * LCD RS pin to digital pin 8 12 9 * LCD Enable pin to digital pin 11 10 * LCD D4 pin to digital pin 5 11 12 * LCD D5 pin to digital pin 4 13 * LCD D6 pin to digital pin 3 14 * LCD D7 pin 15 to digital pin 2 16 * LCD R/W pin to ground 17 * LCD VSS pin to ground 18 * LCD 19 VCC pin to 5V 20 * LCD K pin to ground 21 * LCD A pin to 5V 22 * LCD V0 pin to 23 digital pin 6 24 * LED to pin 13 25 26 27 28 put a 110 ohm resitance in series 29 with the thermistor, the side of 110 resitance to 5V 30 the side of the thermistor 31 to GND and the point where the thermistor and the resitence meet 32 to analog input 33 A0 34 35 36 */ 37 38 39 40 41 42#include <LiquidCrystal.h> 43 44LiquidCrystal 45 lcd(12,11,5,4,3,2);//configure the lcd 46 47#define a A0 // analog input to read 48 the voltage of the thermistor 49double b;// an intermediate variable 50double 51 Vt; 52double Rt; 53int R=110; 54int B=2800; 55int R0=5; 56int T0=25; 57double 58 T; 59void setup() { 60analogWrite(6,120);// configure the contrast of the lcd 61lcd.begin(16,2); 62Serial.begin(115200);// 63 using serial to send data to the computer 64} 65 66void loop() { 67 b=analogRead(a);// 68 get the value in the A0 69 Vt= (b/1024)*5;// convert it to voltage 70 Rt=(R*Vt)/(5-Vt);// 71 measure the resistance of the thermistor 72 T=1/((log(Rt/R0)/B)+(1/(T0+273.15))); 73 // get the temperature in kelvin 74 Serial.println(T-273.15);// send the temperature 75 in using serial 76 77 // desplay the temperature in the lcd 78 lcd.clear(); 79 80 lcd.print("temp is: "); 81 lcd.print(T-273.15); 82 lcd.print(" D"); 83 84 85 // trigger the security alarm if the temperature is higher than 60 degree 86 87 if ((T-273.15)>60){ 88 digitalWrite(13,HIGH); 89 delay(500); 90 digitalWrite(13,LOW); 91 92 delay(500); 93 } 94 else{ 95 delay(1000); 96 } 97 // in the both 98 cases there will be a delay of 1s between sending the temperature to the computer 99 and display in in the lcd 100 } 101
Downloadable files
temperature serial
temperature serial

temperature serial
temperature serial

Comments
Only logged in users can leave comments