Re: DHT-22 and Beaglebone

3,915 views
Skip to first unread message

Dan Watts

unread,
Oct 15, 2012, 7:58:51 PM10/15/12
to beagl...@googlegroups.com
Hi,

You're right: the Adafruit code does look odd. 

As you say, to initiate communication with the DHT11/DHT22, you're supposed to pull the signal low for > 18 ms, then high, then wait for the signal to be pulled low again.  Their code appears to skip the pull-high part.  I can only assume that the Pi stops pulling the signal low when it switches to input mode.  

The only possible problem I can see in your code is that you might not be pulling the signal low for long enough: perhaps the usleep(2000) should be usleep(20000)? Also, trying to catch that 40 microsecond signal that begins the DHT22's response is tricky - that's probably why the Pi's code doesn't pull the line high explicitly.  Perhaps dropping the digitalWrite(HIGH) from your code would have the same effect?

I have my doubts about whether reading data from the DHT22 can work reliably from Linux code, given the tight timing involved.  Apparently the Adafruit code is managing to do it on the Pi, but there is a warning at the beginning of their tutorial saying it may not work, and perhaps the library they are using is more optimized than what is available for the Bone.

Personally, I wimped out and use a separate processor to read the DHT22.  I actually use a .Net Micro Framework processor for this (which surprisingly is able to handle the timings) and serial I/O, but something like the ATTiny2313 and I2C would be a better design.  

Dan.

On Sunday, October 14, 2012 6:26:51 PM UTC-4, Aaron Patterson wrote:
Hi, I was wondering if anyone has been able to get the DHT-22 humidity and temperature sensor to work with the Beagle Bone?

I have a DHT-22 humidity and temperature sensor:


I've been able to get it to work with the TI Launchpad, but had no such luck with the Beagle Bone.  I'm powering the sensor from VDD_3V3EXP, and I have a 5.6k resistor hooked up to the data wire (though I didn't seem to need this with the Launchpad).

I'm using this library for reading the GPIO pin:


I tried porting my code from the Launchpad to the BeagleBone, but I can't seem to read any values from the sensor.  I've verified that I can read values from the pin, but I'm not getting any values from the sensor.  Adafruit has a library for reading the sensor from a Raspberry Pi:


I also tried porting this code, but still can't get any information from the sensor.  Also, the Adafruit code confuses me because it sends a high signal then a low signal to initiate data transfer:


But the spec sheet for the sensor says you should send a low, then a high:


Here is a snippet of the code I'm using just to send the start signal:

  pinMode(P9_12, OUTPUT, 7, PULLUP);
  digitalWrite(P9_12, LOW);
  usleep(2000);
  digitalWrite(P9_12, HIGH);
  pinMode(P9_12, INPUT, 7, PULLUP);

  while (digitalRead(P9_12) == 1) {
  }
  printf("direction changed\n");

After I write HIGH on the pin, the sensor is supposed to pull low after about 40us, but I'm not seeing the pin change directions.

Anyway, I can't seem to get this to work.  Has anyone been able to make the sensor work with the BeagleBone?  How should I go about debugging this?  What other information can I provide to help others help me?

Thanks for your time.

-Aaron

Aaron Patterson

unread,
Oct 15, 2012, 10:08:51 PM10/15/12
to beagl...@googlegroups.com
On Mon, Oct 15, 2012 at 4:58 PM, Dan Watts <gigame...@gmail.com> wrote:
> Hi,
>
> You're right: the Adafruit code does look odd.
>
> As you say, to initiate communication with the DHT11/DHT22, you're supposed
> to pull the signal low for > 18 ms, then high, then wait for the signal to
> be pulled low again. Their code appears to skip the pull-high part. I can
> only assume that the Pi stops pulling the signal low when it switches to
> input mode.
>
> The only possible problem I can see in your code is that you might not be
> pulling the signal low for long enough: perhaps the usleep(2000) should be
> usleep(20000)? Also, trying to catch that 40 microsecond signal that begins
> the DHT22's response is tricky - that's probably why the Pi's code doesn't
> pull the line high explicitly. Perhaps dropping the digitalWrite(HIGH) from
> your code would have the same effect?

I gave both of these a whirl, and they did not work. I am able to see
the signal on my oscilloscope, though I'm new to using the
oscilloscope, so I'm not 100% sure.

> I have my doubts about whether reading data from the DHT22 can work reliably
> from Linux code, given the tight timing involved. Apparently the Adafruit
> code is managing to do it on the Pi, but there is a warning at the beginning
> of their tutorial saying it may not work, and perhaps the library they are
> using is more optimized than what is available for the Bone.

Yes, I read that. I'm starting to have the same doubts as well.

> Personally, I wimped out and use a separate processor to read the DHT22. I
> actually use a .Net Micro Framework processor for this (which surprisingly
> is able to handle the timings) and serial I/O, but something like the
> ATTiny2313 and I2C would be a better design.

I'm starting to think I might need to do the same. I'll give the Pi a
try, and I've got an Electric Imp on the way, so I'll probably try it
too.

I've built a meat curing fridge, and I'm trying to build an internet
enabled controller. I've built a monitor for it using the TI
Launchpad, and I'd like to build something that will post the
information to the internet (without hooking up to my laptop and
reading from a TTY). I suppose I could try XBee, but everything seems
much harder since I'm not using an Arduino. :-(

Anyway, thanks for the response. If I can't get the Pi or the Imp to
work out, I'll hook the Launchpad up to the Bone via UART.

Again, thanks for your time. I appreciate the response!

--
Aaron Patterson
http://tenderlovemaking.com/

Hunyue Yau

unread,
Oct 16, 2012, 2:41:34 AM10/16/12
to beagl...@googlegroups.com
That would appear to be an overkill to toss in another processor.
Prehaps a different part would be more appropriate?

FWIW, I recently got a humidity sensor working on the classic
Beagle. This is the SHT-15 part. Everything pretty much was drop
in. Hookup. The setup was:

SHT-15 - <Level Converter, 3.3V source> - Beagle Classic.

Driver for it was all upstream. It is built on top of the GPIO
subsystem. The level converter and 3.3V are option on the
beaglelcd board. Write up for it will be posted as time permits.

Since IO on the bone is 3.3V, the level converter won't be
needed.

>
> I've built a meat curing fridge, and I'm trying to build an
> internet enabled controller. I've built a monitor for it
> using the TI Launchpad, and I'd like to build something that
> will post the information to the internet (without hooking up
> to my laptop and reading from a TTY). I suppose I could try
> XBee, but everything seems much harder since I'm not using an
> Arduino. :-(
>
> Anyway, thanks for the response. If I can't get the Pi or the
> Imp to work out, I'll hook the Launchpad up to the Bone via
> UART.
>
> Again, thanks for your time. I appreciate the response!
>

--
Hunyue Yau
http://www.hy-research.com/

Dan Watts

unread,
Oct 16, 2012, 1:04:03 PM10/16/12
to beagl...@googlegroups.com
Yes, the SHT-15 is a good Beaglebone-compatible replacement for the DHT22, and it's more accurate too.  However, it is considerably more expensive, especially if you need a breakout board for the SMT chip: https://www.sparkfun.com/products/8257.  

Another Beaglebone-compatible temperature chip is the BMP085 (it also has a kernel driver): less expensive than the SHT-15, but no humidity reading.  (But you'll know how far above sea level your meat locker is!) https://www.sparkfun.com/products/8257

The cheapest Beaglebone-compatible temperature sensor is an analog thermistor like the TMP36 (https://www.sparkfun.com/products/10988), but it also doesn't support humidity, and is less accurate than the others.  

Dan.

Laurent H.

unread,
Jan 4, 2013, 4:07:53 AM1/4/13
to beagl...@googlegroups.com
I recently adapt the adafruit DHT22 code for beaglebone : https://github.com/lhuet/beaglebone-project/tree/master/DHT22
I succeed to read Temp and Humidity with this test code.
For performance reason, GPIO have to be accessed via mmap.
The following C++ code is used for GPIO access  : https://github.com/majestik666/Beagle_GPIO

Laurent

Dan Watts

unread,
Jan 4, 2013, 12:00:45 PM1/4/13
to beagl...@googlegroups.com
Hey, that's excellent!  I'll try it out this weekend.

What error rate have you been getting in your testing?  

The Raspberry Pi DHT22 code (http://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/overview) has about a 5% error rate on my Pi, with the DHT22 running at 5V.  The DHT22 doesn't seem as reliable at 3.3V

Thanks,

Dan.

Laurent H.

unread,
Jan 4, 2013, 5:45:58 PM1/4/13
to beagl...@googlegroups.com
I modified my code with a checksum and add a timestamp (instead of a counter value) for detecting each bit value.
The spec give 26-28 us for a bit data '0' and 70us for a bit data '1'.
I use the same threshold (40 us) than the arduino code : http://playground.arduino.cc/Main/DHTLib

With the counter threshold (from adafruit code), I have around 12% error rate.
With the timestamp threshoid, I have around 8/9 % error rate.
I think the counter threshold is more
I haven't investigate more to find the reason of checksum errors.
I'm not sure that it is due to the 3.3 V.
Perhaps, these errors are due to the very short time and the lack of accuracy of the time calculation.

If I have some time, I will test the same sensor with my Rasperry Pi and with a 5V  VCC (via a level shifter).

Laurent

Dan Watts

unread,
Jan 5, 2013, 6:08:11 PM1/5/13
to beagl...@googlegroups.com
Hi Laurent,

Your code works very well on my Beaglebone.

The error rate is actually a little lower than what I was seeing on the Raspberry Pi: about 4%.  It's being invoked from a Python application every 15 seconds. My Beaglebone doesn't much CPU load: no GUI, just some Python scripts and a shell session.

I'm powering it with 5V, but you might find that yours works the same with 3.3V or 5V.  Based on the comments on this Sparkfun page (https://www.sparkfun.com/products/10167), there seems to be some variation in the manufacturing quality.  Mine got a much higher error rate at 3.3V, about 50%.

Thanks again for the code,

Dan.

ghul...@me.com

unread,
Jan 10, 2013, 9:06:58 PM1/10/13
to beagl...@googlegroups.com
Hi,
How do you specify the gpio pins for clk and data with modprobe?

Thanks,
Greg

Laurent H.

unread,
Jan 11, 2013, 5:44:51 AM1/11/13
to beagl...@googlegroups.com
Hi Greg,

There is no clk pin on the DHT22 sensor.
You don't need to load a module with modprobe with the adafruit code or mine.
For pin wiring, you can go here : http://learn.adafruit.com/dht/connecting-to-a-dhtxx-sensor or here : http://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging
To specify the gpio pin, you can modify my code : https://github.com/lhuet/beaglebone-project/blob/master/DHT22/Beagle_GPIO_dht22.cc
On line 30, the gpio pin is hard-coded :
unsigned short pin = Beagle_GPIO::P8_3;
I wrote this code to test the feasability : only a main method with hard-coded parameters.

Laurent

Nuno Valverde

unread,
Jun 18, 2013, 5:50:07 AM6/18/13
to beagl...@googlegroups.com, aaron.p...@gmail.com
Hi everyone!

I'm compiling the files for the dht 22 on the beaglebone with your makefile and i'm getting error on the usleep function (undefined reference). It seems the library is not included. 
Does it happened to anyone else?

Nuno Valverde

unread,
Jun 18, 2013, 6:17:38 PM6/18/13
to beagl...@googlegroups.com
I think i solved the problem. I compiled with eclipse an worked.
However the results seems a bit strange. I always give error on checksum. When i remove the checksum check, i get temperatures about 80C and humidity of 220%...
I m trying to compare this code with one from arduino i have tested and works fine. I will let you know as soon as i find out.

By the way, anyone got plausible results?

Paul Richards

unread,
Jul 5, 2013, 10:59:17 AM7/5/13
to beagl...@googlegroups.com
I get the same problem, also working on a solution

Nuno Valverde

unread,
Jul 5, 2013, 12:11:11 PM7/5/13
to beagl...@googlegroups.com
I found a solution. 
I'll share with you as soon as i can.

Nuno Valverde

unread,
Jul 14, 2013, 5:32:48 PM7/14/13
to beagl...@googlegroups.com
Try this code I uploaded. It's based on the original posted here with some minor changes and it worked for me.
I'm new to github, so if anyting worng, just tell me.



On Friday, July 5, 2013 3:59:17 PM UTC+1, Paul Richards wrote:

Alvin Mites

unread,
Jul 17, 2013, 7:07:20 PM7/17/13
to beagl...@googlegroups.com
I'm fairly wet behind the ears when it comes to C++

ran g++ on the beaglebone with your scripts and got the following

# g++ Beagle_GPIO.cpp Beagle_GPIO_dht22.cpp 
/tmp/ccNXFgIu.o: In function `main':
Beagle_GPIO_dht22.cpp:(.text+0x21c): undefined reference to `clock_gettime'
collect2: error: ld returned 1 exit status

# g++ Beagle_GPIO.cpp 

/usr/lib/gcc/arm-angstrom-linux-gnueabi/4.7.3/../../../crt1.o: In function `_start':
init.c:(.text+0x34): undefined reference to `main'
collect2: error: ld returned 1 exit status


running newest version of angstrom

Nuno Valverde

unread,
Jul 21, 2013, 3:24:42 PM7/21/13
to beagl...@googlegroups.com
When compiling you have to add the option -lrt

Alvin Mites

unread,
Jul 31, 2013, 12:02:52 AM7/31/13
to beagl...@googlegroups.com
took me a few days to remember to check this, now it compiles (thank you)

when I run it with a DHT connected it says "Bus error" and exits
when I change the pin or remove the DHT it runs in an endless empty loop

any ideas?

Jacek Radzikowski

unread,
Jul 31, 2013, 12:22:29 AM7/31/13
to beagl...@googlegroups.com
Regarding the Bus error: this can help you:
https://groups.google.com/d/msg/beagleboard/yflrTE39KhQ/KIGIsz0GO7oJ

j.
> --
> For more options, visit http://beagleboard.org/discuss
> ---
> You received this message because you are subscribed to the Google Groups
> "BeagleBoard" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to beagleboard...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
Given a choice between two theories, take the one which is funnier

Koen Kooi

unread,
Jul 31, 2013, 3:32:45 AM7/31/13
to beagl...@googlegroups.com
Why not use the kernel driver for the DHTxx chips?

Alvin Mites

unread,
Jul 31, 2013, 12:41:43 PM7/31/13
to beagl...@googlegroups.com
Care to point me to where that is?

It's been an elusive issue for me, constructing a greenhouse controller have all my analog pins and relays working, the DHT has proven difficult without a working knowledge of C to fall back on

Alvin Mites

unread,
Jul 31, 2013, 12:47:21 PM7/31/13
to beagl...@googlegroups.com
Starting to understand, the post you reference refers to a bash script that "exports all pins" included in a library you released though I wasn't able to find the library -- suspect I could figure it out with a couple hours of hacking around but would save quite a bit of time with something to reference...

thanks you

Jacek Radzikowski

unread,
Jul 31, 2013, 1:31:49 PM7/31/13
to beagl...@googlegroups.com
This post was a followup to the earlier discussion so it does not
contain all the details repeated from the earlier posts, but explains
what causes the exception.
You can find the script at
https://github.com/piranha32/IOoo/blob/master/tools/bbb_enable_gpio.sh

j.

Alvin Mites

unread,
Jul 31, 2013, 3:34:29 PM7/31/13
to beagl...@googlegroups.com
thank you, I see what the script is doing but don't understand where the pin numbers used are coming from?
I can see they are GPIO pins but unsure what the reference is,

ex: for a beaglebone black how would I go find the value for P8_11 if I were to adapt the script to 
echo P*_11 > /sys/class/gpio/export # Dummy code

Alvin
al...@mitesdesign.com
386.490.2845
Web Automation Specialist


You received this message because you are subscribed to a topic in the Google Groups "BeagleBoard" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/beagleboard/mtWNv8Gs1d4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to beagleboard...@googlegroups.com.

Jacek Radzikowski

unread,
Jul 31, 2013, 6:57:56 PM7/31/13
to beagl...@googlegroups.com
The numbers in the script are not critical. They just have to activate
the GPIO modules. The kernel knows nothing about assignments of the
signals to the pins on the connectors, so using pin names will not
work. Instead you have to use GPIO line numbers. You can find
assignment table in many places on the web. I put together a
spreadsheet with all useful mappings I could find:
https://github.com/piranha32/IOoo/blob/master/doc/Beaglebone%20Black%20pin%20cheatsheet.ods
This is also a good resource:
https://docs.google.com/spreadsheet/ccc?key=0Ak9jSoNoDhWmdEduX1BOT1lLSGUxdlFUZ2lCVXBGLXc

j.

Alvin Mites

unread,
Jul 31, 2013, 9:05:09 PM7/31/13
to beagl...@googlegroups.com
thank you, the google doc shows being restricted vs public

your spreadsheet and this site http://adis.ca/post/beaglebone-gpio/
got me far enough to get a workable output, calibration seems rather off but I think that can be adapted

Alvin
al...@mitesdesign.com
386.490.2845
Web Automation Specialist


Alvin Mites

unread,
Jul 31, 2013, 9:26:25 PM7/31/13
to beagl...@googlegroups.com

Alvin Mites

unread,
Jul 31, 2013, 9:48:23 PM7/31/13
to beagl...@googlegroups.com
calibration... running this on P9_12 or linux pin 60

output from a loop of 20::

Temp = -102.3°C, Hum = 45.8%
Temp = 102.3°C, Hum = 93.2%
Temp = 614.2°C, Hum = 3463.2%
Temp = 51.1°C, Hum = 47.6%
Temp = 102.3°C, Hum = 46.6%
Temp = 23.9°C, Hum = 46.5%
Temp = 25.5°C, Hum = 33.8%
Temp = 25.5°C, Hum = 45%
Temp = 51.1°C, Hum = 46.6%
Temp = 51.1°C, Hum = 46.6%
Temp = 51.1°C, Hum = 46.5%
Temp = 51.1°C, Hum = 46.4%
Temp = 51.1°C, Hum = 195.4%
Temp = 51.1°C, Hum = 46.5%
Temp = 460.7°C, Hum = 3370%
Temp = 51.1°C, Hum = 91.4%
Temp = 102.3°C, Hum = 93.2%
Temp = 25.5°C, Hum = 46.4%
Temp = 102.3°C, Hum = 93.2%
Temp = 102.3°C, Hum = 93%


humidity in this room is near 46.4 -- temp is near 16C
any ideas?

Jacek Radzikowski

unread,
Jul 31, 2013, 9:56:43 PM7/31/13
to beagl...@googlegroups.com
My first guess is that you're missing the timings. What language do you use?

j.

Alvin Mites

unread,
Jul 31, 2013, 10:13:22 PM7/31/13
to beagl...@googlegroups.com
mostly python -- that is output from a quick bash loop

Alvin
al...@mitesdesign.com
386.490.2845
Web Automation Specialist


You received this message because you are subscribed to a topic in the Google Groups "BeagleBoard" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/beagleboard/mtWNv8Gs1d4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to beagleboard...@googlegroups.com.

CJNZ

unread,
Aug 27, 2013, 11:25:30 PM8/27/13
to beagl...@googlegroups.com
Hi Hunyue,

I am trying to get the SHT15 working on a classic Beagle.
I realise this is an old thread, but I am struggling a bit with the code.
Do you still have a code example that you could share?
Did you just connect clock and data to standard GPIO and 3.3 and ground?

Thanks,
CJ

lloydc...@gmail.com

unread,
Oct 8, 2013, 11:06:43 AM10/8/13
to beagl...@googlegroups.com
You can modify this code http://www.acmesystems.it/?id=89

I have and it works on my Beaglebone black. :)

mharr...@gmail.com

unread,
Feb 21, 2014, 9:07:01 AM2/21/14
to beagl...@googlegroups.com
Hi,

I am trying to use this code with an Adsong dht22 (am2302) temp/rh sensor running on the BBB.  I haven't used C for a number of years so I am kind of rusty.  What modifications did you make to compile it with g++ on the BBB?  How does portG map to the pinout on the BBB? Or what pin do I connect the data line to on the BBB?

Thanks!

JrBenito

unread,
Mar 13, 2014, 5:59:55 PM3/13/14
to beagl...@googlegroups.com
Latest Kernel has DHT11 and DHT22 drivers under IIO subsystem. We shall see it working on BBB very soon.

regards,
Benito.

mharr...@gmail.com

unread,
Mar 18, 2014, 6:05:36 PM3/18/14
to beagl...@googlegroups.com
Could you tell me more?  When and how would I get dht22 working on the BBB? Thanks!

J_ROBO

unread,
Apr 11, 2015, 9:36:51 PM4/11/15
to beagl...@googlegroups.com
What modifications did you make and would you be able to forward source code for this? Thanks.

Alvin Mites

unread,
Apr 11, 2015, 9:46:01 PM4/11/15
to beagl...@googlegroups.com
no modifications needed, just follow the guide/library at adafruit

J_ROBO

unread,
Apr 11, 2015, 9:59:36 PM4/11/15
to beagl...@googlegroups.com
I'd Ideally like to just use C code for the DHT22 to BBB as opposed to the python library based tutorial. 
Reply all
Reply to author
Forward
0 new messages