Some new stuff
  1. Arduino Park Control
  2. Arduino USB2serial programming the bare bone
  3. Arduino Projects Videos added
  4. Arduino Xbee projects
  5. Arduino dB Meter 
  6. Trying to get the IOIO going. 
  7. IOIO is going....bluetooth
  8. Attiny 2313 bluetooth bee RC car
  9. Compass and temperature added
  10. AVR programmer
  11. USB2Serial and Ethernet POE
  12. Arduino LPG Sensor
  13. Program the Arduino Mini 05
  14. Lipo Rider Pro in action
  15. EZ Robot Builder kit arrived 10 Nov 2012
  16. Arduino VA Meter!
  17. 05 Nov 2012 Project Sentry Gun.
  18. Attiny85 PIR Sensor 10 Dec 2012 
  19. 0-30VDC3A and 2-28VDC10A bench PSU's
  20. 19 Dec 2012 Arduino Voltmeter LCD
  21. 29 Dec 2012 Arduino Tachometer&Speedometer LCD
  22. 03 Jan 2013 Arduino AC Phase Control. 
  23. 18 Jan 2013 Arduino Distance Meter
  24. 22 Jan 2013 Arduino Digital Clock and Date
  25. 31 Jan 2013 Arduino Digital Clock and Date 8x2 LCD
  26. Feb 2013 LCD and Backpack
  27. Feb 2013 7 Segment Serial Clock Sparkfun. Distance Sensor with "newping" library
  28. Feb 2013 Lelo Remote
  29. 01/03/2013 Arduino Adafruit 7 Seg Digital Clock with RTC and backpack 
  30. Arduino Frequency Meter 26/03/2013
  31. Another RC  Car controlled by Arduino 06/04/2013
  32. My quadcopter project update 25 April 2013
  33. My Aqua Quad Copter Flying 26 April 2013enlightened
  34. Another monster RC truck;strong one! 11July2013cool
  35. X-Frame Quad Copter 31 July 2013cool
  36. Arduino GSM Sheild added.28/8/2013
  37. 1.8inch TFT screen 13/09/2013cool
  38. 04/11/2014 added PID Soldering Iron Control
  39. Added bluetooth servo control 04/11/2014
  40. ESP8266 first try.16/03/2015
  41. Arduino Uno Quadcopter 19 May 2015
  42. IOT WemosD1 Amp Meter

 

 



13/09/2013

Arduino TFT LM35 Temp Sensor

 

First I tried and tested the mini environment monitor, from the instructables.com website and then decided to change the code.

The buzzer, led, relay and the screen display will activate on a set temperature in the code.

If else statement incorporated for various screen display conditions.

The code make provision for decimal display as well as average temperature.

I changed the screen rotation to better display the result in landscape resulting in larger font sizes.

The code also displays min and max temperatures with printed setpoint temperature.

Current draw about 110mA

1. LM35 on A0

2. 1.8 inch TFT Iteadstudio screen ITDB02-1.8SP

3. Remember the libraries

4. Video HERE

Code:

//1.04 IDE
// this sketch was created using code from both the adafruit and the virtuabotix sample sketches
// You can use any (4 or) 5 pins
#include
#define sclk 4
#define mosi 5
#define cs   6
#define dc   7
#define rst  8  // you can also connect this to the Arduino reset
#include    // Core graphics library
#include // Hardware-specific library
Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, mosi, sclk, rst);
int temperature = 0;       //declare a variable to store obtained temperature 
const char analog_pin = A0;   //assign analog_pin with analog pin A0 
const char buzzer = 2;//assign buzzer with digital pin 2
int relayPin = 12;//Relay via transistor
const int ledPin = 13;
int mini = 1000;//Must be higher than 100 to display result
int maxi = -100;

