Raspberry Pi 4 Data Logger | DHT11/DHT22 Sensor Data Logger

preview_player
Показать описание
Raspberry Pi 4 Data Logger | DHT11/DHT22 Sensor Data Logger
Scroll down for code.....

In this tutorial, we will learn how to use DHT11/ DHT22/AM2302 with Raspberry Pi 4. Also see how to log sensor data in raspberry pi 4. Once you learn how to log data you can log any sensor data in same the fashion. For this tutorial we are logging DHT22 sensor data to .txt file but you can also use .csv extension file to export data directly in .csv file or change .txt file extension to .csv extension.

If you want to support my video please buy any product through my amazon affiliate link. I will receive a commission, at no extra cost to you.

LIST OF COMPONENT (affiliate links)

Music: Elektronomia - Ibiza [NomiaTunes Release]

import Adafruit_DHT
import time

#comment and uncomment the lines below depending on your sensor
#sensor = Adafruit_DHT.DHT11
sensor = Adafruit_DHT.DHT22
#sensor = Adafruit_DHT.AM2302

#DHT pin connects to GPIO 4
sensor_pin = 4

#create a variable to control the while loop
running = True

#new .txt file created with header

#loop forever
while running:

try:
#read the humidity and temperature
humidity, temperature = Adafruit_DHT.read_retry(sensor, sensor_pin)

#uncomment the line below to convert to Fahrenheit
temperature_f = temperature * 9/5.0 + 32

#sometimes you won't get a reading and
#the results will be null
#the next statement guarantees that
#it only saves valid readings
if humidity is not None and temperature is not None:

#print temperature and humidity
print('Temperature = ' + str(temperature) +','+ 'Temperature Fahrenheit = ' + str(temperature_f) +',' + 'Humidity = ' + str(humidity))
#save time, date, temperature in Celsius, temperature in Fahrenheit and humidity in .txt file

else:
print('Failed to get reading. Try again!')

except KeyboardInterrupt:
print ('Program stopped')
running = False
Рекомендации по теме
Комментарии
Автор

Great tutorial, I have problem with RPI4 on DHT11 reading, you made the point very well, thank you so much!

muratbaykal
Автор

great video, i have a question. if we are doing different sensor then given above, example: mlx90614. can the code be use ?

rafiesazali
Автор

Amazing explanation...The sampling rate of DHT22 is 0.5Hz. So, how do i ensure that I collect data for every 2 second?

ranjanpal
Автор

how to make it with the graphic built during the code is executed?

joanS
Автор

please, help.. this error keeps appearing!
from . import Beaglebone_Black_Driver as driver
ImportError: cannot import name 'Beaglebone_Black_Driver' from 'Adafruit_DHT'

basmaal-ghali
Автор

This is what i get when i run the code on my Raspberry pi 4(Model B)
ImportError: cannot import name 'Beaglebone_Black_Driver' from 'Adafruit_DHT'
Can you help me with this ?

nirmalgeorgemathew
Автор

Have you ever tried to use a second sensor and to read & log the data at the same time?

thschmidt
Автор

Very nice project! What do I need to change in %d/%m/%Y') + ', ' + str(temperature) + ', '+ str(temperature_f)+', ' + str(humidity) + '\n')" to write into the txt.file just e.g. 21.0 ...so a number with one decimal only...?

thschmidt
Автор

it doesn't work for me (Raspberry Pi 4). can't read the sensor. any idea?

MicheleAurelio
Автор

i tried this but it isnt working.. Raspberry Pi 4.. i have the 4pin DHT11 will not work i use 10k resistor.. like the kid says for the Ardunio.. it works fine for the Ardunio but running on the Raspberry Pi doesnt tells u to try again with the py program.. i tried different pins for input i tried 2 different Raspberry Pi 4s still nothing.. its there issue with the latest Rasperry pis? also wont work with Home Assistant image.. what is wrong?

comet
Автор

helpme aider moi, je n'arrive pas a fair fonction des sondes DHT 11 ou DHT 22 sur Raspberry Pi 4
runtime error :error accessing GPIO . make sure program is run as root with sudo
merci de votre aide

fredericprigent