Introduction: Temperature and Humidity Sensor With MicroPython Using Bharat Pi Board

About: Bharat Pi is an IoT prototyping platform for students, innovators, startups and developers. A simplified board with an all in one compute network and storage on a single board to build rapid prototypes, colleg…

In this blog, you’ll learn how to use the temperature and humidity Sensor with the ESP32 to get the readings using Micropython firmware. This tutorial covers how to wire the sensor to the ESP32 boards and provides a simple Micropython script to get the temperature and humidity readings.

In this project, we are using Bharat Pi which has both ESP32 microcontroller and a SimCom A7672S 4G/LTE module. Bharat Pi is an IoT prototyping board for students, innovators, startups, and developers. A simplified board with an all-in-one compute, network and storage on a single board to build rapid prototypes.

In the rapidly advancing world of the Internet of Things (IoT) and home automation, understanding and controlling the environment around us has become essential. Temperature and humidity are critical parameters that influence our comfort, health, and the behaviour of various systems.

Supplies

Required Components:

To complete this tutorial you need the following parts:

1. DHT11 Temperature and Humidity Sensor

2. ESP32 (Bharat Pi board)

3. Jumper Wires

5. USB Cable

To follow this instruction you need an IDE to write and upload the code to your board, you can use Thonny IDE or uPycraft IDE. you also need Micropython firmware installed in your ESP32 boards.

If you don't have the setup means then you can check the README file of installing and setup of uPyCraft in our Bharat Pi github page you can find the link below,

https://github.com/Bharat-Pi/MicroPython/blob/main/README.md

Step 1: Schematic – ESP32 With DHT11 Sensor


In this section, we will see how to connect DHT 11 sensor with ESP32. The DHT 11 sensor will be connected with the ESP32 board with its 3 pins (GND, VCC, DATA).

Follow the schematic diagram for the ESP modules and connect them accordingly. Connect the ESP32 device with the DHT 11 sensor as shown in the schematic diagram above:

DHT 11 Sensor Pinout

VCC - Power Supply

DATA - GPIO

GND - GND

Wire the DHT 11 sensor to the ESP32 as shown in the following schematic diagram. We’re connecting the VCC pin to 3V/5V, and the DATA pin to GPIO 23, but you can use any other suitable pins, then GND to GND pin.

ESP32 Wiring With DHT 11 Sensor

DHT 11 Sensor - ESP32

VCC - 3V / 5V

DATA - GPIO 23

GND - GND

Step 2: DHT11 Sensor


What are Temperature and Humidity Sensors?

Temperature sensors, as the name suggests, measure the ambient temperature of the surrounding environment. These sensors can be classified into contact and non-contact types. Contact sensors, like thermocouples and thermistors, physically come in direct contact with the object to be measured. Non-contact sensors, such as infrared temperature sensors (IR sensors), measure temperature remotely by detecting the thermal radiation emitted by the object.

On the other hand, humidity sensors, or hygrometers, measure the moisture content in the air. They are essential in various applications, including weather forecasting, indoor climate control, industrial processes, and even agricultural practices.

Temperature Sensors:

  • Thermocouples work on the principle of the Seebeck effect, where two dissimilar metals produce a voltage proportional to the temperature difference between the hot and cold junctions.
  • Thermistors are resistors whose resistance changes with temperature. They are of two types: negative temperature coefficient (NTC) and positive temperature coefficient (PTC) thermistors.
  • Infrared temperature sensors detect the infrared energy emitted by an object and convert it into an electrical signal, which is then used to calculate the temperature.

Humidity Sensors:

  • Capacitive humidity sensors measure the change in capacitance of a capacitor due to the absorption of water molecules on its surface.
  • Resistive humidity sensors, also known as hygroscopic sensors, use a moisture-absorbing material whose resistance changes with humidity levels.
  • Thermal conductivity humidity sensors measure the cooling effect of water vapor on a heated sensing element.

Here’s the pinout of the DHT11 Sensor :

VCC

DATA

GND

Step 3: CODE (DHT 11 Sensor)

If you are using lcd with the DHT 11 means then add the lcd library before uploading the main code.

You can find the lcd libraries on Bharat-Pi github page :

https://github.com/Bharat-Pi/MicroPython/tree/main/Bharat_Pi_LCD_Display


CODE :



from machine import Pin
from time import sleep
import dht

sensor = dht.DHT11(Pin(23))

while True:

 sensor.measure()
 temp = sensor.temperature()
 hum = sensor.humidity()

 print('Temperature=', temp, 'C')
 print('Humidity=', hum, '%')

 sleep(3)


Step 4: Demonstartion

After uploading the code to your board,the temperature and humidity readings should be printed on the console.


Step 5: Applications

Weather Monitoring: Temperature and humidity sensors are essential components of weather stations, providing accurate data for forecasting and climatology studies.

Indoor Climate Control: In HVAC systems, these sensors ensure that indoor conditions are optimized for human comfort and energy efficiency.

Agriculture and Greenhouses: Monitoring temperature and humidity levels helps optimize crop growth and yield in controlled environments.

Food Storage and Transport: In the food industry, these sensors are used to maintain ideal conditions for storing and transporting perishable goods.

Healthcare: Temperature and humidity monitoring is crucial in healthcare settings to ensure a safe and comfortable environment for patients.

Home Automation: Smart homes use these sensors to adjust heating, cooling, and ventilation based on environmental conditions.