Exercise 33 - Thermostate

1.      INTRODUCTION

A thermostat is the component of a system, which can measure and regulates the temperature. The simplest version consists in a metal sheet that curve itself opening the circuit if the temperature reaches a determinate value. On this exercise, we are going to simulate the regulation with LEDs and the measure viewer with a four digits 7 segment display.


2.      HOW TO CONNECT?

This exercise is a join between CONNECTING_A_TEMPERATURE_AND_RELATIVE_HUMIDITY_SENSOR and CONNECTING_A_LED_DISPLAY but the DHT pin has been changed to pin2 on Arduino board. On the other hand, three LEDs have been added on pinA0-A1-A2. You can connect the LEDs with a standard LED connection (220 ohms resistor).


EXERCISE_A (Thermostat)


Materials:

·         Arduino UNO board

·         Protoboard

·         Some wire Jumpers

·         LED display

·         10x 220 ohms resistor

·         Temperature and relative humidity sensor

·         10K ohms resistor

·         3x Different colour LEDs


3.      PROGRAMMING

This program writes on a four digits 7 segment display the ambient temperature, which is measured by DHT11 sensor. Furthermore, the program has three states: normal, warning, alarm and danger.

The code is similar to CONNECTING_A_LED_DISPLAY.

#include "DHT.h"

 

#define DHTPIN 2     // Arduino pin4, connected to pin2 in sensor

// There are DHT type sensors. Uncoment the sensor which you will go use

#define DHTTYPE DHT11   // DHT 11

//#define DHTTYPE DHT22   // DHT 22  (AM2302)

//#define DHTTYPE DHT21   // DHT 21 (AM2301)

 

//Digit pins

int d1 = 13;

int d2 = 12;

int d3 = 11;

int d4 = 10;

//Segment pins

int a = 9;

int b = 8;

int c = 7;

int d = 6;

int e = 5;

int f = 4;

int g = 3;

//number variables

long n = 0;

int x = 100;

int del = 45;

//Alarm LEDs

int green = A0;

int yellow = A1;

int red = A2;

int i = 0; //Variable that saves the alarm state

 

// Initialize sensor

DHT dht(DHTPIN, DHTTYPE);

 

//Set all as OUTPUT

void setup() {

  pinMode(a, OUTPUT);

  pinMode(b, OUTPUT);

  pinMode(c, OUTPUT);

  pinMode(d, OUTPUT);

  pinMode(e, OUTPUT);

  pinMode(f, OUTPUT);

  pinMode(g, OUTPUT);

  pinMode(d1, OUTPUT);

  pinMode(d2, OUTPUT);

  pinMode(d3, OUTPUT);

  pinMode(d4, OUTPUT);

  Serial.begin(9600);

  dht.begin();

  pinMode(green, OUTPUT);

  pinMode(yellow, OUTPUT);

  pinMode(red, OUTPUT);

 

}

 

On loop, we profit the % operator, which, in Arduino, returns the remainder of when one integer is divided by another. Thanks to it, variable n can be separated in individual digits.

void loop() {

  delay(1);

  int n = dht.readTemperature();

 

The code reads temperature value. If n is smaller than 29 the state is normal and only the LED green is turning on. If the value is between 29 and 30, the state is warning and the LEDs green and yellow are turned on. On the last, if the value is higher than 30 the state is dangerous, all the LEDs are turned on.

On the other hand, when the danger has been activated, it is only deactivated if the temperature goes down to 29 or less.

 

  if (n < 29) {

    digitalWrite(green, HIGH);

    digitalWrite(yellow, LOW);

    digitalWrite(red, LOW);

    i=0;

  }

  else if (n >= 29 && n < 30 && i==0) {

    digitalWrite(green, HIGH);

    digitalWrite(yellow, HIGH);

    digitalWrite(red, LOW);

  }

  else {

    digitalWrite(green, HIGH);

    digitalWrite(yellow, HIGH);

    digitalWrite(red, HIGH);

    i=1;

  }

  n = n * 1000;

  // Validate if the sensor is sending meassures

  if (isnan(n)) {

    Serial.println("Sensor read failed!");

    return;

  }

 

  clearLEDs();

  SelectDigit(1);

  SelectNumber((n / x / 1000) % 10);

  delayMicroseconds(del);

 

  clearLEDs();

  SelectDigit(2);

  SelectNumber((n / x / 100) % 10);

  delayMicroseconds(del);

 

  clearLEDs();

  SelectDigit(3);

  SelectNumber((n / x / 10) % 10);

  delayMicroseconds(del);

 

  clearLEDs();

  SelectDigit(4);

  digitalWrite(a, LOW);

  digitalWrite(b, LOW);

  digitalWrite(f, LOW);

  digitalWrite(g, LOW);

 

 

 

  delayMicroseconds(del);

 

}

