Bdiddy
Posts: 5
Joined: Tue Oct 14, 2014 9:21 pm

Max/Min Logging with DS18B20

Tue Mar 31, 2015 8:57 pm

Hi All,

I have a B+ with an adafruit LCD character plate and DS18B20 temperature sensor. I currently have it displaying the current temperature and the max and min values reached on the LCD. However I have noticed that a lot of the time the max temp value follows the current temperature reading even if it is dropping ie. not the max temperature reached.

I hope some one can help me out with this! I suspect its to do with the refresh and sample values in the code but not entirely sure.

My current code is:

Code: Select all

# Import all the libraries
import Adafruit_CharLCD as LCD
from w1thermsensor import W1ThermSensor
import time

# Sample/Refresh time in seconds
refresh = 2

# Temperature history in values (history duration will be samples * refresh)
samples = 10

# Define and initialise the LCD
lcd = LCD.Adafruit_CharLCDPlate()
# Set Display Colour
lcd.set_color(0.0, 1.0, 0.0)
# Set Size
lcd_columns = 16
lcd_rows    = 2

lcd.message("Logging\nBeginning.......")


# Define the temperature sensor
sensor = W1ThermSensor()

# Set up the history array
temp = sensor.get_temperature()
templist = [temp] * samples

try:
 while True:
 # Shift out the oldest temperature value
         for x in range(len(templist)-1):
                 templist[x] = templist[x+1]
 # Set the end item to be our current temperature
                 templist[len(templist)-1] = sensor.get_temperature()
 # Uncomment the following line for debugging
                 print "max %.2f min %.2f\n" % (max(templist),min(templist))

 # Display it on the LCD
                 lcd.set_cursor(0,0)
                 lcd.message("""   Temp %.2fC\n+%.2f    -%.2f""" %
                 (templist[len(templist)-1],max(templist),min(templist)))

 # Sleep until we need to sample/refresh
                 time.sleep(refresh)

except KeyboardInterrupt:
 print "\n\nProgram Stopped By User\n"

lcd.clear()
lcd.message("Logging Aborted\nBy User........")
This code is various code I've adapted from the internet along with my own code to suit the purpose required.

Many Thanks to anyone who can help!

Byron

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 5644
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: Max/Min Logging with DS18B20

Tue Mar 31, 2015 9:03 pm

Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, 2xB2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, and a A+) but Buster on the P4B's. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

Return to “Troubleshooting”