nekenieh
Posts: 4
Joined: Sat Jan 18, 2020 1:05 pm

Struggling to connect DHT12

Sat Jan 18, 2020 6:16 pm

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.
Attachments
code.PNG
code.PNG (14.46 KiB) Viewed 1301 times
python.PNG
python.PNG (14.5 KiB) Viewed 1301 times
i2cdetec.PNG
i2cdetec.PNG (9.68 KiB) Viewed 1301 times

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

Re: Struggling to connect DHT12

Sat Jan 18, 2020 8:50 pm

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.

ancris00
Posts: 52
Joined: Sun Jan 19, 2020 8:56 pm
Location: Miranda de Ebro, Spain

Re: Struggling to connect DHT12

Sun Jan 26, 2020 11:04 pm

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
:lol: :lol: Pi 4 Model B :lol: :lol:
Raspbian Buster with desktop 4.19

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

Re: Struggling to connect DHT12

Mon Jan 27, 2020 9:05 am

We need photos clearly showing the connections.

ancris00
Posts: 52
Joined: Sun Jan 19, 2020 8:56 pm
Location: Miranda de Ebro, Spain

Re: Struggling to connect DHT12

Mon Jan 27, 2020 10:33 am

Attached the photos
Image
I hope this help
Attachments
Detalle_Dht12.jpg
Detalle_Dht12.jpg (67.52 KiB) Viewed 1199 times
Conexion_Pi_Dht12-jpg.jpg
Conexion_Pi_Dht12-jpg.jpg (86.54 KiB) Viewed 1199 times
Dht12_code_error.JPG
Dht12_code_error.JPG (95.98 KiB) Viewed 1199 times
:lol: :lol: Pi 4 Model B :lol: :lol:
Raspbian Buster with desktop 4.19

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

Re: Struggling to connect DHT12

Mon Jan 27, 2020 11:02 am

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

ancris00
Posts: 52
Joined: Sun Jan 19, 2020 8:56 pm
Location: Miranda de Ebro, Spain

Re: Struggling to connect DHT12

Mon Jan 27, 2020 7:30 pm

I got this answer
Attachments
SyntaxError.JPG
SyntaxError.JPG (24.02 KiB) Viewed 1169 times
:lol: :lol: Pi 4 Model B :lol: :lol:
Raspbian Buster with desktop 4.19

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

Re: Struggling to connect DHT12

Mon Jan 27, 2020 9:23 pm

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.

ancris00
Posts: 52
Joined: Sun Jan 19, 2020 8:56 pm
Location: Miranda de Ebro, Spain

Re: Struggling to connect DHT12

Tue Jan 28, 2020 4:22 pm

I have wroten all the words by myself instead of copy and I get this:
Attachments
Remote_I-O_Error.JPG
Remote_I-O_Error.JPG (38.38 KiB) Viewed 1145 times
:lol: :lol: Pi 4 Model B :lol: :lol:
Raspbian Buster with desktop 4.19

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

Re: Struggling to connect DHT12

Tue Jan 28, 2020 4:59 pm

That usually indicates a wiring problem.

Is the DHT12 detected with i2cdetect -y 1 ?

ancris00
Posts: 52
Joined: Sun Jan 19, 2020 8:56 pm
Location: Miranda de Ebro, Spain

Re: Struggling to connect DHT12

Wed Jan 29, 2020 12:56 pm

Not detected
Attachments
not detected.JPG
not detected.JPG (25.46 KiB) Viewed 1117 times
:lol: :lol: Pi 4 Model B :lol: :lol:
Raspbian Buster with desktop 4.19

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

Re: Struggling to connect DHT12

Wed Jan 29, 2020 1:23 pm

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.

ancris00
Posts: 52
Joined: Sun Jan 19, 2020 8:56 pm
Location: Miranda de Ebro, Spain

Re: Struggling to connect DHT12

Wed Jan 29, 2020 1:34 pm

yes
Attachments
dht12conexionb.JPG
dht12conexionb.JPG (46.81 KiB) Viewed 1101 times
dht12conexion.JPG
dht12conexion.JPG (83.71 KiB) Viewed 1101 times
:lol: :lol: Pi 4 Model B :lol: :lol:
Raspbian Buster with desktop 4.19

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

Re: Struggling to connect DHT12

Wed Jan 29, 2020 2:26 pm

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/

ancris00
Posts: 52
Joined: Sun Jan 19, 2020 8:56 pm
Location: Miranda de Ebro, Spain

Re: Struggling to connect DHT12

Thu Jan 30, 2020 9:29 am

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:
Attachments
Meaning.JPG
Meaning.JPG (25.85 KiB) Viewed 1067 times
Python2_ok.JPG
Python2_ok.JPG (25.25 KiB) Viewed 1067 times
:lol: :lol: Pi 4 Model B :lol: :lol:
Raspbian Buster with desktop 4.19

User avatar
rpdom
Posts: 17172
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Struggling to connect DHT12

Thu Jan 30, 2020 10:28 am

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).
Unreadable squiggle

ancris00
Posts: 52
Joined: Sun Jan 19, 2020 8:56 pm
Location: Miranda de Ebro, Spain

Re: Struggling to connect DHT12

Fri Jan 31, 2020 3:39 pm

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 ;)
:lol: :lol: Pi 4 Model B :lol: :lol:
Raspbian Buster with desktop 4.19

ancris00
Posts: 52
Joined: Sun Jan 19, 2020 8:56 pm
Location: Miranda de Ebro, Spain

Re: Struggling to connect DHT12

Mon Feb 03, 2020 4:10 pm

Any help?
:lol: :lol: Pi 4 Model B :lol: :lol:
Raspbian Buster with desktop 4.19

ancris00
Posts: 52
Joined: Sun Jan 19, 2020 8:56 pm
Location: Miranda de Ebro, Spain

Re: Struggling to connect DHT12

Wed Feb 05, 2020 11:28 am

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
:lol: :lol: Pi 4 Model B :lol: :lol:
Raspbian Buster with desktop 4.19

nekenieh
Posts: 4
Joined: Sat Jan 18, 2020 1:05 pm

Re: Struggling to connect DHT12

Thu Feb 13, 2020 7:38 am

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.
Attachments
IMG_20200213_084207.jpg
IMG_20200213_084207.jpg (75.15 KiB) Viewed 751 times
IMG_20200213_084153.jpg
IMG_20200213_084153.jpg (73.42 KiB) Viewed 752 times

ancris00
Posts: 52
Joined: Sun Jan 19, 2020 8:56 pm
Location: Miranda de Ebro, Spain

Re: Struggling to connect DHT12

Wed Feb 19, 2020 11:02 am

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/
:lol: :lol: Pi 4 Model B :lol: :lol:
Raspbian Buster with desktop 4.19

Return to “Troubleshooting”