Getting Started with Raspberry Pi: Your First Linux Embedded Project
The Raspberry Pi, a credit card-sized computer, has become a popular choice for DIY enthusiasts and developers due to its low cost and powerful computing capabilities. It runs the Linux operating system and can connect various sensors and modules through GPIO (General Purpose Input Output) pins to realize countless creative projects. Today, I will guide you through your first Linux embedded project – creating a temperature and humidity monitoring system using Raspberry Pi.
Preparation
Before we start, you need to prepare the following items:
-
• Raspberry Pi (any model, this project uses Raspberry Pi 4 as an example)
-
• Temperature and humidity sensor (DHT11 or DHT22)
-
• Several Dupont wires
-
• A MicroSD card (8GB or more, with Raspbian installed)
-
• Network connection
-
• Power adapter
Once you ensure that the Raspberry Pi has successfully installed the Raspbian operating system and can start normally, we can begin our project.
Step 1: Connect the Temperature and Humidity Sensor
First, we need to connect the DHT11 or DHT22 temperature and humidity sensor to the Raspberry Pi. The temperature and humidity sensor generally has three pins: VCC, GND, and DATA. We need to connect them to the Raspberry Pi’s 5V, GND, and GPIO pins respectively. For DHT11, the connection is shown in the diagram below:
Step 2: Install Libraries
Before reading temperature and humidity data on the Raspberry Pi, we need to install a Python library specifically for reading DHT series sensor data. Open the Raspberry Pi terminal and execute the following commands:
sudo pip3 install adafruit-circuitpython-dht
sudo apt-get install libgpiod2
These two commands install Adafruit’s DHT library and GPIO library, enabling the Raspberry Pi to read sensor data through Python scripts.
Step 3: Write the Data Reading Script
After installing the necessary libraries, we can start writing a Python script to read temperature and humidity data. Create a Python file named read_dht.py
and add the following code:
import adafruit_dht
import board
# Initialize the DHT11 sensor connected to GPIO4
dhtDevice = adafruit_dht.DHT11(board.D4)
try:
# Read temperature and humidity data from the sensor
temperature_c = dhtDevice.temperature
humidity = dhtDevice.humidity
print(f"Temperature: {temperature_c:.1f}C, Humidity: {humidity}%")
except RuntimeError as error:
# Capture and print any errors
print(error.args[0])
This code first imports the necessary libraries and initializes the DHT11 sensor connected to GPIO 4 (modify according to physical connection). Then, it reads the temperature and humidity data by calling the temperature
and humidity
attributes and prints the results.
Step 4: Run the Script
After saving the script, return to the terminal and run the following command to execute your script:
python3 read_dht.py
If everything goes smoothly, you will see the terminal output the current temperature and humidity information.
Advanced Expansion
Through the above steps, you have successfully completed a basic project of reading temperature and humidity using Raspberry Pi and the DHT11 sensor. This is just one of the many simple examples of what Raspberry Pi can do. You can try adding more features, such as:
-
• Storing data in a database for long-term tracking.
-
• Connecting the Raspberry Pi to the internet for remote monitoring.
-
• Adding other types of sensors to expand the project’s functionality.
The Raspberry Pi offers unlimited possibilities as long as you are willing to explore and try.
If you like my content, feel free to like and follow, and see you next time!
Leave a Comment
Your email address will not be published. Required fields are marked *