Fritzing Dht22

0 views
Skip to first unread message

James Talbot

unread,
Aug 3, 2024, 2:06:28 PM8/3/24
to ubewunin

As you can see from the previous list, each step of this tutorial is independent from the others. So, if, for example, you are looking for an easy way to get your DS3231 module working, this tutorial is for you.

The first step is to download and install the DHT22 sensor library.
Download the attached DHT.zip file. In this zip file you will find two files which must be in the \DHT subfolder. If you are on Windows, install the library by moving the \DHT folder inside the folder:

So the two files dht.cpp and dht.h must be in a folder called DHT and there must be no other subfolders. You can get more information on installing the libraries in the Arduino IDE by clicking this link: -v1/tutorials/installing-libraries

In fact we would need this part of code only if we were using sensors other than the DHT22.
The second difference is the change of the Arduino pin connected to the data pin of the DHT22. We chose pin 9 instead of pin 6 (chosen by default) because pin 6 is used by the LCD display.
Upload the sketch to the Arduino and open the IDE serial monitor (just press the magnifying glass button at the top right of the IDE). The serial monitor should show lines of output as in the following box and in the following screenshot:

The best way to get the date and time is to use an external module (a so-called RTC or Real Time Clock). In our project we will use a well-known and well-tested module, the DS3231 with a 3.6V rechargeable battery (model lir2032). In this way our Arduino will be able to keep the date and time even if it is turned off.

