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
Summer of Green Tech Design Challenge
  • Challenges & Projects
  • Design Challenges
  • Summer of Green Tech Design Challenge
  • More
  • Cancel
Summer of Green Tech Design Challenge
Blog Cool Wave - Using the DHT20 in MicroPython on the Seeeduino XIAO
  • Blog
  • Forum
  • Documents
  • Leaderboard
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Summer of Green Tech Design Challenge to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Alistair
  • Date Created: 19 Sep 2023 11:11 AM Date Created
  • Views 1928 views
  • Likes 8 likes
  • Comments 6 comments
  • summer of green tech design challenge
  • Xiao
Related
Recommended

Cool Wave - Using the DHT20 in MicroPython on the Seeeduino XIAO

Alistair
Alistair
19 Sep 2023
Cool Wave - Using the DHT20 in MicroPython on the Seeeduino XIAO

One thing that is critical for the Cool Wave project is to know what temperature it is both instead and outside. I have a number of modules to test for this, but one I am confident that I will use is the Grove Temperature and Humidity Sensor with a DHT20 at the heart of it.

The DHT20 module has a lot of parallels with the DHT11 I have used many times before. The key differences are that it is more accurate, and it is communicated with the I²C protocol. The Grove module is effectively a DHT20 with voltage management built around it.

It is worth noting that the Seeeduino XIAO has a dedicated I²C controller on pins A4/D4 for SDA (GPIO 8) and A5/D5 for SCL (GPIO 9). This is great as talking to the HDT20 is not trivial.

First let's check that we can talk to the hardware using the I²C bus. Ideally I would use a Grove Base for Seeeduino XIAO to connect things up, but I don’t have one, so I am using jumper wires as photographed. Don’t judge me, but if you do judge me then make certain it is a comment with a good pun in it. :-)

A mass of wires connecting things together

SDA on the module goes to D4 in the Seeeduino XIAO, SCL goes to D5, GND to GND, and VCC to 3V3. The Grove module’s voltage regulator can use the 5V VCC instead of 3V3, but for initial testing we will do less damage if we mess up white using the 3.3V supply.

Now here is some test Python code that we can run through the Thonny IDE. It will search the I²C bus and list all the devices it can find. Note that we need to use the GPIO references in MicroPython and not the pin numbers.

from machine import Pin, I2C

i2c = I2C(0, scl=Pin(9), sda=Pin(8))

print('Scanning I2C bus.')
devices = i2c.scan() # this returns a list of devices

device_count = len(devices)

if device_count == 0:
    print('No i2c devices found.')
if device_count == 1:
    print('1 device found.')
else:
    print(device_count, 'device(s) found.')

for device in devices:
    print('Decimal address:', device, ", Hex address: ", hex(device))

When this is run we should see that we have one device with the address of 56, or 0x38 expressed in hexadecimal.

Now we need to get some data from the sensor and convert it to a temperature. The good news is that there are many libraries that will do the heavy lifting for us. I have used the library from https://github.com/flrrth/pico-dht20 but others are available. To use this library download https://github.com/flrrth/pico-dht20/blob/main/dht20/dht20.py and use Thonny to save it to the Seeeduino XIAO. After that is done the following code should run and continually read the current temperature and humidity.

from machine import Pin, I2C
from dht20 import DHT20

i2c = I2C(0, scl=Pin(9), sda=Pin(8))
    
dht20 = DHT20(0x38, i2c)

while True:
    measurements = dht20.measurements
    temper = measurements["t"]
    humidity = measurements["rh"]
    print("temper : " + str(temper))
    print("humidity : " + str(humidity))

And that is that done. Sadly we can only have one DHT20 on the I²C hardware bus as only one device ID is allowed pre bus, and we can not change the device ID of the DHT20, but I intend to use this for inside and a different sensor for outside so this is not an issue on this occasion. We should be able to use some other pins and implement I²C in software, but that is not needed on this occasion and what I lovingly call scope creep so let’s leave that test for another day.

  • Sign in to reply
Parents
  • beacon_dave
    beacon_dave over 2 years ago

    An I2C multiplexer could perhaps help you out down the line

    https://learn.adafruit.com/adafruit-tca9548a-1-to-8-i2c-multiplexer-breakout/overview

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Alistair
    Alistair over 2 years ago in reply to beacon_dave

    Good pointer. That is really useful. I did not know that existed. I don't think I will need it for this, but I have already ordered some ready for when I do need them.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • beacon_dave
    beacon_dave over 2 years ago in reply to Alistair

    There are also I2C address translators if you need further control:

    https://www.analog.com/en/products/ltc4316.html

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • beacon_dave
    beacon_dave over 2 years ago in reply to Alistair

    There are also I2C address translators if you need further control:

    https://www.analog.com/en/products/ltc4316.html

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
No Data
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