How to use raspberry pi 2 model B with DS18B20 probe to read temperature of 2 pumps temperature? Prefer individual gpio for each probe. The program need to auto start whenever power on the raspberry and show temperature for every 2 minutes. for visual, include 3 LED. first, green light on when the program start running.2nd and 3rd LED for each probe. light up when temp less than 35. blinking if no signal or error.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Sure! I'll guide you through setting up your Raspberry Pi 2 Model B with two DS18B20 temperature sensors and three LEDs to monitor the temperature of two pumps. This setup will include:
Hardware Connections
Software Installation and Configuration
Python Script to Read Temperatures and Control LEDs
Auto-Starting the Program on Boot
Let's dive into each step.
1. Hardware Connections
Components Needed
Raspberry Pi 2 Model B
2 × DS18B20 Temperature Probes
3 × LEDs (1 Green, 2 Indicator LEDs)
3 × 220Ω Resistors (for LEDs)
4.7kΩ Resistor (for DS18B20)
Breadboard and Jumper Wires
Wiring Diagram
DS18B20 Sensors
Each DS18B20 has three pins: VCC, GND, and DATA.
Connect VCC: Connect the VCC pin of both DS18B20 sensors to the 3.3V pin on the Raspberry Pi.
Connect GND: Connect the GND pin of both sensors to a GND pin on the Raspberry Pi.
Connect DATA:
Sensor 1 DATA: Connect to GPIO4 (Pin 7).
Sensor 2 DATA: Connect to GPIO17 (Pin 11).
Pull-Up Resistor: Connect a 4.7kΩ resistor between the DATA lines (GPIO4 and GPIO17) and VCC (3.3V).
LEDs
Each LED has two legs: Anode (+) and Cathode (-).
Green LED (Program Indicator):
Anode: Connect to GPIO27 (Pin 13) through a 220Ω resistor.
Cathode: Connect to GND.
Indicator LED for Sensor 1:
Anode: Connect to GPIO22 (Pin 15) through a 220Ω resistor.
Cathode: Connect to GND.
Indicator LED for Sensor 2:
Anode: Connect to GPIO23 (Pin 16) through a 220Ω resistor.
Cathode: Connect to GND.
Note: You can use different GPIO pins if desired, but ensure they are correctly referenced in your Python script.
2. Software Installation and Configuration
Step 1: Update and Upgrade
First, ensure your Raspberry Pi is up to date.
bash
sudo apt-get update
sudo apt-get upgrade -y
Step 2: Enable 1-Wire Interface
The DS18B20 communicates via the 1-Wire protocol. Enable it as follows:
Open the Raspberry Pi configuration tool:
bash
sudo raspi-config
Navigate to Interfacing Options → 1-Wire → Enable.
Exit the configuration tool and reboot:
bash
sudo reboot
Step 3: Verify DS18B20 Sensors
After rebooting, verify that the sensors are detected.
Install the necessary tools if not already installed:
bash
sudo apt-get install python3-pip
Navigate to the 1-Wire devices directory:
bash
cd /sys/bus/w1/devices/
ls
You should see directories starting with 28- for each DS18B20 sensor.
3. Python Script to Read Temperatures and Control LEDs
We'll create a Python script that:
Turns on the green LED when the program starts.
Reads temperatures from both DS18B20 sensors every 2 minutes.
Turns on the corresponding LED if the temperature is below 35°C.
Blinks the LED if there's an error or no signal.
Step 1: Install Required Python Libraries
Install w1thermsensor for easier temperature reading:
bash
pip3 install w1thermsensor
Step 2: Create the Python Script
Create a new Python script, e.g., temp_monitor.py:
bash
nano ~/temp_monitor.py
Paste the following code:
python
import os
import time
from w1thermsensor import W1ThermSensor, SensorNotReady
import RPi.GPIO as GPIO
# GPIO pin definitions
GREEN_LED = 27
IND_LED1 = 22
IND_LED2 = 23
# Temperature threshold
TEMP_THRESHOLD = 35.0
# Initialize GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(GREEN_LED, GPIO.OUT)
GPIO.setup(IND_LED1, GPIO.OUT)
GPIO.setup(IND_LED2, GPIO.OUT)
# Turn on green LED to indicate program start
GPIO.output(GREEN_LED, GPIO.HIGH)
# Initialize sensors
sensor1 = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, "28-0000065bcb8e")
sensor2 = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, "28-0000065bcc9a") # Replace with your sensor's ID
def read_temperature(sensor):
try:
temperature = sensor.get_temperature()
return temperature
except SensorNotReady:
return None
def control_led(led_pin, temp):
if temp is not None:
if temp < TEMP_THRESHOLD:
GPIO.output(led_pin, GPIO.HIGH)
else:
GPIO.output(led_pin, GPIO.LOW)
else:
# Blink the LED to indicate error
GPIO.output(led_pin, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(led_pin, GPIO.LOW)
time.sleep(0.5)
try:
while True:
temp1 = read_temperature(sensor1)
temp2 = read_temperature(sensor2)
print(f"Pump 1 Temperature: {temp1 if temp1 is not None else 'Error'} °C")
print(f"Pump 2 Temperature: {temp2 if temp2 is not None else 'Error'} °C")
print("----------------------------")
control_led(IND_LED1, temp1)
control_led(IND_LED2, temp2)
# Ensure LEDs are off between blinks if no error
if temp1 is not None and temp1 < TEMP_THRESHOLD:
GPIO.output(IND_LED1, GPIO.HIGH)
if temp2 is not None and temp2 < TEMP_THRESHOLD:
GPIO.output(IND_LED2, GPIO.HIGH)
# Wait for 2 minutes
time.sleep(120)
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()
Important: Replace "28-0000065bcb8e" and "28-0000065bcc9a" with the actual IDs of your DS18B20 sensors. You can find these IDs by listing the devices:
bash
ls /sys/bus/w1/devices/
Step 3: Make the Script Executable
bash
chmod +x ~/temp_monitor.py
Step 4: Test the Script
Run the script manually to ensure it works:
bash
python3 ~/temp_monitor.py
The green LED should stay on.
Indicator LEDs should turn on if temperatures are below 35°C.
If there's an error reading a sensor, the corresponding LED should blink.
Press Ctrl+C to stop the script.
4. Auto-Starting the Program on Boot
We'll use systemd to create a service that runs the Python script on startup.
Verify that the service is running without errors:
bash
sudo systemctl status temp_monitor.service
You should see output indicating that the service is active and running. If there are errors, they will be displayed here for troubleshooting.
Additional Considerations
Logging Temperatures
If you want to log temperature readings to a file for future reference, modify the Python script to append readings to a log file.
python
# Add at the top
import logging
# Configure logging
logging.basicConfig(filename='/home/pi/temperature.log', level=logging.INFO,
format='%(asctime)s - Pump1: %(message)s - Pump2: %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
# In the loop, after reading temperatures
if temp1 is not None and temp2 is not None:
logging.info(f"{temp1} °C - {temp2} °C")
elif temp1 is None and temp2 is not None:
logging.info(f"Error - {temp2} °C")
elif temp1 is not None and temp2 is None:
logging.info(f"{temp1} °C - Error")
else:
logging.info(f"Error - Error")
Handling Multiple GPIOs for DS18B20
While the standard 1-Wire protocol uses a single data line, you've requested to use individual GPIOs for each DS18B20. This is unconventional since 1-Wire typically shares the data line for multiple devices. However, if it's essential to have separate GPIOs, you might need to use two separate 1-Wire buses. This requires additional configuration.
Enable Multiple 1-Wire Interfaces
Edit the boot configuration:
bash
sudo nano /boot/config.txt
Add the following lines to enable a second 1-Wire bus on GPIO17 (for example):
Note: This approach depends on w1thermsensor supporting multiple buses and may require additional handling.
Ensuring Proper Shutdown
To prevent issues, ensure that GPIOs are properly cleaned up on shutdown. The provided Python script includes GPIO.cleanup() in the finally block, which should handle this.
Summary
By following these steps, you will have a Raspberry Pi setup that:
Reads temperatures from two DS18B20 sensors connected to separate GPIO pins.
Controls three LEDs: one green LED indicates the program is running, and two indicator LEDs show the status of each pump's temperature.
Automatically starts the monitoring program on boot and logs temperature readings every two minutes.
Feel free to customize the script further based on your specific requirements, such as adding notifications or integrating with other systems.
Disclaimer: Always double-check your wiring to prevent any damage to your Raspberry Pi or components. Use appropriate resistors and ensure correct GPIO connections.