Lesson 36 Thermostatic Water Tank

Share for us

Introduction

After having learnt so many modules independently, let’s combine these modules together to make some funny interactive works. In this lesson, we will use a Rotary Encoder module, a Thermistor module, a Relay module, a Button module and an I2C LCD1602, all of which we have learnt previously, to assemble a Thermostatic Water Tank. The tank can be used in the test department of medical organizations and colleges, scientific research, chemical industry, printing and dyeing industry to distil, dry, inspissate and heat chemical medicines and cultivate biological products in constant temperature, as well as to boil and disinfect equipment.

Components

– 1 * SunFounder Uno board

– 1 * USB data cable

– 1 * Rotary Encoder module

– 1 * Thermistor module

– 1 * I2C LCD1602

– 1 * Relay module

– 1 * Button module

– 3 * 3-Pin anti-reverse cable

– 1 * 5-Pin anti-reverse cable

– 1 * 4-Pin anti-reverse cable

– 2 * Dupont wire (M to M)

– 1 * Breadboard

Experimental Principle

How a thermostatic water tank works

Set a temperature value for the tank. When the temperature is lower than that set, the tank gets heated and is power off till the temperature reaches the set value. Thus, the tank can keep a constant temperature all the time.

Based on the principle, in this experiment, use an LCD screen and a thermistor module to both display value of the current temperature and the value set. Also use a rotary encoder to allow the set temperature value adjustable.

When the temperature is lower than that set, the normally open contact of the relay is closed and the characters on the LCD blur.  Touch the thermistor with fingers and the temperature gets higher. When it reaches or is higher than the value set, the contact is open and the characters will become clear again.

For more about each component in the experiment, please see the previous experiments.

Experimental Procedures

Step 1: Build the circuit

The wiring between the Button module and SunFounder Uno board:

ButtonSunFounder Uno
SIG10
VCC5V
GNDGND

The wiring between the Thermistor module and SunFounder Uno is as shown below:

ThermistorSunFounder Uno
SIGA0
VCC5V
GNDGND

The wiring between the Rotary Encoder module and SunFounder Uno:

Rotary EncoderSunFounder Uno
CLK7
DT6
SW5
VCC5V
GNDGND

The wiring between the Relay module and SunFounder Uno:

RelaySunFounder Uno
SIG2
VCC5V
GNDGND

The wiring between the I2C LCD1602 module and SunFounder Uno:

I2C LCD1602SunFounder Uno
VCC5V
GNDGND
SDAA4
SCLA5

Step 2: Program (Please refer to the example code in LEARN -> Get Tutorial on our website)

Note: Here you need to add a library. Refer to the description in Lesson 1 previously in the manual.

Step 3: Compile the program

Step 4: Upload the sketch to SunFounder Uno board

Now, after the startup, the LCD1602 first displays Thermostatic Water Tank and then the value of the current temperature. Press the button on the Button module to enter setup mode. Then rotate the Rotary Encoder to change the threshold of temperature value. After setting a value, for example, 30℃, you can press the switch on the Rotary Encoder module to confirm.

If the current temperature is lower than 30℃ (the threshold you just set), the normally open contact of the relay is closed. Thus, the characters on the LCD become blurred and the LED on the SunFounder Uno goes out. Touch the thermistor. Then the value displayed on the LCD will increase and at last the LED on the Uno board will light up. Release your fingers and the value of temperature displayed on the LCD will decrease. When the value reaches the threshold, the contact is open, characters on the LCD blurred, and LED out.

 Code

/***********************************************************
* name:Thermostatic Water Tank
* function:after the startup, the LCD1602 first displays Thermostatic Water Tank and then the value of the current temperature.
* Press the button on the Button module to enter setup mode.
* Then rotate the Rotary Encoder to change the threshold of temperature value.
* After setting a value, for example, 30℃, you can press the switch on the Rotary Encoder module to confirm.
* If the current temperature is lower than 30℃ (the threshold you just set), the normally open contact of the relay is closed.
**********************************************************/
//Email:support@sunfounder.com
//website:www.sunfounder.com#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include “encoder.h”
#include <EEPROM.h>
LiquidCrystal_I2C lcd(0x27,16,2);
ENCODER encoder;
#define buttonPin 10 //the key attach to
#define temPin A0 //the thermistor attach to
#define relayPin 2 //the relay attach to
#define ledPin 13
float upperTem = 0.00;
#define beta 3950 //the beta of the thermistor
#define resistance 10 //the value of the pull-down resistor
float hysteresis = 0.25;
void setup()
{
  pinMode(relayPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(relayPin, HIGH);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print(” Thermostatic “);
  lcd.setCursor(0, 1);
  lcd.print(” water tank “);
  encoder.Init(7, 6, 5);// Init(CLKPIN, DTPIN, SWPIN)
  pinMode(buttonPin, INPUT);
  upperTem = EEPROM.read(0);
  delay(1000);
}void loop()
{
  if(digitalRead(buttonPin) == 0)
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(” Set Mode “);
    lcd.setCursor(0, 1);
    lcd.print(” start… “);
    delay(500);
    lcd.clear();
    while(1)
    {
      lcd.setCursor(0, 0);
      lcd.print(“Adjust: “);
      float change = encoder.turnSettle();
      upperTem = upperTem + change;
      lcd.print(upperTem);
      if(encoder.pressed())
      {
        EEPROM.write(0, upperTem);
        delay(100);
        lcd.setCursor(0, 1);
        lcd.print(“Upper Tem: “);
        lcd.print((float) EEPROM.read(0));
        delay(1000);
        lcd.clear();
        break;
      }
    }
  }
  else
  {
    //read thermistor value
    long a = 1024 – analogRead(temPin);
    //the calculating formula of temperature
    float tempC = beta /(log((1025.0 * 10 / a – 10) / 10) + beta / 298.0) – 273.0;
    float tempF = 1.8*tempC + 32.0;
    // Print a message of “Temp: “to the LCD.
    // set the cursor to column 0, line 0
    lcd.setCursor(0, 0);
    lcd.print(“Temp: “);
    // Print a centigrade temperature to the LCD.
    lcd.print(tempC);
    // Print the unit of the centigrade temperature to the LCD.
    lcd.print(char(223));
    lcd.print(” C”);
    // set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
    lcd.setCursor(0, 1);
    lcd.print(“Fahr: “);
    // Print a Fahrenheit temperature to the LCD.
    lcd.print(tempF);
    // Print the unit of the Fahrenheit temperature to the LCD.
    lcd.print(char(223));
    lcd.print(” F”);
    delay(100); //wait for 100 milliseconds
    if(tempC >= upperTem + hysteresis)
    {
      digitalWrite(relayPin, HIGH);
      digitalWrite(ledPin, HIGH);
    }
    else if(tempC < upperTem – hysteresis)
    {
      digitalWrite(relayPin, LOW);
      digitalWrite(ledPin, LOW);
    }
  }