User avatar
joan
Posts: 15112
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Mon Jan 26, 2015 8:31 am

Tarcas wrote:Paul,
You mentioned that "All three sensors are on the same 1-Wire bus."
...
He was referring to his DS18B20 sensors which are 1-wire bus (Dallas) devices.

The DHT sensors are not 1-wire bus devices. You'd have to add your own hardware to stop more than one talking at a time.

paulv
Posts: 564
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Mon Jan 26, 2015 4:23 pm

Hi Tarcas,

Maybe you misunderstood?
I have (had) three DS18B20's on the 1-Wire bus. They each show-up as a different file, named by using their serial number. The data in those files can be read for the individual readings.
I have replaced one by an AM2302, using the method I described, to add the relative humidity.

Paul

Tarcas
Posts: 741
Joined: Thu Jan 09, 2014 5:38 am
Location: USA

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Tue Jan 27, 2015 2:47 am

paulv wrote:Hi Tarcas,

Maybe you misunderstood?
I have (had) three DS18B20's on the 1-Wire bus. They each show-up as a different file, named by using their serial number. The data in those files can be read for the individual readings.
I have replaced one by an AM2302, using the method I described, to add the relative humidity.

Paul
Okay, that's not what I understood. Thanks for clearing that up.

leva
Posts: 12
Joined: Mon Feb 23, 2015 6:18 am

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Mon Feb 23, 2015 6:38 am

Hello everybody

I also thought that since DHT sensors pull lines down during the commnunication, it would be safe to
supply 5V power, and to pull the data line to 3.3V with a resistor.

However, when I make the connections, I see 3.6V on the data line. I have DHT22/AM2302 sensor (small package). Is it possible it has a built-in pull-up resistor? I do not think so, not according to the datasheet.

