Implementing DHT11 Temperature Sensor Parameters with C# on ESP32 Microcontroller[1]
Welcome to C# enthusiasts, in this article we will use the C# nanoframework to develop a program to read temperature and humidity from the DHT11 sensor on the ESP32 microcontroller.
To implement this, we need to prepare and configure the environment for ESP32. You can refer to a previous article on setting up ESP32: How to write microcontroller programs using C# – hejiale010426 – Blog Garden (cnblogs.com)[2]
Then purchase a DHT11 sensor (available for a few dollars on online shopping sites) as shown in the image.
Now we can start the development.
The first step is to create a nanoframework project Demo.
Click on NuGet
Search for nanoFramework.Iot.Device.Dhtxx.Esp32 and install it into your project.
The version of the installed nanoFramework.Iot.Device.Dhtxx.Esp32 dependency must match the version of nanoFramework.CoreLibrary. Please copy the following code block.
using Iot.Device.DHTxx.Esp32;
using System.Diagnostics;
namespace DemoDHT11
{
public class Program
{
public static void Main()
{
// 12, 24 represent pin numbers
using (Dht11 dht = new Dht11(12, 14))
{
var temperature = dht.Temperature; // Get temperature
var humidity = dht.Humidity; // Get humidity percentage
if (dht.IsLastReadSuccessful) // Check if read was successful
{
Debug.WriteLine($"Temperature: {temperature.DegreesCelsius} °C, Humidity: {humidity.Percent} %");
}
else
{
Debug.WriteLine("Error reading DHT sensor");
}
}
}
}
Connect the wires as shown: 12 and 14 are out (data) external connections.
Select the device and run the program
Results: Temperature: 20.8 °C, Humidity: 64%
Thank you to those who love C# programming. I hope more people will enjoy C#, and even enjoy programming microcontrollers with C#.
References
Implementing DHT11 Temperature Sensor Parameters with C# on ESP32 Microcontroller: https://www.cnblogs.com/hejiale010426/p/15848574.html
[2]How to write microcontroller programs using C# – hejiale010426 – Blog Garden (cnblogs.com): https://www.cnblogs.com/hejiale010426/p/15798425.html