Error message here!

Hide Error message here!


Error message here!


Hide Error message here!


Error message here!



Introduction to small white hands of esp8266 nodemocu (Practical chapter) take soil humidity and DHT sensor as an example to explain reading sensor values

drip coffee 2021-07-26 11:24:39 阅读数:5 评论数:0 点赞数:0 收藏数:0

The Internet of things makes the physical and digital world in the real world more closely connected than ever before .NodeMCU As one of the important equipment , One of its functions is to connect with sensors to realize the interconnection and communication of all things . This article about NodeMCU The practical operation of this paper is based on soil moisture sensor and DHT For example, sensors , How to use ESP8266 Get the value of the sensor and the corresponding example code . We chose these two sensors as examples to explain , Because the signal pin of the output value of the soil sensor outputs an analog signal , and DHT The signal pin of the sensor outputs a digital signal , In the process of use, it is also necessary to import and store ( You can also not use the library , The code is relatively complex ). When you understand this article ,NodeMCU Communication with most other sensors is also easy .


One 、 Equipment and environment required for the experiment

Two 、 Connection mode and sample code

3、 ... and 、 Extension and precautions


One 、 Equipment and environment required for the experiment

( One )NodeMCU ESP8266




First you need to understand ESP8266 NodeMCU What is it and the basic operating environment of the device , If you haven't touched it , Please check it first NodeMCU Little white hands, get started . Best choice ESP-12E model , About different models NodeMCU Look at the difference between To solve the puzzle The first part of .

stay NodeMCU The pin diagram of contains some GPIO( General input output general purpose Input Output) Pin , Through them , Can achieve NodeMCU Communication with sensors .GPIO Is a digital pin of an integrated circuit , It can be used as input or output of interface equipment .  If we want to control LED brightness , Motor status, etc , Then we need to configure it to output . Anyway , If we want to read the state of the switch , Sensor data, etc , So we're going to GPIO Configure as input .  Obviously , In this tutorial , We need to configure GPIO For input , Note that in the code , To configure GPIO For input , Related codes pinMode(GPIO, INPUT) Write but not write , Such as configuration GPIO For export , You must use pinMode(GPIO, OUTPUT) explain .







NodeMCU ESP8266 Pin diagram of ( Picture source





( Two ) Soil sensors YL-69

The soil sensor is used to obtain soil moisture parameters , That is, the percentage of water in the soil . Common soil sensors fall into two categories , Resistive and capacitive soil moisture sensors . Their principles are similar , They are used to measure the volume content of water in soil , And input with sensor GPIO(0-1024) The voltage value of indicates the amount of moisture , The lower the voltage value , It shows that there is much water , The wetter the soil ; The higher the voltage value , Indicates poor conductivity , The drier the soil . For detailed knowledge of soil sensors, please see How to get the value of soil sensor with raspberry pie The soil sensor section in . In this tutorial , Use any soil sensor , The price is low .

( 3、 ... and )DHT11 sensor

Used to obtain temperature and humidity data in the air . Detailed about DHT11 Please check the knowledge of How to get... With raspberry pie DHT The value of the sensor .







( Four )Arduino IDE



Two 、 Connection mode and sample code

  stay Arduino IDE Make the following settings in : Development board :NodeMCU 1.0(ESP-12E Module); Upload speed :115200( Consistent with the code ); port : The port to which the board is connected


( One ) Soil sensors and NodeMCU Communication for

(1) Connection mode






(2) Sample code :

#define soilPin A0
// Store intermediate value
int soilValue;
// Define soil moisture
int soilMoisture;
void setup()
// This code can be written or not
//pinMode(soilPin, INPUT);
void loop()
// Read the voltage value on the soil Sensor
soilValue = analogRead(soilPin);
// Set the voltage value according to [0,1023] Mapping to [100,0]
soilMoisture = map(soilValue,0,1023,100,0);
// Output the value every second
Serial.print("soilMositure: ");

(3) Output results




 ( Two )DHT11 And NodeMCU Communication for

(1) Connection mode




(2) Download library . Open the library manager , Search for DHT, download DHT sensor libraray.


 (3) Code implementation

#include "DHT.h"
#define DHTPIN D4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.

void setup() {
void loop() {
// Wait a few seconds between measurements.
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// If one of the temperature and humidity is empty , The output “ a = Failed to get value ”
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
// Output temperature 
// Output humidity 


(4) Output results


  3、 ... and 、 Extension and precautions

All apply to NodeMCU The code can be used directly for Arduino, Change the corresponding parameters and pin names .

NodeMCU Only one analog pin , The value of only one sensor that outputs an analog signal can be obtained at the same time , This is its limitation .


Copyright statement
In this paper,the author:[drip coffee],Reprint please bring the original link, thank you