Also, some one suggested using 5k-10k voltage divider (http://www.raspberrypi.org/forums/viewt ... 11#p643611) to get to 3.3V for the data line (when using 5V power). This also produces 3.6V on the data line with DHT22 sensor attached.

How safe is it to apply 3.6V to a data pin ? I would like to make a long cable for DHT22 thus need 5V power.

Thank you for advice.

danjperron
Posts: 3526
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Mon Feb 23, 2015 11:37 am

If I use A multimeter and I check the voltage at the DHT22 sensor data. I got 3.28V (I do have 3.3V for power).
If I check the resistance between power and the data pin on my DHT22 sensor, I got 2400 ohm.

Then there is a pull-up resistor!


All CMOS have protection diode link the I.C. power and ground bus. That was a good learning experience for Philips with the CMOS 4000 first series. Any static was damaging them. After that, they added protection diode on all I/O pins . This is why you have the 'B' suffix to the 4000 series now.

You have 3.6 V because you have current draw from the internal pull resistor from the AM2302.

5V => AM2302 pull up => GPIO PIN => GPIO protection diode => 3.3V power.

The trick is that you don't want to draw too much current that the 3.3V power is affected or you burst the diode.

Daniel

leva
Posts: 12
Joined: Mon Feb 23, 2015 6:18 am

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Mon Feb 23, 2015 11:36 pm

5V => AM2302 pull up => GPIO PIN => GPIO protection diode => 3.3V power

The trick is that you don't want to draw too much current that the 3.3V power is affected or you burst the diode.
Dear Daniel Perron

Forgive me for being slow, but are you suggesting that it is acceptable to connect the sensor they way I propose?

5V----------------------- pin1 power line
3.3V --|5kOhm|--,-- pin2 data line
GPIO---------------/

ground---------------- pin4 ground


Or you suggest to insert a small (say 100-300Ohm) resistor inline with GPIO to protect the GPIO pin?? whenever I tried to put a protective resistor on GPIO pin , the pin would not get any data on it...

thank you very much for your help

leva

leva
Posts: 12
Joined: Mon Feb 23, 2015 6:18 am

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Mon Feb 23, 2015 11:47 pm

Also, I am apologize, I was not clear in my original post
The voltages were measured in this circuit:

Raspberry DHT22/AM2302 sensor
5V---------------------------- pin1
3.3V-------|5.1kOhm|----pin2
ground----------------------pin4

the voltage difference between pin2 and pin4 is 3.6V
Note, there is no GPIO connected. I was expecting to see 3.3V

leva

paulv
Posts: 564
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Tue Feb 24, 2015 3:13 am

leva,

The 5K1 resistor should not be in series with the data line, but should be used to pull it up to the 5V.
Just connect it from the 5V (pin 1) to the data line (pin 2)

There is a diagram in the beginning of this post. I suggest you look at that. It also would not hurt to read the post in full to understand it all a bit better before trying things out.

paulv

leva
Posts: 12
Joined: Mon Feb 23, 2015 6:18 am

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Tue Feb 24, 2015 3:04 pm

Hello Everybody

Again, I apologize for not being clear.

The question which I am trying to address is how to connect the sensor to raspberry pi using about 3-5m cable (10-15ft) with minimum amount of additional hardware. It appears that connecting power to 3,3V is out of the question for this cable length even with SPI interface. (this is based on the data sheet as I did not find any reference if someone actually tried that). I do not have the cable yet, once it arrives, I will try, but I expect that it will not work...

So, thus the question how to power the sensor with 5V and plug it into 3,3V data line...

To translate the data sheet into my ascii-art, the hookup is this
Vcc -------------------- pin1 power line
Vcc --|Rpullup|--,-- pin2 data line
data ----------------/
ground----------------- pin4 ground

(I hope I did not make a mistake here)

Now, as far as I understand, the power to device and Vcc (logical zero) have nothing to do with each other. Thus, unless there is something funny in the sensor, the hookup should be

V++ -------------------- pin1 power line
Vcc --|Rpullup|--,-- pin2 data line
data ----------------/
ground----------------- pin4 ground

where V++ is some voltage. Then, again, if there is nothing funny inside the sensor, there is a switch between pin2 and pin4 (does not matter how it is implemented, relay or collector-emmitter, drain-source, etc) which when closed sets the data-ground voltage difference to zero indicating that logical level "one" is transmitted. when the switch is open, logical level "zero" is transmitted. Timing tells when each bit is transmitted and the transmission is initialized by setting pin2 to logical one from "outside" of the sensor for about 1ms or more. (again this is from data sheet). The Rpullup is needed to protect Vcc power supply from a short to ground during transmit of logical "one". Since capacitance is involved, the value fo Rpullup cannot be arbitrary large because the time to reach logical "zero" after release of the line will be slow. Thus, there is a compromise (Rpullup has to be as large as possible, but low-enough to allow fast-enough switching). From the posts above, I conclude that it is 1kOhm for Vcc=3.3V, not the 5kOhm I used in my circuit (i missed that when I was reading the posts above for the first time)

do I understand this correctly?

Of course, the statement that V++ and Vcc have nothing to do with each other is a "dream world". in reality, there are couplings..

Since I have little experience with electronics, to test "rest-state" voltages, I assembled this circuit (no data line)

V++ -------------------- pin1 power line
Vcc --|Rpullup|--,-- pin2 data line
ground----------------- pin4 ground

I set V++ to 5V and Vcc to 3,3V and measured the voltage between pin2 and pin4. It was 3,6V.
Thus, I conclude that this world is not a "dream world"...

Now, is this dangerous to connect pin2 to the GPIO on raspberry under these conditions? Does anyone know?
If it is dangerous, does anyone have a suggestion on how to proceed with minimum additional hardware?

Thank you
leva

danjperron
Posts: 3526
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Tue Feb 24, 2015 3:36 pm

The 3.6V only mean that you have a current leakage from the DHT22. simple as that.


I know that The DHT22 is a drain output signal. It's mean it is pulling only low (to ground). This is why you need to add a pull up
to the data pin.


The current leakage also mean that the DHT22 has a internal pull up resistor from the 5V pin. Or a current source!

Do you know Thevenin theorem? We could estimate what is the pull up resistance value at the DHT22.
Thevenin1.gif
DHT22 internal pull-up calculation
Thevenin1.gif (7.33 KiB) Viewed 6614 times
Then using the theorem we got a pull up resistor of ~24K with a leakage current of ~60 μA.

And yes it is safe to put the data line directly at the GPIO pin. if you want to be more safe put a diode, cathode to the DHT22, anode to the GPIO pin with the pull up resistor. This way the only thing that the DHT22 could do will be to ground the pin.

B.T.W. I tried that! and it works fine. But I found out that if I use joan's DHT22 code I need to power down the DHT22 when it is in lock up mode. This is why I use another GPIO to power the DHT22. So I use 3.3V only.

Daniel

leva
Posts: 12
Joined: Mon Feb 23, 2015 6:18 am

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Tue Feb 24, 2015 5:42 pm

Dear Daniel

I have heard about Thevenin's theorem, but I do not understand it. :-(

I do understand Ohm's law and both Kirchhoff's laws :-) So I think, I understand how the calculation you show is done..

