====== Thermometer ====== **//The Circuit://** {{:hpc0:thermometer.jpg?nolink|}} **//The Code://** 1 /********************************************************************** 2 * Filename : Thermometer.cpp 3 * Description : DIY Thermometer 4 * Author : www.freenove.com 5 * modification: 2020/03/09 6 **********************************************************************/ 7 #include 8 #include 9 #include 10 #include 11 12 ADCDevice *adc; // Define an ADC Device class object 13 14 int main(void){ 15 adc = new ADCDevice(); 16 int adcValue; //get input from hardware 17 float voltage; //stores the voltage 18 float Rt; //stores the resistance of thermistor 19 float tempK; //Temp in Kelvin 20 float tempC; //Temp in Celsius 21 printf("Program is starting ... \n"); 22 23 if(adc->detectI2C(0x48)){ //Is it the pcf8591 24 delete adc; //Empty previous hardware 25 adc = new PCF8591(); //Set the hardware 26 } 27 else if(adc->detectI2C(0x4b)){ //Is it the ads7830 28 delete adc; //Empty previous hardware 29 adc = new ADS7830(); //Set the hardware 30 } 31 else{ 32 printf("No correct I2C address found, \n" 33 "Please use command 'i2cdetect -y 1' to check the I2C address! \n" 34 "Program Exit. \n"); 35 return -1; 36 } 37 printf("Program is starting ... \n"); 38 while(1){ 39 adcValue = adc->analogRead(0); //read analog value A0 pin 40 voltage = (float)adcValue / 255.0 * 3.3; //calculate voltage 41 Rt = 10 * voltage / (3.3 - voltage); //calculate resistance value of thermistor 42 tempK = 1/(1/(273.15 + 25) + log(Rt/10)/3950.0); //calculate temperature (Kelvin) 43 tempC = tempK -273.15; //calculate temperature (Celsius) 44 printf("ADC value : %d ,\tVoltage : %.2fV, \tTemperature : %.2fC\n",adcValue,voltage,tempC); 45 delay(100); 46 } 47 return 0; 48 } This project has the same setup as [[hpc0:nightlamp|Nightlamp]] and [[hpc0:softlight|Softlight]] with the difference being that this project uses a thermistor to detect how much voltage should be running through the circuit. So when the program runs the thermistor will detect the temperature and with that change its resistance. This resistance will tell us how hot it is and how much voltage is going through the system. As for the code I aligned the comments, added comments to the conditional, and I moved all the variable declarations to be outside the loop.