//To simplify loop, you should create functions out of it.

 

 

//This function do no return anything. It is used to select

//the digit where you want to write. You must intruduce the number

//of digit.

void SelectDigit(int x)

{

  digitalWrite(d1, LOW);

  digitalWrite(d2, LOW);

  digitalWrite(d3, LOW);

  digitalWrite(d4, LOW);

 

  switch (x)

  {

    case 1: digitalWrite(d1, HIGH); break;

    case 2: digitalWrite(d2, HIGH); break;

    case 3: digitalWrite(d3, HIGH); break;

    default: digitalWrite(d4, HIGH); break;

  }

}

 

//Do no return anything. It is used to select the number which

//you want to write. You must intruduce the number

void SelectNumber(int x)

{

  switch (x)

  {

    default: zero(); break;

    case 1: one(); break;

    case 2: two(); break;

    case 3: three(); break;

    case 4: four(); break;

    case 5: five(); break;

    case 6: six(); break;

    case 7: seven(); break;

    case 8: eight(); break;

    case 9: nine(); break;

  }

}

 

void clearLEDs()

{

  digitalWrite(a, HIGH);

  digitalWrite(b, HIGH);

  digitalWrite(c, HIGH);

  digitalWrite(d, HIGH);

  digitalWrite(e, HIGH);

  digitalWrite(f, HIGH);

  digitalWrite(g, HIGH);

}

 

void zero()

{

  digitalWrite(a, LOW);

  digitalWrite(b, LOW);

  digitalWrite(c, LOW);

  digitalWrite(d, LOW);

  digitalWrite(e, LOW);

  digitalWrite(f, LOW);

  digitalWrite(g, HIGH);

}

 

void one()

{

  digitalWrite(a, HIGH);

  digitalWrite(b, LOW);

  digitalWrite(c, LOW);

  digitalWrite(d, HIGH);

  digitalWrite(e, HIGH);

  digitalWrite(f, HIGH);

  digitalWrite(g, HIGH);

}

 

void two()

{

  digitalWrite(a, LOW);

  digitalWrite(b, LOW);

  digitalWrite(c, HIGH);

  digitalWrite(d, LOW);

  digitalWrite(e, LOW);

  digitalWrite(f, HIGH);

  digitalWrite(g, LOW);

}

 

void three()

{

  digitalWrite(a, LOW);

  digitalWrite(b, LOW);

  digitalWrite(c, LOW);

  digitalWrite(d, LOW);

  digitalWrite(e, HIGH);

  digitalWrite(f, HIGH);

  digitalWrite(g, LOW);

}

 

void four()

{

  digitalWrite(a, HIGH);

  digitalWrite(b, LOW);

  digitalWrite(c, LOW);

  digitalWrite(d, HIGH);

  digitalWrite(e, HIGH);

  digitalWrite(f, LOW);

  digitalWrite(g, LOW);

}

 

void five()

{

  digitalWrite(a, LOW);

  digitalWrite(b, HIGH);

  digitalWrite(c, LOW);

  digitalWrite(d, LOW);

  digitalWrite(e, HIGH);

  digitalWrite(f, LOW);

  digitalWrite(g, LOW);

}

 

void six()

{

  digitalWrite(a, HIGH);

  digitalWrite(b, HIGH);

  digitalWrite(c, LOW);

  digitalWrite(d, LOW);

  digitalWrite(e, LOW);

  digitalWrite(f, LOW);

  digitalWrite(g, LOW);

}

 

void seven()

{

  digitalWrite(a, LOW);

  digitalWrite(b, LOW);

  digitalWrite(c, LOW);

  digitalWrite(d, HIGH);

  digitalWrite(e, HIGH);

  digitalWrite(f, HIGH);

  digitalWrite(g, HIGH);

}

 

void eight()

{

  digitalWrite(a, LOW);

  digitalWrite(b, LOW);

  digitalWrite(c, LOW);

  digitalWrite(d, LOW);

  digitalWrite(e, LOW);

  digitalWrite(f, LOW);

  digitalWrite(g, LOW);

}

 

void nine()

{

  digitalWrite(a, LOW);

  digitalWrite(b, LOW);

  digitalWrite(c, LOW);

  digitalWrite(d, HIGH);

  digitalWrite(e, HIGH);

  digitalWrite(f, LOW);

  digitalWrite(g, LOW);

}

You can see more comments to this code in EXERCISE_12 and EXERCISE_24.


Última modificación: Tuesday, 21 de January de 2020, 10:33