just to confirm, you think it is safe to connect 3.6V WITHOUT the protection diode for long periods of time (I would like to build a thermostat using raspberry pi). you seem to imply that you have tried it and it worked without ill effects. right?

I do not think the diode between pull-up resistor and pin2 will solve the problem, though. I mean, it will solve the problem of 3,6V applied to GPIO, but to initialize a transaction, GPIO has to pull pin2 of the sensor to ground and if the diode is present, this will not happen (pin2 will stay at 3,6V)


Software:

Ideally, I would like to use SPI in bi-directional mode as described
http://www.raspberrypi.org/forums/viewt ... 37&t=86771

by joan.

However, I would like to use C-language, not python. I have not found a c-library which would do it and did not have time to write myself.
So, to start, was thinking of using C-library https://github.com/ondrej1024/foxg20/tree/master/dhtlib

in SPI mode with the base circuit described above (which I think you proposed) in this thread. The only difference is the power.

Does the sensor lock-up in SPI mode? I was under an impression that SPI-way is very reliable...

Thank you

leva

danjperron
Posts: 3526
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Tue Feb 24, 2015 6:09 pm

No I never had trouble in SPI mode.

But I did make a C version of my code

http://www.raspberrypi.org/forums/viewt ... 50#p506650

I think it is simple to use only 3 pins. You just need to toggle the pin into output mode to send the low signal for start, and then you select the SPI mode.

I use this technique to change the GPIO
http://elinux.org/RPi_Low-level_peripherals

N.B. the PI 2 use iobase address 0x3f000000. So you will need to check which RPi you have

http://www.raspberrypi.org/forums/viewt ... 3&t=100678


Daniel

leva
Posts: 12
Joined: Mon Feb 23, 2015 6:18 am

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Wed Feb 25, 2015 12:09 am

Dear Daniel

Oh, I thought your Ccode, but then I lost the link and I could only find
the python links afterwards. So I reasoned, I made a mistake...

These are good points you make about modifying the code for 3-wire operation. this is the reason I wanted to do it, as the only "transmit" from the host to the sensor is to pull the line low. Then the sensor replies...

what about the 3.6V business???

leva

danjperron
Posts: 3526
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Wed Feb 25, 2015 12:21 am

I thought that was solved. It is a 24K pull up resistor! So there is no problem.

You won't increase the 3.3V power and the system won't hang!

Daniel

leva
Posts: 12
Joined: Mon Feb 23, 2015 6:18 am

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Sun Mar 01, 2015 6:45 am

Hello everybody

I thought I would report this because I found it interesting...

As you know, I am trying to attach AM2302/DHT22 sensor to raspberry pi with a long cable (3-5m or 10-15 ft). Following the specifications of the sensor and other posts on the web, 5V supply is needed for the sensor to work. Then, there is this question of "pulling the data line high" to the power level. So, I (and several others) reasoned that the data line can be pulled to 3.3V instead of 5V which would provide a safe way to interface the sensor with raspberry pi and power it with 5V.

At least in my experiments, when 5V power is given to the sensor and data line is pulled to 3.3V, the resting potential of the data line is 3.6V. I was "afraid" to connect this to GPIO as I want a reliable system. Daniel convinced me that it is safe and if I would like to be conservative, I should install an inline resistor of 300 Ohm (or 1k) into the data line (and I saw similar suggestions elswhere) . Well, after I install a "protection" resistor, there is no communication with the sensor...

By the way, if the data line is pulled to 5V, then the resting state potential of the data line is 4.4V.

Then I found this page
http://tansi.info/rp/interfacing5v.html

This seem to imply that I should install a 1k inline resistor into the data line with a diode to 3.3V. And then assemble the circuit for the sensor as regular 5V (with pull up to 5V). When I assemble the circuit I see 3.8V on the data line during rest. Since by now, I was convinced that it is safe, I hooked it to GPIO and .... no communication with the sensor.....

Thus, I am forced to conclude, that the laws of physics do not work the same way in my kitchen as they work in many other kitchens... This protection resistor never worked for me even if I use it with a button, the button does not work. I tried it on raspberry pi B and now on B+...

As a "last" resort, I have done something forbidden. I have assembled the DHT22 circuit for 3.3V operation (the one in the data sheet), no inline resistor, hooked the sensor up via 25ft!!! (8m) serial cable and ....
the digits are flowing....

there are 4 lines going via the cable. 3.3V, ground, MOSI and MISO. On the sensor side, I have a 5.1kOhm pull-up resistor and data line is connected to MOSI/MISO with a diode between MOSI and MISO. I do not think it is important, but the pins on the serial cable are:
1- ground
2-3.3V
3-MISO
4-MOSI

The thing is running for 30min and reading the sensor every 3s without any apparent problems.
I had a cable in a roll, now it is mostly straight...

again, I am forced to conclude that the laws of physics do not work in the same way in my kitchen...
but why? is it the sensor I got? or is it the Pi that I got? or I did not wash hands before I started working on the thing???

What can I use as electrical noise generator to test robustness????

any thoughts and suggestions???

leva

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Sun Mar 01, 2015 7:35 am

ondrej1024 wrote:Hi Paul,

did you ever try to test the maximum cable length? On the DHT22 datasheet it says that the sensor communication works over a distance of 20m but later on I found this statement:
"With 3.3V supply voltage, cable length shall not be greater than 100cm. Otherwise, the line voltage drop will lead to the sensor power supply, resulting in measurement error."
Since we are powering the DHT22 via the RPi 3.3V line, I guess that applies to our case. I have always used short cables (50cm) but it would be interesting to know if the limit is really just 1m or if we can get reliable readings also at greater distances.

Ondrej
I tried.
I have 2 DHT22, one connected at about 2 meters from the pi and one connected at about 10 meters.
The near one works good with 3,3V
The far one needs to be powered at 5V, otherwise i get 99,9% in humidity always.
In my case, i used a lever shifer to take back data line to 3,3V world (my chip is pulled up to 5V)

paulv
Posts: 564
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Sun Mar 01, 2015 12:19 pm

Here are my two cents about this issue.

I did not try out the DHT with long leads myself, but please go the the very beginning of my post and have a look at the screenshots I took with an oscilloscope of the data with a 4K7 and a 1K pull-up resistor. I used a pull-up to 3V3, but note the effect on the slope of the pulse. And this is with only centimeters of wire between the DHT and the Pi. It looks to me that there is probably a capacitor between the DHT dataline and ground to make that kind of an effect on the rising edge of the pulse. It sounds to me therefore that your pull-up is not strong enough to compensate for the extra capacity in the cabling.

Pulling the data line up to 5V and connecting that data signal straight to a GPIO port is safe, provided there is not much current flowing. If you can stay below 5mA (hence the 1K series resistor), you are absolutely safe. Contrary to popular belief, there is no clamp from the GPIO ports to 3V3 through a diode on the die of the SOC. What has been proven by measurements is that there seems to be a diode to the 5V supply because when you inject a voltage, it is clamped at 5.6V (5V plus the junction of the diode), and that makes the GPIO ports "tolerant" for 5V logic. ("seems" because there is no specification, but the ports are definitely clamped to 5V6)

In any case, optionally adding a 1K resistor in series of the DHT data line, close the the Pi board, will protect it for transients that carry higher currents or plain mistakes. If you want to be even safer, add a diode after the series resistor from the data to 3V3 to clamp it at that level.

These two additional parts should make absolutely no difference in the signal quality you're getting to and from the DHT at the Pi. The operating voltage of the DHT (3V3 or 5V) and the pull-up resistor on the data line (I recommend 1K) to that supply are the only things that matter. If not, I would look for a bad component or a wiring error somewhere. Too bad you don't seem to have access to a scope, we're all rather blind at the moment.
Last edited by paulv on Fri Mar 13, 2015 7:11 pm, edited 1 time in total.

leva
Posts: 12
Joined: Mon Feb 23, 2015 6:18 am

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Sun Mar 01, 2015 12:37 pm

Dear paulv and others
In any case, optionally adding a 1K resistor in series of the DHT data line, close the the Pi board, will protect it for transients that carry higher currents or plain mistakes. If you want to be even safer, add a diode after the series resistor from the data to 3V3 to clamp it at that level.

These two additional parts should make absolutely no difference in the signal quality you're getting to and from the DHT at the Pi. The operating voltage of the DHT (3V3 or 5V) and the pull-up resistor on the data line (I recommend 1K) to that supply are the only things that matter. If not, I would look for a bad component or a wiring error somewhere. Too bad you don't seem to have access to a scope, we're all rather blind at the moment.
For me, adding the 1k resistor and a diode kills the communication with cables of 10cm or so.... what am I doing wrong??? I just put the resistor (even 300Ohm) and ... good-bye communication...

