[Microbit-Python] Microbit DHt11 temp and humidity sensor

Jim Mussared jim at groklearning.com
Sat Jul 8 07:51:48 EDT 2017


OK you piqued my interest and I remembered I had a humidity sensor in
my drawer which fortunately is a (very similar) DHT22.

The way the sensor works, like a 1-wire or 12C device, is both the
micro:bit and the sensor share the same line which is pulled-up to
3.3V. Either device can pull it low to transmit data.

To initiate a read, the micro:bit needs to pull it low momentarily,
then the sensor will do it 40 times. A short pulse (!30us) is a '0'
and a long pulse (70us) is a '1'.

There's no way you can measure those pulses using read_digital etc on
a micro:bit, but there is a very easy way to sample a pin a high speed
using SPI.

So we wire the temperature sensor's data line to two pins - one in SPI
mode for reading, the other in digital mode to do the initial pulse.
The complete wiring is GND=GND, Vcc=3V, Data=Pin0,Pin1, and a 10k
resistor between data and 3V. You could use any pair of pins though
instead of Pin0, Pin1.

Basically the way this code works is it toggles pin1 low, then
switches it back to input mode (high impedance). Then it reads at
250000 bits/s on the SPI line, which means we get approximately 4-5
bits for a '0' and 11-12 bits for a '1'. The rest of the code just
unpacks this into an array of 5 bytes that contain two bytes of
humidity data, two bytes of temperature, and one byte checksum.

On your DHT11 it only has one byte each for the humidity and
temperature, I think the changes you'll need are:
temp = result[2]
humidity = result[0]

I used https://github.com/adafruit/DHT-sensor-library/blob/master/DHT.cpp
as a reference.

Let me know if that works for you!

Jim

On 8 July 2017 at 21:01, Jim Mussared <jim at groklearning.com> wrote:
> I just realized you had the sensor details in the subject line - DHT11.
>
> It looks a lot like a 1-wire device -- datasheet at
> https://cdn-shop.adafruit.com/datasheets/DHT11-chinese.pdf (and
> similar to https://cdn-shop.adafruit.com/datasheets/DHT22.pdf). I
> think you're going to have a hard time doing this from MicroPython on
> the micro:bit because you don't have microsecond-level timing.
>
> I recommend trying to find an I2C sensor instead - that'll be much
> easier to use. Or if you want to make it really easy, some sort of
> analog interface sensor (i.e. where you read a voltage from it).
>
>
> On 8 July 2017 at 17:10, Jim Mussared <jim at groklearning.com> wrote:
>> Hi,
>>
>> The micro:bit doesn't have a humidity sensor built-in, so you'll need
>> to use some sort of external device. I've seen a few I2C sensors, but
>> it's also pretty common to see Dallas 1-wire used in these sorts of
>> sensors. You'll find I2C a lot easier on the micro:bit.
>>
>> So if you could let us know which sensor you're using then we can give
>> you some advice.
>>
>> Jim
>>
>>
>> On 8 July 2017 at 12:55, sasithaweerasekera via Microbit
>> <microbit at python.org> wrote:
>>> Hi I want to measure and display the humidity by the microbit using microbit
>>> and the leds of the microbit, do you know the coding, because I can't find
>>> any except for bit banging which is a concept I am not familiar with, if
>>> there is a library it would help a lot
>>> Thanking you in advance for ur time;
>>> Sasitha
>>>
>>>
>>>
>>> Sent from my Samsung Galaxy smartphone.
>>>
>>> _______________________________________________
>>> Microbit mailing list
>>> Microbit at python.org
>>> https://mail.python.org/mailman/listinfo/microbit
>>>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dht22.py
Type: text/x-python
Size: 1276 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/microbit/attachments/20170708/aed3f821/attachment.py>


More information about the Microbit mailing list