element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Legacy Personal Blogs MicroPython #4: Interfacing Sensors
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: navadeepganeshu
  • Date Created: 8 May 2020 1:35 PM Date Created
  • Views 878 views
  • Likes 1 like
  • Comments 0 comments
  • python 3.7
  • powerful
  • micropython
  • esp8266
  • ideas_worth_sharing
Related
Recommended

MicroPython #4: Interfacing Sensors

navadeepganeshu
navadeepganeshu
8 May 2020

MicroPython

 

Interfacing Sensors

 

image
DHT-11 Sensor interphase with NodeMCU

 

At the end of this, you will be able to interphase DHT11 Temperature Sensor with ESP 8266 NodeMCU bare-metal and also by attaching external LED's. The whole process will be done by MicroPython programming. So, Let's begin with !

 


 

Note: You must have your MicroPython firmware pre-flashed onto your board before beginning with this. If not, follow up these before getting back here.

 

MicroPython #1: Introduction and Gearing Up
MicroPython #3: PWM, LED Fade

 

MicroPython #2: Triggering I/O's

 

 

 

Introduction to DHT 11 and Sensor Interphase

 

The DHT11 is a basic, ultra low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data pin. Here, we'll operate it on Analog mode.

There is a dht module that comes with the MicroPython firmware by default. So, it is easy to get temperature and humidity.

 

DHT-11 Datasheet

 

Implementation on ESP8266 (NodeMCU)

Using MicroPython Temperature data acquisition can be easily implemented and you will be able to read Temperature and Humidity data. Let's go through then !

 

 

The algorithm goes as follows :

 

 

> Import the Pin on the device to your program using 'from machine import Pin'.

 

 

> Call the time

 

With the help of time, you can turn LED on and off. Call time with 'from time import sleep' and 'sleep(<delay>)' is used to set delay later on.

 

 

> Now, Import the standard module for reading DHT sensor by 'import dht' and assign pin definition by 'dht.DHT11(PIN(14))'. DHT11/22 is an option and any ADC pin on bare-metal can be assigned for the same. (pin 14 here)

 

> Now, it's all about acquiring the data from dht. We have already imported and set all factors and made declarations. Call temperature as 'sensor.temperature()' and humidity as 'sensor.humidity()'

Putting these functions in while loop with try/except logic its all set to go.

 

 

Below is the MicroPython script for the same:
------------------------------

 

 

from machine import Pin
from time import sleep
import dht

sensor = dht.DHT11(Pin(14))

while True:
  try:
    sleep(2)
    sensor.measure()
    temp = print('Temp:',sensor.temperature(),'*C')
    hum = print('Humi:',sensor.humidity(),'%')
  except OSError as e:
    print('Failed to read sensor.')
------------------------------

 

 

 

Get this written and run the script. There there !

 

You will find logging of Temperature and Humidity data on the terminal with the delay being set.

This can be upgraded to the next level by setting a temperature triggered Buzzer or LED. Add a statement saying 'if (temperature>38): machine.Pin(2, OUT)' saying to turn LED on a certain threshold. In the below image, the green LED indicates humidity is above 90%  and red one says the temperature is above 37*C. That's something interesting !

 

image
DHT-11 Temperature/Humidity Alerting System


 

image

image

 

 

 

Result and Display:

 

 

image

 

 

Summary: All of the above proves the power of MicroPython and shows its simplicity compared to C++ / Embedded C. Now, we have Sensor interphased. Why not we use this to control Thermostats, Switches, Multiple sensor integration,............and what not ??

 


Just drop down your comments, troubles or any bugs you found on the way in reaching so far in the comment section below.
GitHub link for source codes: https://github.com/NavadeepGaneshU/CL3VERTRONICS

Hope you followed up things tight.

Spark something

Cheers !!!

 

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube