‘How-To’ Series – More Temp Sensors

There continues to be great interest in hacking weather sensors on the Pi. A while ago I wrote a ‘How-To‘ for the AOSONG AM2315 temperature/humidity sensor that was quite popular. Today I have released another ‘How-To‘ for the AM2315’s siblings – the AM2302, DHT11, and DHT22 sensors.

Temperature/Humidity Sensors

Temperature/Humidity Sensors

I have found that experienced Pi/Linux users can get these sensors up and running in a very short time. For many hackers new to the Pi and or Linux, it is a challenging learning process, sometimes even intimidating. Sopwith’sHow-To‘ series are guides designed to help these folks succeed in their Pi project.

Each ‘How-To‘ includes screen shots for nearly every step of a project. Although this takes some work and makes the documents longer, I have found it is these images that help Pi enthusiasts understand each implementation step.

You can download the ‘How-To‘ below. The Zip file also contains the modified test Python script described in the document.

 

Post a comment if the ‘How-To‘ Series helps you with your projects. Improvements, edits, bug reports, and requests for other ‘How-To‘ topics are most welcome.

There is a kid out there who would love to help you hack your Pi.

Sopwith

2 thoughts on “‘How-To’ Series – More Temp Sensors

  1. Hello
    You are the first which give the right information about the DHT22 and AM2302 humidity -temp sensor. Many thanks
    But I have still other problems with my RPI-B+. The sensor AM2302 is working at 3.3V, but he missing 40% of the readings. And If the sensor is activated too frequent it shuts down his power. In my situation that happens between 24 to 48 hours after starting the program. Reseting the RPI is not enough to reset the sensor because the 3.3V power stays on the pin during reset .The only way it can be reset is by taking of the power wire . and again power-wire on it go’s intermediately on running while the program was continuously waiting on the sensor signal even for 12 hours.
    I don’t have a storage scope only a normal analog scope otherwise I can see the signal of the sensor.I am wondering How is the signal .
    So I have the following questions:
    a. (How) Can I change The time between the retry’s and the number of retry’s in the Adafruit_DHT.read_retry routine.
    b. I want the program to stop with an error output ,if there is no response from the sensor.
    c. Can I see and change the Adafruit_DHT.read_retry routine. ?
    An other trail to optimize the response .
    d. IN the manual of the Rpi, I show the possibility that you can give a program Higher priority .How can I do that and is that working better.

    e. I want to use more of these sensors is it better to use an other board, which is more real-time and with what OR . Possible the arduino?
    These are a lot questions. You can split them .
    Many THanks
    Cees van Dam .
    I

    • Hello Cees,

      Sorry to hear you are having so much trouble with your sensor. My first question is – how often are you reading the sensor? If you read the datasheet, you cannot read the sensor more than once every 2 seconds. If you do, the sensor will stop responding. The second thing to note is this quote in the datasheet:
      “If signal from AM2302 is always high-voltage-level, it means AM23032is not working properly, please check the electrical connection status.”

      This means the sensor will put the data pin high and leave it there if it detects a fault. This is probably why you have to disconnect the Pi in order to get the sensor working again.

      Not knowing anything about your setup, here are a few things to think about:
      1) Are you providing the Pi adequate power? Make sure you are providing it at least 1A of regulated power and are not drawing excessive current with any connected USB devices. Flaky power supplies cause all kinds of problems. If you have a lot of USB devices, connect them to a powered USB hub.
      2) Are you using a lot of processing power by running a lot of other applications? This could interfere with the sensor readings.
      3) Did you connect a pull-up resistor to the sensor? The AM2302 has one built in so you should not be using one.

      The Adafruit_DHT.read_retry(sensor, pin) function call will attempt to read data from the sensor fifteen (15) times in two (2) second intervals. It returns as soon as is has valid data. This means the function call can take up to 30 seconds to return results. After 15 tries it gives up and return None values for the temp and humidity. I have found this code to work very well and have had few problems with it. If you do use it, then you are only able to get a sensor reading in a 2-30 second time window. In my view, if you need a temp/hum reading more than once or twice a minute, this device is not for you.

      As in all troubleshooting scenarios, create the simplest test harness to identify what is causing your problem. I suggest you write a very simple Python script that calls Adafruit_DHT.read_retry(sensor, pin), waits for a response, writes the results to a file, sleeps 5 seconds, and then calls Adafruit_DHT.read_retry(sensor, pin) again. This can be done with a simple loop.

      This should help you determine where the problem is. If you need help writing this test program, let me know.

      Here are my answers to your questions:
      a. (How) Can I change The time between the retry’s and the number of retry’s in the Adafruit_DHT.read_retry routine.
      *** In the pi_dht_read.c source code there is a #define DHT_MAXCOUNT 32000. You can increase this value but I advise against it. This value works well for the traditional Pi family. If you are running a Pi 2 – then you need to update the library because new code has been added for the faster processor.

      b. I want the program to stop with an error output ,if there is no response from the sensor.
      *** This is easy. If the read of the sensor fails both the temperature and humidity values will be None. When this happens, simply print an error message and exit the program.

      c. Can I see and change the Adafruit_DHT.read_retry routine. ?
      *** Yes. The source code for the library is here: https://github.com/adafruit/Adafruit_Python_DHT
      Make the changes you need to the source before running the setup script to install it.

      An other trail to optimize the response .
      d. IN the manual of the Rpi, I show the possibility that you can give a program Higher priority .How can I do that and is that working better.
      *** The Adafruit library already does this for you (void set_max_priority(void)) automatically.

      e. I want to use more of these sensors is it better to use an other board, which is more real-time and with what OR . Possible the arduino?
      *** You can switch to the Arduino if you wish. The sensor works fine there too. Take note though, I know people that are running 8 AM2302 sensors on a Pi with no problems using the Adafruit library. You should be able to get this working on a Pi.

      Try writing a simple test program and let me know what you find out. I will help you get you sensor working in any way I can.

      Sopwith

Leave a Reply to Sopwith Cancel reply

Your email address will not be published. Required fields are marked *