DHT12 humidity temperature sensor and ESP8266 example

The DHt12 is an upgraded version of the classic DHT11 humidity temperature sensor, it is fully downward compatible, more precise and adds an I2C interface.


compact size
low power consumption
low voltage operation
Standard I2C and 1-wire interface.

Sensing range
Temperature: -20 ~ +60 C
Humidity: 20-95 RH
Resolution: 0.1%RH
Repeat: -+ 1%RH
Precision 25C @ -+5RH
Resolution: 0.1C
Repeat: -+0.2C
Precision: 25C @ -+0.5C
Power: DC 2.7-5.5V
Normal current 1mA
Standby current 60uA
Sample cycle: > 2 seconds

Pin interface: 1. VDD 2. SDA 3. GND 4. SCL (connect to GND when use as 1-wire)


Putting the information above together you can see this is an I2C device and although the spec says 3.1 to 5v, I played safe and used 3.3v rather than the 5v Wemos connection

wemos and dht12

wemos and dht12


This is from https://github.com/xreef/DHT12_sensor_library , you will need to download and add the library. this is the default example


#include "Arduino.h"
#include <DHT12.h>
// Set dht12 i2c comunication on default Wire pin
DHT12 dht12;
void setup()
// Start sensor handshake
int timeSinceLastRead = 0;
void loop()
// Report every 2 seconds.
if(timeSinceLastRead > 2000) {
// Reading temperature or humidity takes about 250 milliseconds!
// Read temperature as Celsius (the default)
float t12 = dht12.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f12 = dht12.readTemperature(true);
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h12 = dht12.readHumidity();
bool dht12Read = true;
// Check if any reads failed and exit early (to try again).
if (isnan(h12) || isnan(t12) || isnan(f12)) {
Serial.println("Failed to read from DHT12 sensor!");
dht12Read = false;
if (dht12Read){
// Compute heat index in Fahrenheit (the default)
float hif12 = dht12.computeHeatIndex(f12, h12);
// Compute heat index in Celsius (isFahreheit = false)
float hic12 = dht12.computeHeatIndex(t12, h12, false);
// Compute dew point in Fahrenheit (the default)
float dpf12 = dht12.dewPoint(f12, h12);
// Compute dew point in Celsius (isFahreheit = false)
float dpc12 = dht12.dewPoint(t12, h12, false);
Serial.print("DHT12=> Humidity: ");
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(" *C ");
Serial.print(" *F\t");
Serial.print(" Heat index: ");
Serial.print(" *C ");
Serial.print(" *F");
Serial.print(" Dew point: ");
Serial.print(" *C ");
Serial.println(" *F");
timeSinceLastRead = 0;
timeSinceLastRead += 100;




Open that serial monitor and all going well you should see something like

DHT12=> Humidity: 33.70 % Temperature: 20.20 *C 68.36 *F Heat index: 19.16 *C 66.48 *F Dew point: 3.60 *C 38.48 *F
DHT12=> Humidity: 39.70 % Temperature: 20.60 *C 69.08 *F Heat index: 19.75 *C 67.55 *F Dew point: 6.34 *C 43.41 *F
DHT12=> Humidity: 44.60 % Temperature: 21.20 *C 70.16 *F Heat index: 20.54 *C 68.97 *F Dew point: 8.64 *C 47.55 *F
DHT12=> Humidity: 47.80 % Temperature: 21.80 *C 71.24 *F Heat index: 21.28 *C 70.31 *F Dew point: 10.25 *C 50.45 *F
DHT12=> Humidity: 48.90 % Temperature: 22.40 *C 72.32 *F Heat index: 21.97 *C 71.55 *F Dew point: 11.16 *C 52.08 *F
DHT12=> Humidity: 53.90 % Temperature: 22.90 *C 73.22 *F Heat index: 22.65 *C 72.78 *F Dew point: 13.14 *C 55.66 *F
DHT12=> Humidity: 55.10 % Temperature: 23.60 *C 74.48 *F Heat index: 23.45 *C 74.22 *F Dew point: 14.15 *C 57.46 *F
DHT12=> Humidity: 54.90 % Temperature: 23.90 *C 75.02 *F Heat index: 23.78 *C 74.80 *F Dew point: 14.37 *C 57.86 *F
DHT12=> Humidity: 45.30 % Temperature: 24.40 *C 75.92 *F Heat index: 24.08 *C 75.34 *F Dew point: 11.80 *C 53.24 *F
DHT12=> Humidity: 40.60 % Temperature: 24.60 *C 76.28 *F Heat index: 24.18 *C 75.52 *F Dew point: 10.28 *C 50.50 *F
DHT12=> Humidity: 37.10 % Temperature: 24.80 *C 76.64 *F Heat index: 24.30 *C 75.75 *F Dew point: 9.09 *C 48.35 *F



You can pick up one of these for under $1

1pcs DHT-12 DHT12 sensor Digital output Temperature and Humidity Sensor high quality

ESP8266 and DS18b20 sensor readings displayed on a web server

In this example we will connect a Wemos compatible DS18B20 shield to a Wemos Mini and display a page on a web server. This is a combination of our two examples that we have already created and uses 2 shields and a Wemos mini

DS18b20 example


Web server example



You will need to add the one wire library via the IDE -> library manager.  Since I am using the shield and its set to D2 for the sensor that is what is set in the code, if you prefer to use a different standalone sensor and a different pin change the following line

OneWire ds(D2);

If copying and pasting is too much I’ve know created a githib repo  at – https://github.com/getelectronics/ESP8266learning/tree/master/wemos_ds18b20_server

#include <ESP8266WiFi.h>
#include <OneWire.h>
const char* ssid = "username";
const char* password = "password";
OneWire  ds(D2);
WiFiServer server(80);
void setup() 
  // Connect to WiFi network
  Serial.print("Connecting to ");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  Serial.println("WiFi connected");
  // Start the server
  Serial.println("Server started");
  // Print the IP address
  Serial.print("Use this URL : ");
void loop() 
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
  // Wait until the client sends some data
  Serial.println("new client");
  // Read the first line of the request
  String request = client.readStringUntil('\r');
  //now the DS18b20
  if ( !ds.search(addr)) 
  if (OneWire::crc8(addr, 7) != addr[7]) 
      Serial.println("CRC is not valid!");
    // the first ROM byte indicates which chip
  switch (addr[0]) 
    case 0x10:
      type_s = 1;
    case 0x28:
      type_s = 0;
    case 0x22:
      type_s = 0;
      Serial.println("Device is not a DS18x20 family device.");
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end  
  present = ds.reset();
  ds.write(0xBE);         // Read Scratchpad
  for ( i = 0; i < 9; i++) 
    data[i] = ds.read();
  // Convert the data to actual temperature
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) 
      raw = (raw & 0xFFF0) + 12 - data[6];
    byte cfg = (data[4] & 0x60);
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  celsius = (float)raw / 16.0;
  fahrenheit = celsius * 1.8 + 32.0;
  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<meta http-equiv=\"refresh\" content=\"5\">"); //refresh every 5 seconds
  client.println("<H3>DS18b20 temperature example</h3>");
  client.print("Temperature in Celsius = ");
  client.print("Temperature in Celsius = ");
  Serial.println("Client disconnected");



Again open the serial monitor to find your IP address

wemos ip address

wemos ip address


Now navigate to the IP address in the browser and you should see something like this which refreshes every 5 seconds

ds18b20 web page

ds18b20 web page

ESP8266 and Stepper motor example

In this example we will show a basic stepper motor example, we use a driver board that can be bought on many sites which basically comprises of a ULN2003 IC and a few other components, you can use either 5v or 12v, the motor that comes quite often with this board is a 5v type, you can see it further down the page.

Here is the board that I used for this example and here are the connections

Wemos D5 -> IN1
Wemos D6 -> IN2
Wemos D7 ->IN3
Wemos D8 -> IN4


Diameter: 28mm; Voltage: 5V; Step angles: 5.625 x 1/64; Speed reduction ratio: 1/64; Power consumption: About 5V / 63mA; Load pull in frequency: >500Hz; Load pull out frequency: > 900Hz; 5-wire 4-phase can be driven with an ordinary ULN2003A chip

I powered the module externally

There are other similar boards where the motor is separate from the board but its the same stepper motor and it uses a ULN2003



The code uses the built in stepper library, this is a fairly basic example


#include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// initialize the stepper library on pins D5 to D8
Stepper myStepper(stepsPerRevolution, D5,D6,D7,D8);
int stepCount = 0; // number of steps the motor has taken
void setup() {
void loop() {
// step one step:




5V 4-phase Stepper Motor+ Driver Board ULN2003

ESP8266 and L9110 fan module example

In this example we connect an ESp8266  to a dual L9110 fan module. This is a commonly found, basic low cost module which consists of an L9110 chip and a small motor attached. You need 4 connections between the arduino and the module. VCC, GND , INA and INB. You should use an external power source for Vcc and Gnd

L9110 The ASIC device control and drive motor design two-channel push-pull power amplifier discrete circuits integrated into a monolithic IC, peripheral devices and reduce the cost, improve the reliability of the whole. This chip has two TTL / CMOS compatible with the level of the input, with good resistance; two output terminals can directly forward and reverse movement of the drive motor, it has a large current driving capability, each channel through 750 ~ 800mA of continuous current, peak current capability up to 1.5 ~ 2.0A; while it has a low output saturation voltage; built-in clamp diode reverse the impact of the current release inductive load it in the drive relays, DC motors, stepper motor or switch power tube use on safe and reliable. L9110 is widely used in toy car motor drives, stepper motor drive and switching power tube circuit.

  • Motor Voltage: 2.5 ~12V
  • Motor channels: 2
  • Max Continuous Current per Channel: 800mA
  • Size: 31mm x 22mm x 12mm

this is a picture of a typical module

Lets look at how to connect the ESP8266 to the module






No libraries needed in this example, fairly basic example this one, upload the sketch and the fan will just run in one direction at one speed. You could use analogWrite and set a value between 0 and 255, if you have one of the dual modules you can have 2 motors fitted and technically move in any direction


int INA = D3; 
int INB = D4;
void setup() 
void loop() 




L9110 Fan Module for Arduino