Page 1 of 1

Struggling to connect DHT12

Posted: Sat Jan 18, 2020 6:16 pm
by nekenieh
Hello,

I desperately need help with connecting the brand new DHT12 sensor to my pretty much unused Raspberry Pi model 3b.
The problem is that the sensor does not seem to be working properly. The i2cdetect command somehow recognizes the connected sensor only every other time or so, and the Python3 program of mine also shows some incorrect values (zeroes) about 4 to 5 times out of 10. In the remaining 6 cases, it returns "OSError: [Errno 121] Remote I/O error".
I have followed several tutorials on setting RPi up and connecting the modules, so everything should be up to date and SPI, I2C and Serial options should be allowed in "raspi-config".
I connected the sensor in the following way: SCL to pin no. 5, - to pin no. 6, SCA to pin no. 3, + to pin no. 1 (I tried no. 2 - the 5V one as well).

Thank you for any tips, it is a part of a very important school project and I got stuck on this for several days. I am a complete beginner at this stuff.

Re: Struggling to connect DHT12

Posted: Sat Jan 18, 2020 8:50 pm
by joan
A clear photo of the connections you have made may help.

You could try it in non-I2C mode to see if that is more reliable.

Re: Struggling to connect DHT12

Posted: Sun Jan 26, 2020 11:04 pm
by ancris00
Hi nekenieh hi joan,
I am pretty sure that you have conected the sensor as other user in the following topic, followings the instruction in the website www.pibits...
https://www.raspberrypi.org/forums/view ... 2#p1531518
I do not know how to help, there is not much info about this sensor
I have got pi4, and I have activated i2c, have I got to do all the instructions of www.radiostud..? (look very old)
https://radiostud.io/howto-i2c-communication-rpi/
I execute i2cdetect -y 1 and I got nothing
Can someone help us?
Image

Re: Struggling to connect DHT12

Posted: Mon Jan 27, 2020 9:05 am
by joan
We need photos clearly showing the connections.

Re: Struggling to connect DHT12

Posted: Mon Jan 27, 2020 10:33 am
by ancris00
Attached the photos
Image
I hope this help

Re: Struggling to connect DHT12

Posted: Mon Jan 27, 2020 11:02 am
by joan
The code you are running appears to be Python 2. The system may default to Python 3 now.

Try running the script with Python 2.

E.g. python2 ./script.py

Re: Struggling to connect DHT12

Posted: Mon Jan 27, 2020 7:30 pm
by ancris00
I got this answer

Re: Struggling to connect DHT12

Posted: Mon Jan 27, 2020 9:23 pm
by joan
The script has illegal characters. Sometimes that is caused by cutting & pasting from a web page. See if you can download the script from another source.

Re: Struggling to connect DHT12

Posted: Tue Jan 28, 2020 4:22 pm
by ancris00
I have wroten all the words by myself instead of copy and I get this:

Re: Struggling to connect DHT12

Posted: Tue Jan 28, 2020 4:59 pm
by joan
That usually indicates a wiring problem.

Is the DHT12 detected with i2cdetect -y 1 ?

Re: Struggling to connect DHT12

Posted: Wed Jan 29, 2020 12:56 pm
by ancris00
Not detected

Re: Struggling to connect DHT12

Posted: Wed Jan 29, 2020 1:23 pm
by joan
So it's either faulty of not connected to the I2C bus. I'd guess it's not connected to the I2C bus. Can we have close-ups of the connections at the Pi and the sensor and a verbal description of which pin/GPIO is connected to which pin.

Re: Struggling to connect DHT12

Posted: Wed Jan 29, 2020 1:34 pm
by ancris00
yes

Re: Struggling to connect DHT12

Posted: Wed Jan 29, 2020 2:26 pm
by joan
Yellow needs to be connected to pin 3 at the PI (GPIO 2/ SDA).
Blue needs to be connected to pin 5 at the PI (GPIO 3/ SCL).

https://pinout.xyz/

Re: Struggling to connect DHT12

Posted: Thu Jan 30, 2020 9:29 am
by ancris00
Thanks joan,
you were Wright.
Now works :D
So, pins 27 and 28 what they are for? :roll:
More questions, why in Phyton3 does not work? :roll:
What the table/matriz does mean? :roll:

Re: Struggling to connect DHT12

Posted: Thu Jan 30, 2020 10:28 am
by rpdom
Pins 27 and 28 are for i2c bus 0 which is used for identifying HATs and other internal things. Pins 3 and 5 are i2c bus 1 which is available for general use.

The "print" statement is one of the major things that changed between Python 2 and Python 3.
In 2 you can use

Code: Select all

print "blah blah blah"
but in Python 3 you need to use

Code: Select all

print("blah blah blah")
The table shows that your i2c device has the address 5C. Every device connected to the i2c pins needs a unique address so the Pi knows which device it is talking to. Some devices have fixed addresses. Some have options where you can change the address. Valid addresses are 03 to 77 (245 addresses total available).

Re: Struggling to connect DHT12

Posted: Fri Jan 31, 2020 3:39 pm
by ancris00
Thank you rpdom,
now I want to go further
I want to see all these data in a chart using https://mydevices.com/
but the problema is that cayenne does not use this sensor (dht12=. It uses tmp102, tmp36, ds18b20 but not this one :(
Any idea?
Thanks in advance ;)

Re: Struggling to connect DHT12

Posted: Mon Feb 03, 2020 4:10 pm
by ancris00
Any help?

Re: Struggling to connect DHT12

Posted: Wed Feb 05, 2020 11:28 am
by ancris00
Hi all,
Finally I found a web with the solution https://community.mydevices.com/t/dht11 ... ry-pi/2015
You need only to copy this code to your file and that is all
And of course copy the MQTT username, password and client ID from www.mydevices.com

Code: Select all

import paho.mqtt.client as mqtt
import time
import sys
import Adafruit_DHT

time.sleep(30) #Sleep to allow wireless to connect before starting MQTT

username = "MQTT Username From Dashboard"
password = "MQTT Passsword From Dashboard"
clientid = "MQTT Client ID From Dashboard"

mqttc = mqtt.Client(client_id=clientid)
mqttc.username_pw_set(username, password=password)
mqttc.connect("mqtt.mydevices.com", port=1883, keepalive=60)
mqttc.loop_start()

topic_dht11_temp = "v1/" + username + "/things/" + clientid + "/data/1"
topic_dht11_humidity = "v1/" + username + "/things/" + clientid + "/data/2"
topic_dht22_temp = "v1/" + username + "/things/" + clientid + "/data/3"
topic_dht22_humidity = "v1/" + username + "/things/" + clientid + "/data/4"

while True:
    try:
        humidity11, temp11 = Adafruit_DHT.read_retry(11, 17) #11 is the sensor type, 17 is the GPIO pin number (not physical pin number)
        humidity22, temp22 = Adafruit_DHT.read_retry(22, 18) #22 is the sensor type, 18 is the GPIO pin number (not physical pin number)
        
        if temp11 is not None:
            temp11 = "temp,c=" + str(temp11)
            mqttc.publish(topic_dht11_temp, payload=temp11, retain=True)
        if humidity11 is not None:
            humidity11 = "rel_hum,p=" + str(humidity11)
            mqttc.publish(topic_dht11_humidity, payload=humidity11, retain=True)
        if temp22 is not None:
            temp22 = "temp,c=" + str(temp22)
            mqttc.publish(topic_dht22_temp, payload=temp22, retain=True)
        if humidity22 is not None:
            humidity22 = "rel_hum,p=" + str(humidity22)
            mqttc.publish(topic_dht22_humidity, payload=humidity22, retain=True)
        time.sleep(5)
    except (EOFError, SystemExit, KeyboardInterrupt):
        mqttc.disconnect()
        sys.exit()
Bye

Re: Struggling to connect DHT12

Posted: Thu Feb 13, 2020 7:38 am
by nekenieh
Hello,
Sorry for the late reply. I have checked many times and I believe the way I connected the sensor to RPi is correct. Attaching a photo anyway.
I'm starting to think the DHT12 or my RPi is not working as it should.

Re: Struggling to connect DHT12

Posted: Wed Feb 19, 2020 11:02 am
by ancris00
hi nekenieh,
the conexion looks ok
You can try a new sensor or to check with an oscilloscope
Regards
https://www.electrodragon.com/product/dht12/