Now, download the attached ds3231.ino file (remove the .txt extension if present) and double-click it. The arduino IDE will open and ask you to save the new sketch in a folder with the same name. Save the sketch wherever you like.
Now go to line 19 and uncomment it by deleting the two slashes (//) at the beginning:

We no longer need these lines because we only use them when we need to adjust the date and time on form DS3231. For the same reason, we also delete lines 17 to 19 found in the setup function:

In the ds3231_bis.ino sketch add the DST22 library, define the pins to use and also define the variables used to read and write the detected temperature and humidity values. Here we show how the new code should start:

In order not to mess things up too much, we have created the dht22 function which contains the lines belonging to the setup function of our dht22.ino sketch. Thanks to this, our loop and setup functions look much cleaner and tidier.

The most important functions are Wire.begin() and Serial.begin(). These functions initialize the DS3231 and the serial monitor respectively. The other lines print other information about DHT22 that is not strictly necessary:

Now is the time to connect the LCD display. You will need a 10 kΩ linear potentiometer and a 16-pin connector.
Once the display is connected as shown in the figure, connect the +5V line to a pin of the red stripe of the breadboard and the GND pin to a pin of the blue stripe. Also connect the upper and lower red strips together, then connect the upper and lower blue strips together (as shown in the figure above). Remember that by convention red wires are used for the +5V line and black wires for the GND line.
Now connect the light for the backlighting of the display and the potentiometer for the contrast: place the potentiometer as shown in the figure and connect its central pin to the V0 pin of the display while the two lateral pins you will connect them to GND.
Connect pin 16 of the display to GND and pin 15 to +5V (these two pins are connected internally in the display to a backlight light). If you connect the Arduino to power the backlight should turn on.
Now turn off the Arduino and continue with the remaining connections.

At this point we can load the attached HelloWorld.ino sketch and test the circuit (remove the .txt extension if present). If everything is connected correctly you should read a message on the display.

Since the backlighting of the display draws a certain current, it may be useful to check that it is switched on, especially if you are planning to run the thermo-hygrometer on a battery. In our solution, you only need to press a button to turn on the backlight. The display will automatically turn off after about 8 seconds. We achieved this by controlling the led responsible for the backlighting with a PWM signal generated by Arduino. Since the PWM output cannot supply the current needed to control this led, we cannot connect it directly.

We solve the problem using a 2N3904 transistor and a 10 kΩ resistor, as shown in the Fritzing schematic below. The current required to turn on the LED will flow between the collector and emitter of the transistor rather than through the PWM output.

which are required to set the button status and the display lighting time, respectively.
In the setup function we have to define the pin used as input and the one used as output of the PWM signal:

Now, add the library required by the LCD display and declare the two variables and constants used to turn it on and off. Next you need to declare two byte arrays. These two arrays, called drop and temp, contain the icons shown on the display.
To initialize the display we use the LiquidCrystal() function which contains, within its brackets, the pins used by the display.

The first two lines of the setup function initialize the two pins BUTTON and LUMIN. These are used for display backlight control. The third line initializes the DS3231. The fourth and fifth create the two icons.
The last one initializes the display.

In the loop function we find the instructions necessary to control the backlighting of the display. We do not repeat here how they work because we addressed the problem in a previous step.
The last two statements call the functions that print date and time and temperature and humidity. The only difference from the code of the previous step is that the counter (which was initialized to 1000 before) is initialized to 5 this time because we have now inserted a 2 second delay.

If you want to see me and hear me explain everything in this tutorial, I encourage you to purchase the video version from my Courses site. Not only it will make for a better learning experience, but you'll also be supporting my effort in keeping my blog updated with relevant content. Thank you!

The DHT22 sensor is a low cost device that can obtain temperature and humidity readings. It uses a custom data transmission protocol that is very friendly to microcontrollers such as the Arduino and of course, the ESP8266. There are versions of this sensor with three and four pins, but in the four pin variant only three pins are used. The pins are connected to voltage, ground, and an available GPIO pin. One limitation these devices have is that to get more accurate results, you have to wait at least two seconds between readings.

In the three-pin versions of this device the pins are VCC, OUT and GND from left to right and looking at it from the front. The OUT pin is the data output pin. You should verify this is the order for your own device before you connect it. You may see the voltage pin labeled as VCC or maybe +. The GND pin is sometimes labeled -. If you have a four-pin version, then the order is the same, but you have to skip the third pin which does not have any use.

To connect this sensor I'm taking advantage of the power strips in the breadboard. The only connection that goes directly into the microcontroller is the one for the data pin or OUT, which I connected to pin D2, also known as GPIO4. I've chosen the D2 pin randomly from the available ones. I have used D0 and D4 as output pins to control the on-board LEDs, which I may want to still have access to. If I were to connect the sensor on one of these pins, I would not be able to use the corresponding LED because the pin is going to be configured as an input pin to receive the data from the sensor, but I could have also picked D1, D3, or any other available D pin. You can see in the diagram that I left the external reset button from the previous chapter connected, since it should not interfere with the sensor in any way.

MicroPython comes with a few drivers for commonly used devices. The DHT22 sensor is luckily supported, so all I needed to do is import the corresponding class and use it. The dht package contains two temperature sensor drivers, one for the DHT11 and another for the DHT22. The only argument that needs to be provided to the DHT22() class is a machine.Pin() instance for the GPIO pin connected to the data pin of the sensor. In my case I have it connected to D2, which is GPIO4.

Once you have the object created, a call to measure() activates the sensor and performs a reading of both temperature and humidity. This method sends a "start" signal to the sensor through the data pin, and this wakes the sensor from its standard dormant state. The sensor then takes measurements of temperature and humidity and returns them, also through the data pin. As soon as this process completes the sensor goes back to sleep. If you are curious about the communication protocol used in the exchanges between the microcontroller and the sensor, you can read about it here.

After a measurement has been taken, you can use the temperature() and humidity() methods to retrieve the results. The temperature is returned in the Celsius scale, and the humidity is returned as a percentage, what is usually known as relative humidity.

If you live in the United States or one of the few other countries in the world where the Fahrenheit scale is used for temperatures, then the Celsius results are not going to mean much to you. In that case you can take the Celsius measurement and convert it to Fahrenheit with the following formula:

The above calculation takes the celsius temperature and multiplies it by 1.8 (or by the fraction 9/5, which is the same thing) and then adds 32. The result is the same temperature, but expressed in the Fahrenheit scale.

c80f0f1006
Reply all
Reply to author
Forward
0 new messages