Task:write an application that interfaces with an input &|| output device that you madeArduino CodeWell here I have used the same arduino code that i have used for the input week, but without the indicating led
// which analog pin to connect
#define THERMISTORPIN A0
// resistance at 25 degrees C
#define THERMISTORNOMINAL 100000
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 4267
// the value of the 'other' resistor
#define SERIESRESISTOR 100000
uint16_t samples[NUMSAMPLES];
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
uint8_t i;
float average;
// take N samples in a row, with a slight delay
for (i=0; i < NUMSAMPLES; i++) {
samples[i] = analogRead(THERMISTORPIN);
delay(10);
}
// average all the samples out
average = 0;
for (i=0; i < NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
//Serial.print("Average analog reading ");
//Serial.println(average);
// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
//Serial.print("Thermistor resistance ");
//Serial.println(average);
float steinhart;
steinhart = average / THERMISTORNOMINAL; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to C
//Serial.print( analogRead(THERMISTORPIN));
//Serial.print("------------------\n");
//Serial.print("Temperature ");
Serial.println(steinhart);
//Serial.println(" *C");
delay(200);
}
Python CodeHere I have made a Thermistor GUI, so that we can visual track how the value changes. The code is well documented below, so there is no need to write about it here again
import serial #here we import serial
from tkinter import * #here we import everything from tkinter
my_window = Tk(); #with this start our GUI window
my_window.title("Thermisotr 100k "); #here we change a Title of the window, like in the html
my_window.geometry('512x265'); # here we set the sizes our window
canvas = Canvas(my_window, width=512, height=265, bg='#002'); # now to drow something I have used the canvas
canvas.pack(); #here we activate a canvas
canvas.create_text(256,50,fill="darkblue",font="Times 20 italic bold",
text="Thermistor C"); # here we create a text
a = canvas.create_rectangle(50, 103, 450, 153, fill='white'); # creating a background rectangle
b = canvas.create_rectangle(50, 108, 450, 148, fill='Green'); # creating a foreground certangle
textc = canvas.create_text(256,125,fill="#000",font="Times 20 italic bold", text="Waiting..."); #second writing
mySerial = serial.Serial('COM6',9600); #starting the sserial, define the port and baudrate
while True: #infinite loop
s = mySerial.readline(); # read serialline
s = str(s); # convert the input to the string
print(s);
s = s.replace("b","").replace("'","").replace("\\r\\n",""); #removing unneede string parts
print(s);
celsius = float(s) # convert to float
if celsius > 0: # want to read only positive values
celsius2=(400/350)* celsius; # this is needed to the temp-showing rect.
print(celsius);
canvas.coords(b,50, 108, 50+celsius2, 148); # changing the length of the rect
canvas.itemconfig(textc,text=str(celsius)); # changing the text with the Celsius value
my_window.update_idletasks(); # here we update the tast
my_window.update(); # and the window