void setup()
{
  pinMode(ledPin,OUTPUT);//set ledPin13 as output
  pinMode(buzzer,OUTPUT);  //set buzzer (or pin 2) as output
  pinMode(relayPin,OUTPUT);
  pinMode(analog_pin,INPUT); //set analog_pin (or pin A0) as input
  analogReference(INTERNAL); //configure the ADC reference voltage to 1.1V (internal built-in)
  Serial.begin(9600);
  tft.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab
  tft.setRotation(tft.getRotation()+1); //uncomment to rotate display
  // get time to display "sensor up time"
  uint16_t time = millis();
  tft.fillScreen(ST7735_BLACK);
  time = millis() - time;
  tftPrintTest();
  delay(500);
  tft.fillScreen(ST7735_BLACK);
  // optimized lines
  testfastlines(ST7735_RED, ST7735_BLUE);
  delay(500);
  testdrawrects(ST7735_GREEN);
  delay(500);
  tft.fillScreen(ST7735_BLACK);
}

void loop()
{
  temperature = get_temperature();  //get the temperature
  if(temperature > maxi) {maxi = temperature;} // set max temperature
  if(temperature < mini) {mini = temperature;} // set min temperature
  if(temperature>320) {  //test if the temperature > 29(or any other given temperature setting)celsius
    beep();//buzzer activated
    blink_LED();//led in synch with buzzer
    highTemp();//if yes, activate red text display
    relayControl();//if yes, activate relay pin 12
     
  } else {
    digitalWrite(relayPin, LOW);  
    tft.setTextColor(ST7735_WHITE);
    tft.setCursor(32,0);
    tft.setTextSize(2);
    tft.println("CHEMCOOL");
    tft.setCursor(0, 16);
    tft.setTextSize(2);
    tft.setTextColor(ST7735_BLUE);
    tft.print("Max=");
    tft.setCursor(48, 16);
    tft.setTextSize(2);
    tft.setTextColor(ST7735_RED);
    tft.print(maxi/10,DEC);//current temp and set to max
    tft.setCursor(70, 15);
    tft.setTextSize(2);
    tft.print(".");
    tft.setCursor(80, 16);
    tft.setTextSize(2);
    tft.setTextColor(ST7735_RED);
    tft.print(maxi%10,DEC);//remainder of current temperature
    tft.setCursor(90, 16);
    tft.setTextSize(2);
    tft.setTextColor(ST7735_YELLOW);
    tft.print("(32.0)");//remainder of current temperature
    tft.setCursor(0, 92);
    tft.setTextSize(2);
    tft.setTextColor(ST7735_BLUE);
    tft.print("Min=");
    tft.setCursor(48, 93);
    tft.setTextSize(2);
    tft.setTextColor(ST7735_RED);
    tft.print(mini/10,DEC);//current temp and set to min
    tft.setCursor(70, 93);
    tft.setTextSize(2);
    tft.print(".");
    tft.setCursor(80, 93);
    tft.setTextSize(2);
    tft.setTextColor(ST7735_RED);
    tft.print(mini%10,DEC);//remainder of current temperature
    tft.setCursor(0, 40);
    tft.setTextColor(ST7735_YELLOW);
    tft.setTextSize(6);
    tft.print(temperature/10,DEC);
    tft.setCursor(65, 60);
    tft.setTextSize(3);
    tft.print(".");
    tft.setTextSize(6);
    tft.setCursor(85, 40);
    tft.print(temperature%10,DEC);
    tft.setCursor(20, 110);
    tft.setTextSize(2);
    tft.setTextColor(ST7735_WHITE);
    tft.setCursor(130, 35);
    tft.setTextSize(2);
    tft.setTextColor(ST7735_YELLOW);
    tft.print("o");
    tft.setCursor(130, 55);
    tft.setTextSize(4);
    tft.setTextColor(ST7735_YELLOW);
    tft.print("C");
    tft.setCursor(32,110);
    tft.setTextSize(2);
    tft.setTextColor(ST7735_WHITE);
    tft.println(" -2013-");
    delay(10000);
    tft.fillScreen(ST7735_BLACK);
    
    }
}
  void beep()
{
  //buzzer sound pattern
  digitalWrite(buzzer,HIGH);  //buzzer on
  delay(50);  //short delay
  digitalWrite(buzzer,LOW);  //buzzer off
  delay(50);  //short delay
  digitalWrite(buzzer,HIGH);  //buzzer on
  delay(50);  //short delay
  digitalWrite(buzzer,LOW);  //buzzer off
  delay(50);  //short delay
}
void blink_LED ()
{
  //blink ledPin pattern
  digitalWrite(ledPin,HIGH);  //led on
  delay(50);  //short delay
  digitalWrite(ledPin,LOW);  //led off
  delay(50);  //short delay
  digitalWrite(ledPin,HIGH);  //led on
  delay(50);  //short delay
  digitalWrite(ledPin,LOW);  //led off
  delay(50);  //short delay
}
void highTemp()
  {
  tft.setCursor(20, 50);
  tft.setTextColor(ST7735_RED);
  tft.setTextSize(6);
  tft.print(temperature/10,DEC);
  tft.setCursor(85, 72);
  tft.setTextSize(3);
  tft.print(".");
  tft.setTextSize(6);
  tft.setCursor(105, 50);
  tft.print(temperature%10,DEC);
  delay(2000);
  tft.fillScreen(ST7735_BLACK);
  
}
void relayControl()

    digitalWrite(relayPin, HIGH);
}
    
