انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Maker: home
0. Lab tour1
1. The start: the Arduino6
2. Wiring and the traffic light4
3. Inputs: the button4
4. Sound and movement5
5. Sensors and numbers5
6. Distance, the screen and colours5
7. Motors and the car4
8. The project: the city's smart gate3
9. From blocks to C++ code5
10. Sensors and screens in code5
11. Smart systems5
12. Bluetooth: control from the phone1

A thermometer with an alarm

Section 11: Smart systemsLesson 11.3 · ⏱ 8 min

The temperature on the screen, and above 30 degrees the red LED lights up.

⚙️ Temperature and alarm
#include <DHT.h>
#include <LiquidCrystal_I2C.h>

DHT dht(2, DHT11);
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  dht.begin();
  lcd.init();
  lcd.backlight();
  pinMode(13, OUTPUT);
}

void loop() {
  float t = dht.readTemperature();
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(t);
  if (t > 30) {
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);
  }
  delay(500);
}
35 degrees: the red LED is on
35 degrees: the red LED is on
25 degrees: off
25 degrees: off
🖥️ What happens on the stage?
We ran this code in Robo Maker and the challenge passed: “Bravo! It works”.

📌 Another example: the cold alarm

The LED lights if the temperature drops below 10.

⚙️ The cold alarm
#include <DHT.h>
#include <LiquidCrystal_I2C.h>

DHT dht(2, DHT11);
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  dht.begin();
  lcd.init();
  lcd.backlight();
  pinMode(13, OUTPUT);
}

void loop() {
  float t = dht.readTemperature();
  lcd.setCursor(0, 0);
  lcd.print(t);
  if (t < 10) {
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);
  }
  delay(500);
}
22 degrees: off
22 degrees: off
4 degrees: on
4 degrees: on
✍️ Practise
Make the thermometer.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

A heat alarm: above 30 the LED lights up and it writes “HOT!”.

👀 Show the solution
⚙️ Solution
#include <DHT.h>
#include <LiquidCrystal_I2C.h>

DHT dht(2, DHT11);
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  dht.begin();
  lcd.init();
  lcd.backlight();
  pinMode(13, OUTPUT);
}

void loop() {
  float t = dht.readTemperature();
  lcd.clear();
  lcd.setCursor(0, 0);
  if (t > 30) {
    digitalWrite(13, HIGH);
    lcd.print("HOT!");
  } else {
    digitalWrite(13, LOW);
    lcd.print(t);
  }
  delay(500);
}
In the lab
In the lab

Result: HOT! and the LED is on.

Medium

The LED blinks if the temperature goes above 35.

👀 Show the solution
⚙️ Solution
#include <DHT.h>
#include <LiquidCrystal_I2C.h>

DHT dht(2, DHT11);
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  dht.begin();
  lcd.init();
  lcd.backlight();
  pinMode(13, OUTPUT);
}

void loop() {
  float t = dht.readTemperature();
  if (t > 35) {
    digitalWrite(13, HIGH);
    delay(200);
    digitalWrite(13, LOW);
    delay(200);
  } else {
    digitalWrite(13, LOW);
  }
}
In the lab
In the lab

Result: blinking in great heat.

Hard

Three states on the screen: COLD below 15, HOT above 30, otherwise NICE.

👀 Show the solution
⚙️ Solution
#include <DHT.h>
#include <LiquidCrystal_I2C.h>

DHT dht(2, DHT11);
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  dht.begin();
  lcd.init();
  lcd.backlight();
  pinMode(13, OUTPUT);
}

void loop() {
  float t = dht.readTemperature();
  lcd.clear();
  lcd.setCursor(0, 0);
  if (t < 15) {
    lcd.print("COLD");
  } else if (t > 30) {
    lcd.print("HOT");
  } else {
    lcd.print("NICE");
  }
  delay(500);
}
In the lab
In the lab

Result: NICE, then COLD.

Finished the lesson? Mark it so your progress counts.