This project collects data about temperature and air humidity. The data is written to a csv file, which can be used to perform analysis and gain insights from the data.
- DHT11 (or DHT22) sensor
- Raspberry Pi 4 (Model B)
- 3 Female-to-female jumper cables
- SD-card (8GB minimal)
- Keyboard/Mouse (Optional)
- Monitor (Optional)
- Raspberry Pi OS (with an IDE if you're not using SSH)
- Tool to analyse data from CSV files (for example Excel, Google Sheets, Jupyter Notebooks, etc.)
Download the 'Raspberry Pi Imager' from https://www.raspberrypi.com/software/. Choose 'Raspberry Pi OS (32-bit) as the Operating System, and choose your micro SD card as 'storage'. Click 'Write' and wait untill the process is done. Insert the SD card into the Raspberry Pi and follow the set up process. Once set up is done, update the OS by entering the following commands into the terminal:
sudo apt-get update
sudo apt-get upgrade
As seen on the image above, the sensor has 3 labelled pins: VCC, DATA and GND. Connect your three jumper cables to the pins. The cable connected to VCC should go to a 5V power output pin (in my case I used a 5V pin, pin number 2). The DATA cable should go to a GPIO pin (I used pin 7) and the GND cable should go to a ground pin (I used pin 6). If everything is connected properly, the LED on the sensor should turn on (as seen on the first picture).
The only required library is the 'dht11' library. Install it by entering the following command into the terminal:
pip install dht11
Open up an IDE to test if everything is set up correctly. You can use the following Python code to test if your sensor is outputting data:
# Necessary imports
import dht11
import RPi.GPIO as GPIO
import time
import datetime
def main() -> None:
# Setting GPIO settings
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()
# Initialising sensor instance
instance = dht11.DHT11(pin=4) # CHANGE THE PIN NUMBER IF YOU'RE USING A DIFFERENT PIN
try:
while True:
# Reading values from sensor
result = instance.read()
# Checking if reading is valid
if result.is_valid():
# Printing data
print(f'Last valid input: {str(datetime.datetime.now())}')
print(f'Temperature: {result.temperature}C')
print(f'Humidity: {result.humidity}%')
# Waiting for 5 seconds to get next reading
time.sleep(5)
except KeyboardInterrupt:
print('Cleanup')
GPIO.cleanup()
if __name__ == '__main__':
main()
If everything is working correctly, readings should be printed to the console.
I modified the code above so it will store each correct reading inside a CSV file. The code will create a CSV file named 'data.csv', and it will store everything there. You can collect data over a period of time, and then stop by hitting 'CTRL/Command + C' on your keyboard. Here is my code:
# Necessary imports
import dht11
import RPi.GPIO as GPIO
import time
import datetime
def main() -> None:
# Setting GPIO settings
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()
# Initialising sensor instance
instance = dht11.DHT11(pin=4) # CHANGE THE PIN NUMBER IF YOU USE A DIFFERENT PIN
# Creating File and Writing headers
with open('data.csv', 'w') as file:
file.write('Time, Temp (C), Humidity (%)\n')
try:
while True:
# Reading values from sensor
result = instance.read()
if result.is_valid():
# Storing instance values
current_time = datetime.datetime.now()
temperature = str(result.temperature)
humidity = str(result.humidity)
# Printing data
print(f'Last valid input: {str(datetime.datetime.now())}')
print(f'Temperature: {result.temperature}C')
print(f'Humidity: {result.humidity}%')
# Writing data to file
file.write(f'{str(current_time)}, {temperature}, {humidity}\n')
# Waiting 5 seconds per measurements
time.sleep(5)
except KeyboardInterrupt:
print('Cleanup')
GPIO.cleanup()
if __name__ == '__main__':
main()
An example of the data can be found in the directory 'data' in this repository. There is also an example of the analysis I did on the data.
Data is collected by the sensor and saved in a CSV file. To collect the data, send the CSV file to your desired location. Visualize data with your tool of choice.