DHT22 Temperatura e umidità su un display a 7 segmenti con Arduino RJT43
Questo progetto dimostra come costruire un sistema semplice ma efficace per visualizzare le letture di temperatura e umidità su un display a 7 segmenti utilizzando un Arduino. Questa configurazione è utile per diverse applicazioni in cui il monitoraggio delle condizioni ambientali è cruciale. Il progetto combina il sensore di temperatura e umidità DHT22 con il display a 7 segmenti TM1637, fornendo un'uscita chiara e facilmente leggibile.


Idee per progetti:
- Monitoraggio dell'ambiente domestico per un comfort ottimale.
- Monitoraggio delle condizioni in una serra o in un terrario.
- Costruire una stazione meteorologica di base.
- Visualizzazione della temperatura e dell'umidità in un progetto di registrazione dati.
Hardware/Componenti
Per costruire questo progetto, avrai bisogno dei seguenti componenti:
- Arduino Uno (o scheda compatibile)
- Sensore di temperatura e umidità DHT22
- Display a 7 segmenti TM1637
- Fili di collegamento
- Breadboard (opzionale, ma consigliata)
Guida al cablaggio



Il cablaggio è semplice. Fai riferimento al video (al minuto 01:51) per una guida visiva. Le connessioni principali sono:
- Display TM1637:VCC a 5V, GND a GND, CLK al pin 2 di Arduino, DIO al pin 3 di Arduino (nel video a 02:00).
- Sensore DHT22:VCC a 5V, GND a GND, DATA al pin 9 dell'Arduino (nel video a 02:20). Nota che nel video viene usato il pin 8 dell'Arduino per fornire 5V al sensore (nel video a 02:41).
Spiegazione del codice
Il codice Arduino utilizza due librerie: TM1637Display per il display a 7 segmenti e DHT per il sensore DHT22. I parametri configurabili del codice si trovano principalmente all'inizio:
// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
#define DHTPIN 9 // DHT22 data pin
#define DHTTYPE DHT22 // DHT sensor type
Queste righe definiscono i pin Arduino collegati al display e al sensore DHT22. Potrebbe essere necessario modificarli se si usano pin diversi. IlgetTemp()La funzione (nel video a 07:03) è cruciale. Ti permette di recuperare diversi valori dal sensore DHT22 passando una stringa come argomento:
float getTemp(String req) {
// ... (Sensor reading code) ...
if(req =="c"){ return t; } // Celsius
else if(req =="f"){ return f; } // Fahrenheit
// ... (Other options for Kelvin, humidity, heat index) ...
}
Questa funzione semplifica la lettura di diversi dati dal sensore (Celsius, Fahrenheit, umidità, indice di calore). Il ciclo principale usa questa funzione per recuperare i dati e visualizzarli sul display a 7 segmenti, arrotondando i valori in virgola mobile a interi per la visualizzazione (nel video a 07:14).
Progetto/Dimostrazione dal vivo
Il video (al minuto 00:32) mostra una dimostrazione dal vivo del progetto. Il display a 7 segmenti mostra chiaramente la temperatura in gradi Fahrenheit. Il video mostra anche come il codice possa essere modificato per visualizzare i valori in gradi Celsius, in Kelvin, l'umidità e l'indice di calore (al minuto 08:21).
Capitoli
- [00:06] Introduzione e panoramica del progetto
- [00:53] Primi passi e panoramica dei componenti
- [01:51] Cablaggio del display TM1637
- [02:20] Cablaggio del sensore DHT22
- [03:14] Spiegazione del codice: Configurazione TM1637
- [04:00] Spiegazione del codice: Configurazione DHT22
- [05:54] Spiegazione del codice: ciclo principale e funzione di visualizzazione
- [07:03] Spiegazione del codice: Funzione getTemp()
- [08:21] Dimostrazione e diverse opzioni di output
/*
* Original code from TM1637 https://github.com/avishorp/TM1637
* Original code and library for DHT22 https://github.com/adafruit/DHT-sensor-library
* Modified for Robojax video on January 7, 2018
* by Ahmad Shamshiri, in Ajax, Ontario, Canada
* Watch the video for this code https://youtu.be/z_FvRm6Te78
* Other Arduino library and videos https://robojax.com
* Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in a structured course with all material, wiring diagrams, and libraries
all in one place.
If you found this tutorial helpful, please support me so I can continue creating
content like this.
or make a donation using PayPal http://robojax.com/L/?id=64
* * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// ****** Start of TM1637 Display code
#include <Arduino.h>
#include <TM1637Display.h>
// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
// The amount of time (in milliseconds) between tests
#define TEST_DELAY 1000
TM1637Display display(CLK, DIO);
// ****** end of TM1637 Display code
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
// ****** Start of DHT code
#include "DHT.h"
#define DHTPIN 9 // what digital pin we're connected to
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
// ********** end of DHT22 code
void setup()
{
Serial.begin(9600);
Serial.println("DHT22 Robojax Test with Display");
pinMode(8,OUTPUT);
digitalWrite(8,HIGH);// gives 5v for DHT22
dht.begin();
}
void loop()
{
delay(TEST_DELAY);// wait
// **** TM1637 code start
display.setBrightness(0x0f);// set brightness
uint8_t data[] = { 0x0, 0x0, 0x0, 0x0 };// clear display values
display.setSegments(data);//clear display
// **** TM1637 code end
// Robojax.com test video
Serial.println(getTemp("h"));
int temp = round(getTemp("h"));
display.showNumberDec(temp, false, 3,1);
}// loop end
/*
* getTemp(String req)
* returns the temperature related parameters
* req is string request
* This code can display temperature in:
* getTemp("c") is used to get Celsius
* getTemp("f") is used to get Fahrenheit
* getTemp("k") is used for Kelvin
* getTemp("hif") is used to get Fahrenheit
* getTemp("hic") is used to get Celsius
* getTemp("h") is used to get humidity
* written by Ahmad Shamshiri for Robojax.com on January 7, 2018
* in Ajax, Ontario, Canada
*/
float getTemp(String req)
{
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return 0.0; //return 0.0 instead of nothing.
}
// Compute heat index in Kelvin
float k = t + 273.15;
if(req =="c"){
return t;//return Celsius
}else if(req =="f"){
return f;// return Fahrenheit
}else if(req =="h"){
return h;// return humidity
}else if(req =="hif"){
return hif;// return heat index in Fahrenheit
}else if(req =="hic"){
return hic;// return heat index in Celsius
}else if(req =="k"){
return k;// return temperature in Kelvin
}else{
return 0.000;// if no request found, return 0.000
}
}
Risorse e riferimenti
Nessuna risorsa ancora.
File📁
Librerie Arduino (zip)
-
Modulo PCB DHT22 rosso
DHT22-module-red.fzpz0.01 MB
File Fritzing
-
Sensore di umidità e temperatura DHT22
DHT22 Humidity and Temperature Sensor.fzpz0.01 MB -
Modulo PCB DHT22 rosso
DHT22-module-red.fzpz0.01 MB -
Modulo DHT22 bianco
DHT22-module-white.fzpz0.01 MB -
Modulo a sette segmenti TM1637
TM1637.fzpz0.01 MB -
Sensore di Umidità e Temperatura DHT11 (3 pin)
DHT11 Humitidy and Temperature Sensor (3 pins).fzpz0.20 MB
Manuale dell'utente
-
Manuale dell'utente del sensore di temperatura e umidità DHT22
robojax-DHT22_manual.pdf0.36 MB