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. it also open a terminal and showing the outputs
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To achieve your goal of using a Raspberry Pi 2 Model B with DS18B20 temperature probes and LEDs for visual feedback, follow these steps:
Hardware Setup:
DS18B20 Wiring:
Connect each DS18B20's VDD to the Raspberry Pi's 3.3V pin.
Connect each DS18B20's GND to a ground pin on the Raspberry Pi.
Connect each DS18B20's DATA to a separate GPIO pin on the Raspberry Pi (e.g., GPIO4 for probe 1 and GPIO17 for probe 2).
Place a 4.7K ohm resistor between VDD and DATA lines on each DS18B20.
LEDs Wiring:
Connect the Anode of the green LED to a GPIO pin (e.g., GPIO27) and its Cathode to ground through a resistor.
Connect the Anodes of the second and third LEDs to their respective GPIO pins (e.g., GPIO22 for probe 1 status and GPIO23 for probe 2 status) and their Cathodes to ground through resistors.
Software Setup:
Enable 1-Wire Interface:
Open the Raspberry Pi configuration tool with sudo raspi-config.
Navigate to Interfacing Options -> 1-Wire and enable it.
Create a Python script to handle the temperature readings and LED indications:
python
import time
import os
import RPi.GPIO as GPIO
from w1thermsensor import W1ThermSensor, NoSensorFoundError
# Define GPIO pins
LED_GREEN = 27
LED_PROBE1 = 22
LED_PROBE2 = 23
# Initialize GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_GREEN, GPIO.OUT)
GPIO.setup(LED_PROBE1, GPIO.OUT)
GPIO.setup(LED_PROBE2, GPIO.OUT)
def read_temperature(sensor):
try:
temp = sensor.get_temperature()
return temp
except NoSensorFoundError:
return None
def main():
# Setup DS18B20 sensors
sensors = W1ThermSensor.get_available_sensors()
if len(sensors) < 2:
print("Error: Less than two DS18B20 sensors found.")
return
sensor1 = sensors[0]
sensor2 = sensors[1]
GPIO.output(LED_GREEN, GPIO.HIGH) # Indicate program is running
try:
while True:
temp1 = read_temperature(sensor1)
temp2 = read_temperature(sensor2)
# Handle probe 1
if temp1 is not None:
if temp1 < 35:
GPIO.output(LED_PROBE1, GPIO.HIGH)
else:
GPIO.output(LED_PROBE1, GPIO.LOW)
else:
# Blink LED on error
GPIO.output(LED_PROBE1, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(LED_PROBE1, GPIO.LOW)
time.sleep(0.5)
# Handle probe 2
if temp2 is not None:
if temp2 < 35:
GPIO.output(LED_PROBE2, GPIO.HIGH)
else:
GPIO.output(LED_PROBE2, GPIO.LOW)
else:
# Blink LED on error
GPIO.output(LED_PROBE2, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(LED_PROBE2, GPIO.LOW)
time.sleep(0.5)
# Print temperatures
print(f"Probe 1: {temp1 if temp1 is not None else 'Error'} °C")
print(f"Probe 2: {temp2 if temp2 is not None else 'Error'} °C")
time.sleep(120) # Wait for 2 minutes
finally:
GPIO.cleanup()
if __name__ == "__main__":
main()
Auto Start the Script on Boot:
Edit the rc.local file to run the script at startup:
bash
sudo nano /etc/rc.local
Add the following line before exit 0:
bash
su -c 'python3 /path/to/your/script.py' pi &
Replace /path/to/your/script.py with the actual path to your Python script.
Final Steps:
Ensure your 1-Wire devices are correctly configured by checking:
bash
cd /sys/bus/w1/devices
ls
You should see folders named starting with 28-.
By following these steps, your Raspberry Pi 2 Model B should read temperatures from two DS18B20 probes and control the LEDs accordingly, with the script running automatically on startup.