//Get temperature subroutine
int get_temperature()
{
  /*****get temperature subroutine*****
  -The formula to convert ADC reading to temperature is shown below:
   Temperature = 100 x ADC reading x 1.1 / 1024 x 10
  -The temperature sensor (LM35DZ or LM35DT) sensing range is between 0 - 100 celsius
  -Sensitivity --> 10mv per celsius
  -With the sensing range --> 0 - 100 celsius, the LM35DZ/DT voltage output --> 0 - 1V
  -With analog voltage reference = 1.1, maximum ADC value obtained = 1 / 1.1 * 1024 = 930. Hence, the ADC value range is between 0 - 930
  -A multipler 1.1 have to be introduced so that we can get 100 celsius when the value is 930 as 930 x 1.1 --> approximately 1024
  -Another multipler 10 also have to be introduced, so we can obtain the temperature in one decimal place instead of rounded number without decimal place
   eg:
   As temperature --> 61.5 celsius is sensed,
   -if multiplier 10 is introduced, average temperature store in a variable average_temperature is 615 instead of 61
   -the round number part is obtained using average_temperature / 10 --> 61
   -the decimal number part is obtained using average_temperature % 10 --> 5
   -By manipulating this, we can display the temperature with one decimal place
  ************************************/
  
  float adc_value = 0;  //declare a variable to store ADC value
  float total_temperature = 0;  //declare a variable to store sum of multiple temperatures
                                //for average temperature calculation
  int average_temperature = 0;  //declare a variable to store the average temperature calculated
  
  for(int count =0; count<100; count++)  //obtainning the sum of multiple temperatures (100 samples in this case) 
  {
    adc_value = analogRead(analog_pin);  //obtain ADC reading
    total_temperature += adc_value/1024*100*1.1*10;  //accumulate the sum of temperatures untill all samples are taken, same as total_temperature = total_temperature + adc_value/1024*100*1.1*10;
  }
  average_temperature = int(total_temperature / 100);  //calculate the average temperature, convert it from float to integer 
  return average_temperature;  //return average temperature to the main 
}
 
void tftPrintTest() {
  tft.setTextWrap(false);
  tft.fillScreen(ST7735_BLACK);
  tft.setCursor(0, 30);
  tft.setTextColor(ST7735_RED);
  tft.setTextSize(2);
  tft.println("LM35");
  tft.println("Temperature");
  tft.setTextColor(ST7735_YELLOW);
  tft.setTextSize(2);
  tft.println("Monitor");
  tft.setTextColor(ST7735_BLUE);
  tft.setTextSize(2);
  tft.println("Chemcool");
  tft.setTextColor(ST7735_RED);
  tft.setTextSize(2);
  tft.println("2013");
  delay(5000);
  }
 
void testfastlines(uint16_t color1, uint16_t color2) {
  tft.fillScreen(ST7735_BLACK);
  for (int16_t y=0; y < tft.height(); y+=5) {
    tft.drawFastHLine(0, y, tft.width(), color1);
  }
  for (int16_t x=0; x < tft.width(); x+=5) {
    tft.drawFastVLine(x, 0, tft.height(), color2);
  }
}

void testdrawrects(uint16_t color) {
  tft.fillScreen(ST7735_BLACK);
  for (int16_t x=0; x < tft.width(); x+=6) {
    tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color);
  }
}

Back Back to top