Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-mini-2024-07-18· OpenAIAi2 WildChat-4.8M (AI2)

How to use raspberry pi 2 model B with DS18B20 probe to read temperature of 2 pumps…

  1. User

    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

  2. Assistant

    To achieve your goal of interfacing a Raspberry Pi 2 Model B with two DS18B20 temperature probes, controlling LEDs based on temperature readings, and setting up a program to auto-start on boot, follow the steps below.

    Required Materials

    1. Raspberry Pi 2 Model B
    2. 2 x DS18B20 temperature sensors
    3. 3 x LEDs (Green, Red, Yellow)
    4. 3 x 220-ohm resistors (for LEDs)
    5. Jumper wires
    6. Breadboard
    7. 4.7kΩ resistor (between data line and Vcc for DS18B20)

    Wiring Diagram

    1. DS18B20 Wiring:

      • Connect the VCC of both DS18B20s to the 3.3V pin on Raspberry Pi.
      • Connect the GND of both to the Ground pin on the Raspberry Pi.
      • Connect the Data pin of the first DS18B20 to GPIO4 (Pin 7).
      • Connect the Data pin of the second DS18B20 to GPIO5 (Pin 29).
      • Place the 4.7kΩ resistor between the Data pin and the VCC.
    2. LED Wiring:

      • Connect the anode of the Green LED to a GPIO pin, e.g., GPIO17 (Pin 11), and cathode to Ground.
      • Connect the anode of the Red LED to GPIO27 (Pin 13), and cathode to Ground.
      • Connect the anode of the Yellow LED to GPIO22 (Pin 15), and cathode to Ground.
      • Use 220Ω resistors in series with LEDs to limit current.

    Software Setup

    1. Install Necessary Packages:

      Open a terminal on your Raspberry Pi and install the w1thermsensor library by running:

      sudo apt-get update
      sudo apt-get install python3-pip
      pip3 install w1thermsensor
      
    2. Enable 1-Wire Interface:

      • Open the config.txt file:
      sudo nano /boot/config.txt
      
      • Add the following line:
      dtoverlay=w1-gpio,gpio=4
      
      • Enable the 1-wire module by adding:
      dtoverlay=w1-gpio
      
      • Reboot your Raspberry Pi:
      sudo reboot
      
    3. Python Script:

      Create a Python script named temperature_monitor.py:

      nano ~/temperature_monitor.py
      
      import time
      import os
      from w1thermsensor import W1ThermSensor
      import RPi.GPIO as GPIO
      
      # Setup GPIO pins
      GPIO.setmode(GPIO.BCM)
      GPIO.setup(17, GPIO.OUT)  # Green LED
      GPIO.setup(27, GPIO.OUT)  # Red LED for Probe 1
      GPIO.setup(22, GPIO.OUT)  # Yellow LED for Probe 2
      
      # Initialize sensors
      sensor1 = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, "28-00000xxxxxx1")  # Replace with your sensor ID
      sensor2 = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, "28-00000xxxxxx2")  # Replace with your sensor ID
      
      GPIO.output(17, True)  # Turn on Green LED at start
      
      try:
          while True:
              temperature1 = sensor1.get_temperature()
              temperature2 = sensor2.get_temperature()
      
              print(f"Temperature Pump 1: {temperature1}°C")
              print(f"Temperature Pump 2: {temperature2}°C")
      
              # Handle LED indication
              # For sensor 1
              if temperature1 < 35:
                  GPIO.output(27, True)  # Turn on Red LED
              else:
                  GPIO.output(27, False)  # Turn off Red LED
              
              # For sensor 2
              if temperature2 < 35:
                  GPIO.output(22, True)  # Turn on Yellow LED
              else:
                  GPIO.output(22, False)  # Turn off Yellow LED
      
              time.sleep(120)
      
      except Exception as e:
          print(f"Error: {str(e)}")
      finally:
          GPIO.cleanup()
      
    4. Make the Script Executable:

      chmod +x ~/temperature_monitor.py
      
    5. Setup Autostart at Boot:

      Create a new systemd service to run the script at startup:

      sudo nano /etc/systemd/system/temp_monitor.service
      

      Add the following configuration:

      [Unit]
      Description=Temperature Monitor Service
      After=multi-user.target
      
      [Service]
      ExecStart=/usr/bin/python3 /home/pi/temperature_monitor.py
      Restart=always
      User=pi
      
      [Install]
      WantedBy=multi-user.target
      
    6. Enable the Service:

      sudo systemctl daemon-reload
      sudo systemctl enable temp_monitor.service
      sudo systemctl start temp_monitor.service
      

    Conclusion

    Now, your Raspberry Pi should automatically run the temperature monitoring script on boot. The green LED will light up to indicate the program is running, and the red/yellow LEDs will indicate temperatures for each probe according to the specified logic. You can monitor the terminal directly or check the logs for any issues.

    For error conditions or lack of signal, you can enhance the code to blink the respective LEDs. Note that you may need to replace "28-00000xxxxxx1" and "28-00000xxxxxx2" in the script with the actual IDs for your temperature sensors. You can find the IDs by listing the devices in /sys/bus/w1/devices/.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.