I did use 5.1kOhm pull-up just because after my initial cursory read of this thread, it did not register in my brain that there is a 1k-effect. What I plan to do is to add more cable length to kill the communication with my current setup and then swap the 5.1k pullup resistor to 1k one. Yes, lowering the resistor should help with cable length.

I am not sure if I will be able to test the setup using an oscilloscope to provide better information....

leva

User avatar
joan
Posts: 15112
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Sun Mar 01, 2015 12:49 pm

leva wrote: ...
I am not sure if I will be able to test the setup using an oscilloscope to provide better information....
...
piscope will show you any AM2302/DHT11/DHT22 messages.

leva
Posts: 12
Joined: Mon Feb 23, 2015 6:18 am

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Sun Mar 01, 2015 1:02 pm

I think an analogue trace is of interest to see the shape of the pulses...

i did not know about piscope existance. Looks like a cool tool, but if it is
"digital" then it shows only logical levels as a function of time and it is not enough in this situation...

User avatar
joan
Posts: 15112
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Sun Mar 01, 2015 1:20 pm

leva wrote:I think an analogue trace is of interest to see the shape of the pulses...

i did not know about piscope existance. Looks like a cool tool, but if it is
"digital" then it shows only logical levels as a function of time and it is not enough in this situation...
Fair enough. I should have read more closely. I didn't realise the problem was lack of voltage.

danjperron
Posts: 3526
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Sun Mar 01, 2015 2:26 pm

SPI and DHT22 are not meant to be on long cable wires.

Both of them are not made for that.


I had that problem long time ago and I switch my system using a more robust system.

RS-485 is a serial communication bus using line differential. This way you could go up to 1200 meters at 115k baud.

It is a bus system! You are able to put more than one module on the same cable.

If you check on the Raspberry PI forum you will notice that I did two types of RS-485 adaptor.

The first one is the simplest and add only 2 I/O.

The Github https://github.com/danjperron/PicMultiPurpose.git

And then I made a better version , with gerber PCB. This one had 10 I/Os.

The Forum http://direct.raspberrypi.org/forums/vi ... 12#p538112
The Github https://github.com/danjperron/PIC_MULTI_10_IO.git

And if you want to use an arduino. Modbus is available and of course DHT22 code is also available.

I didn't use the arduino with the DHT22 but I made an RFID reader using the Arduino. Then it is possible to look at my code to create your own Modbus code for the DHT22.

This is the post for the RFID reader using Modbus.
http://www.raspberrypi.org/forums/viewt ... 53#p658053


And for the RS-485 link with an Raspberry PI, it is so cheap that you don't need to do your own.

http://www.dx.com/p/usb-to-rs485-adapte ... een-296620

Daniel

leva
Posts: 12
Joined: Mon Feb 23, 2015 6:18 am

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Sun Mar 01, 2015 10:02 pm

What I plan to do is to add more cable length to kill the communication with my current setup and then swap the 5.1k pullup resistor to 1k one.
OK, same setup (3,3V, 5,1kOhm pullup) is running with 40ft (12m) serial cable.
I will try to find another extension...

I do not know how robust this setup is against RF noise, as I have no means of testing it other than
with an electrical razor (which did not have any affect on the signal)...

leva

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Mon Mar 02, 2015 8:58 am

i have to say that in my case cable length didn't kill temperature reading, but humidity reading (fixed 99.9%)
Can't understand why, but this was the case :)

leva
Posts: 12
Joined: Mon Feb 23, 2015 6:18 am

Re: Detailed Description of the RHT03 / DHT22 / AM2302 senso

Fri Mar 20, 2015 7:37 am

Hello everybody

at the beginging of this thread, Paul
Also note the pull-up resistor of 1K. In many documents, you will find a resistor of 4K7, and that is what I initially used as well. If you look at the signals with a scope, you will see that the 4K7 is not sufficient to overcome the capacitance on the pin to pull it high quick enough.
Earlier, I claimed that 5.1k resistor, 3,3V supply works with 40ft cable (which it does even after several weeks of acquisition with 3s interval there were no errors).

But, what I have done is, I have split the data line which was going to the sensor and added about 10f (3m) dangling cable... (just a little extra capacitance on the line) and that destroyed the communication to the sensor. Thus, as Pual said, 4,7k pull up resistor is not enough! I replaced the resistor to 1k and everything became happy....

I would like to express my gratitude to Paul, Daniel, Ondrej, Joan and all the other people who contributed to this thread. It was very educational to me, at least.

Lev

Return to “Automation, sensing and robotics”