element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum Need Assist in Programing Flowmeter and temperature sensor to the RaspberryPi (Python)
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 15 replies
  • Subscribers 705 subscribers
  • Views 3381 views
  • Users 0 members are here
  • raspberry_pi
  • flowmeter
  • temperaturesensor
Related

Need Assist in Programing Flowmeter and temperature sensor to the RaspberryPi (Python)

e14 Contributor
e14 Contributor over 12 years ago

Hey guys i'm new to the community and just starting out with the raspberry pi.

i do have some basic programming experience with C and FORTRAN but python is new to me.

 

I am working on a project that requires tow of the Liquid flow Meter - Plastic 1/2" NPS Threaded (adafruit 828), and six of the Waterproff DS18B20 Digital temperature (adafruit item 381).

 

I am trying to program them into the raspberry Pi in order to record and display each of temperature sensor data in Fahrenheit and each flow meter need to put out instantaneous speed of liquid in Gal / Minutes.

 

Adafruit does have the code for the temperature sensor and the flow meter but the problem is that the code for flowmeter is designed to output the data on a twitter page and i can't make any heads or tails of it. also the two programs need to be able to put together in to a single file.

 

please help.

  • Sign in to reply
  • Cancel
  • iagorubio
    0 iagorubio over 12 years ago

    I have experience with the DS1620 temperature sensor.

     

    If this DS18B20 is anywhere close to the DS1620, it's not the easiest temperature sensor to work with. Analog sensors use to be much easier as you just need a lookup table for taking temperature from the analog value read.

     

    Those are digital temperature sensors and have a more or less complex communication protocol to get data out of them. They also use to have signaling - at least the DS1620 - where you program a max and min temperatures Th and Tl and the chip will trigger an alarm when the temperature is higher than Th or lower than Tl. The 1620 also have a Tcom pin that goes high from when one temperature is reached, to when the opposite temperature is reached so it can act as a standalone thermostat is you plug it to a fan or a heater.

     

    I write this so you realize that may be those sensors are more complex than what you need to just make some temperature readings.

     

    With this kind of thermistor TMP36 - Analog Temperature sensor [TMP36] ID: 165 - $1.50 : Adafruit Industries, Unique & fun DIY electronics and kits as example the temp reading is trivial Temp in °C = [(Vout in mV) - 500] / 10.

     

    You just would need some marine heat shrink tube to isolate the sensor and solder it to a wire to have a working sensor like the other one you are linking. And much cheaper I guess.

     

    Anyway if you have already bought the temp sensors, there is a Arduino library you can use with the ardupi or PiFace wiring libraries. That may be the easier path.

     

    Ardupi - Cooking Hacks - Documentation - Raspberry Pi to Arduino shields connection bridge

    PiFace wiring - PiFace | Wiring Pi

    DS18B20 library - http://www.milesburton.com/?title=Dallas_Temperature_Control_Library

     

    Don't try the same path with the flow meter arduino example though as it uses interrupts, and those are completely different beasts in Arduino and raspberry. In the raspberry you can use separate processes for each reading so you don't need to use interrupts and use a dedicated process for each flow meter instead.

     

    The flow meter you just need to count pulses,  and average the readings to get the flow so it's trivial to dead with it.

     

    It just have a pinwheel with a magnet so each time it spins it sends a pulse when a hall effect sensor detects the magnet passing.

     

    You will also need to calibrate it because I am sure the piping will have something to do with how much liquid is passing by on each spin.

     

    You will also need to have a steady flow, because reflows -liquid going back - will make the pinwheel spin. This is a wild guess from the device description but something to take into account.

     

    What libraries / languages you plan to use ?

     

    ... and ...

     

    What kind of output you need ?

     

    It may be easier to separate each sensor in a task and later on plug it all together than to try to get it all working at the same time.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to iagorubio

    Thanks for the replay.

     

    I actually already have the thermal sensor working with this code

     

    import os

    import glob

    import time

    os.system('sudo modprobe w1-gpio')

    os.system('sudo modprobe w1-therm')

     

     

    base_dir = '/sys/bus/w1/devices/'

    sensor1_folder = glob.glob(base_dir + '28-000004a8873d')[0]

    sensor1_file = sensor1_folder + 'w1-slave'

     

     

    def sensor1_raw():

      s1 = open(sensor1_file, 'r')

      s1lines = s1.readlines()

      s1.close()

      return s1lines

     

    def sensor1_temp():

      s1lines = sensor1_ra()

      while s1lines[0].strip()[-3:] !='YES':

      time.sleep(0.2)

      s1lines = sensor1_raw()

      equals_pos = s1lines[1].find('t=')

      if equals_pos != -1:

      tempstarting = s1lines[1][equals_pos+2:]

      temp_c = float(temp_string) / 1000.0

      temp_f = temp_c * 9.0 / 5.0 + 32.0

      return temp_c, temp_f

     

    while True:

      Print(sensor1_temp())

      time.sleep(1)

     

    ________________________________________________________________________________

    Which prints out the Temperature data every second but my big problem is figuring out how to count the pulses of the flow meter or to count the time between the pulses to get the frequency. That is the part of the program that i need big help with.

     

    Using Python

     

    I have tried this code but i'm doing something wrong it seems.

     

    ________________________________________________________________________________

     

    import os # importing OS module just in-case needed

    import time # Importing Time module

    import RPi.GPIO as gpio # Importing RPIO module as gpio

     

     

    # Initializing GPIO ports

    boardRevision = gpio.RPI_REVISION # Clearing previous gpio port settings

    gpio.setmode(gpio.BCM) # Use real physical gpio port numbering

    gpio.setup(22, gpio.IN, pull_up_down=gpio.PUD_UP) # setting pin 22 as pull up resistor

     

     

    while True: # main loop continuously running

     

     

      gflowcount = 1 # setting loop execution count

     

     

      while gflowcount <= 2: # loop for getting two times to compare

     

     

      if (gflowcount == 1 & gpio.input(22) == True): # Executing loop at first loop

     

      gflowtime1 = int(time.time()*1000) # Getting time of first pulse

      gflowcount = gflowcount + 1

     

      else if (gflowcount == 2 & gpio.input(22) == True): # Executing loop at second pulse

     

      gflowtime2 = int(time.time()*1000) # Getting time of second pulse

     

      else:

      print ('error')

     

      freq = gflowtime2 - gflowtime1 # Calculating time between two pulses

      hertz = 1000.0000 / freq # Calculating frequency

      flow = hertz / (60 * 7.5) # L/S # Calculating L/S

     

     

    while True: # loop running every 2 seconds to output the data

     

     

      print freq # Printing time between two pulses

      print ('time between two pulse')

      print hertz # printing the frequency

      print ('frequency')

      print flow # Printing L/S

      print ('L/S')

      time.sleep(2) # Pausing for 2 sec.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • iagorubio
    0 iagorubio over 12 years ago

    What is the problem with this second code ?

     

    The indentation is lost when pasting code here, and unfortunately it's hard to follow unindented Python code.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to iagorubio

    I do apologize, I didn't realize that copy and paste messed with the indentation.

    the problem is that only output i see to the screen is the 'error' instead of seeing the frequency. If i can get the frequency i should be able to make it work with that.

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to e14 Contributor

    Perhaps you want 'and' rather than '&'.  The latter is doing a bitwise AND where I think you want a logical AND?

     

    Regards,

    Martyn

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • iagorubio
    0 iagorubio over 12 years ago in reply to e14 Contributor

    I think martyndavies hit the nail on the head.

     

    try with :

     

      if( (gflowcount == 1) and (gpio.input(22) == True) ):

     

    The same in the next if statement.

     

    else if( (gflowcount == 2) and (gpio.input(22) == True) ):

     

    In other languages - C, Java, ... - the logical AND is written "&&".

     

    May be that's what caused that error.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to iagorubio

    hey guys i just found out that there's a noise issue with the breadboard that i'm using so i'll need to address that first.

     

    i'll keep in touch. thanks for all your help.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to e14 Contributor

    hey guys i'm back. I have fixed the noise issue and have completed most of the program but i need some help to do the final touches.

    what i need to do is in the

     

    def glycolFlow():

    and in the

    def waterFlow():

     

    when those functions are called, inside the function it need to look to see if there is a pulse for 2 seconds. if the pulse is there then the rest of the function can execute if the pulse is not there within 2 seconds then the function returns 0.0 instead of the calculated data.

     

    import os
    import glob
    import time
    import RPi.GPIO as gpio
    
    # Initializing GPIO ports
    boardRevision = gpio.RPI_REVISION # Clearing previous gpio port settings
    gpio.setmode(gpio.BCM) # Use real physical gpio port numbering
    gpio.setup(18, gpio.IN, pull_up_down=gpio.PUD_UP) # setting pin 18 as pull up resistor for glycol flow meter
    gpio.setup(25, gpio.IN, pull_up_down=gpio.PUD_UP) # setting pin 12 as pull up resistor for water flow meter
    
    
    # starting thermal probe drivers
    os.system('sudo modprobe w1-gpio')
    os.system('sudo modprobe w1-therm')
    
    
    # setting sensors data file location
    sensor1_file = '/sys/bus/w1/devices/28-000004a8873d/w1_slave'
    sensor2_file = '/sys/bus/w1/devices/28-00000574c16a/w1_slave'
    sensor3_file = '/sys/bus/w1/devices/28-00000561223c/w1_slave'
    sensor4_file = '/sys/bus/w1/devices/28-00000560107a/w1_slave'
    sensor5_file = '/sys/bus/w1/devices/28-00000560dca6/w1_slave'
    sensor6_file = '/sys/bus/w1/devices/28-0000057478b2/w1_slave'
    
    
    def glycolFlow():
      glycolStartTime = int(time.time())
      print 'glycol start time', glycolStartTime
      glycolCount = 0
      while glycolCount < 15 :
           if gpio.input(18) == True:
                if gpio.input(18) == False:
                     glycolCount += 1
                     print 'glycol count', glycolCount
      glycolEndTime = int(time.time())
      print 'glycol end time', glycolEndTime
      glycolSecsPerLiters = glycolEndTime - glycolStartTime
      glycolLitersPerHour = (3600.0 / glycolSecsPerLiters) * 30
      glycolGalPerHour = glycolLitersPerHour * 0.26417205235815
      return glycolGalPerHour
    
    
    def waterFlow():
      waterStartTime = int(time.time())
      print 'water start time', waterStartTime
      waterCount = 0
      print 'water count', waterCount
      while waterCount < 15 :
           if gpio.input(25) == True:
                if gpio.input(25) == False:
                     waterCount += 1
                     print 'water count', waterCount
      waterEndTime = int(time.time())
      print 'water end time', waterEndTime
      waterSecsPerLiters = waterEndTime - waterStartTime
      waterLitersPerHour = (3600.0 / waterSecsPerLiters) * 30
      waterGalPerHour = waterLitersPerHour * 0.26417205235815
      return waterGalPerHour
    
    
    def ts1():
      s1 = open(sensor1_file, 'r')
      s1lines = s1.readlines()
      s1.close()
      while s1lines[0].strip()[-3:] !='YES':
           s1lines = sensor1_raw()
      equals_pos1 = s1lines[1].find('t=')
      if equals_pos1 != -1:
           temp_string1 = s1lines[1][equals_pos1+2:]
           temp_c1 = float(temp_string1) / 1000.0
           temp_f1 = temp_c1 * 9.0 / 5.0 + 32.0
      return temp_c1
    
    
    def ts2():
      s2 = open(sensor2_file, 'r')
      s2lines = s2.readlines()
      s2.close()
      while s2lines[0].strip()[-3:] !='YES':
           s2lines = sensor2_raw()
      equals_pos2 = s2lines[1].find('t=')
      if equals_pos2 != -1:
           temp_string2 = s2lines[1][equals_pos2+2:]
           temp_c2 = float(temp_string2) / 1000.0
           temp_f2 = temp_c2 * 9.0 / 5.0 + 32.0
      return temp_c2
    
    
    def ts3():
      s3 = open(sensor3_file, 'r')
      s3lines = s3.readlines()
      s3.close()
      while s3lines[0].strip()[-3:] !='YES':
           s3lines = sensor3_raw()
      equals_pos3 = s3lines[1].find('t=')
      if equals_pos3 != -1:
           temp_string3 = s3lines[1][equals_pos3+2:]
           temp_c3 = float(temp_string3) / 1000.0
           temp_f3 = temp_c3 * 9.0 / 5.0 + 32.0
      return temp_c3
    
    
    def ts4():
      s4 = open(sensor4_file, 'r')
      s4lines = s4.readlines()
      s4.close()
      while s4lines[0].strip()[-3:] !='YES':
           s4lines = sensor4_raw()
      equals_pos4 = s4lines[1].find('t=')
      if equals_pos4 != -1:
           temp_string4 = s4lines[1][equals_pos4+2:]
           temp_c4 = float(temp_string4) / 1000.0
           temp_f4 = temp_c4 * 9.0 / 5.0 + 32.0
      return temp_c4
    
    
    def ts5():
      s5 = open(sensor5_file, 'r')
      s5lines = s5.readlines()
      s5.close()
      while s5lines[0].strip()[-3:] !='YES':
           s5lines = sensor5_raw()
      equals_pos5 = s5lines[1].find('t=')
      if equals_pos5 != -1:
           temp_string5 = s5lines[1][equals_pos5+2:]
           temp_c5 = float(temp_string5) / 1000.0
           temp_f5 = temp_c5 * 9.0 / 5.0 + 32.0
      return temp_c5
    
    
    def ts6():
      s6 = open(sensor6_file, 'r')
      s6lines = s6.readlines()
      s6.close()
      while s6lines[0].strip()[-3:] !='YES':
           s6lines = sensor6_raw()
      equals_pos6 = s6lines[1].find('t=')
      if equals_pos6 != -1:
           temp_string6 = s6lines[1][equals_pos6+2:]
           temp_c6 = float(temp_string6) / 1000.0
           temp_f6 = temp_c6 * 9.0 / 5.0 + 32.0
      return temp_c6
    
    
    while True:
      glycolGalPerHour = glycolFlow()
     if glycolGalPerHour != 0.0:
           ts1 = ts1()
           ts2 = ts2()
           ts3 = ts3()
           ts4 = ts4()
     else:
           ts1 = 0
           ts2 = 0
           ts3 = 0
           ts4 = 0
      waterGalPerHour = waterFlow()
     if waterGalPerHour != 0.0:
           ts6 = ts6()
           ts5 = ts5()
     else:
           ts5 = 0
           ts6 = 0
      ts5 = ts5()
      ts6 = ts6()
      print "Glycol flow =", glycolGalPerHour, "Gal/Hour"
      print('sensor1', ts1)
      print('sensor2', ts2)
      print('sensor3', ts3)
      print('sensor3', ts4)
      print "water flow =", waterGalPerHour, "Gal/Hour"
      print('sensor5', ts5)
      print('sensor6', ts6)
      print '---------------------------------'
      time.sleep(30)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to e14 Contributor

    Hi Priyam,

     

    Can I get your advice on programming a hall-effect flow meter with the Raspberry Pi?

     

    Ultimately I would like to log the water flow data over time and then calculate the total volume of each continuous stream (ie. if flow drops to 0 for x seconds then start a new total).

     

    As a start, I want to make sure that I can replicate your implementation. Will this (below) do the job?

     

        import os  
        import glob  
        import time  
        import RPi.GPIO as gpio  
          
        # Initializing GPIO ports  
        boardRevision = gpio.RPI_REVISION # Clearing previous gpio port settings  
        gpio.setmode(gpio.BCM) # Use real physical gpio port numbering  
         gpio.setup(25, gpio.IN, pull_up_down=gpio.PUD_UP) # setting pin 12 as pull up resistor for water flow meter  
          
          
        def waterFlow():  
          waterStartTime = int(time.time())  
          print 'water start time', waterStartTime  
          waterCount = 0  
          print 'water count', waterCount  
          while waterCount < 15 :  
               if gpio.input(25) == True:  
                    if gpio.input(25) == False:  
                         waterCount += 1  
                         print 'water count', waterCount  
          waterEndTime = int(time.time())  
          print 'water end time', waterEndTime  
          waterSecsPerLiters = waterEndTime - waterStartTime  
          waterLitersPerHour = (3600.0 / waterSecsPerLiters) * 30   
          return waterLitersPerHour  
          
          print "water flow =", waterLitersPerHour, "l/Hour"  
    
          print '---------------------------------'  
          time.sleep(30) 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to e14 Contributor

    no that was just another test program not working properly.

     

    the program below is the that i'm using at the moment.

    it first checks to see if there is a flow or not. if there is no flow then it returns zero. if there flow then it returns the instantaneous flow rate by looking at the pulses that the flow meter is putting out but if the flow stops before there is enough data to be able to calculate the flow then it will return zero. from there you can take those data points and plot them on excel and figure out the area under the curve to get your total flow from starting to stop and for how long it stopped also.

     

     

    import os # importing OS module just in-case needed 
    import time # Importing Time module
    import RPi.GPIO as gpio # Importing RPIO module as gpio
    
    
    # Initializing GPIO ports
    boardRevision = gpio.RPI_REVISION # Clearing previous gpio port settings
    gpio.setmode(gpio.BCM) # Use real physical gpio port numbering
    gpio.setup(25, gpio.IN, pull_up_down=gpio.PUD_UP) # setting pin 22 as pull up resistor
    
    
    def glycolFlow():
      glycolCurrentTime = int(time.time())
      glycolErrorStopTime = glycolCurrentTime + 1
      glycolTimingPulse = 0
      while glycolCurrentTime <= glycolErrorStopTime:
           if gpio.input(25) == True:
                if gpio.input(25) == False:
                     glycolTimingPulse += 1
                else:
                     glycolCurrentTime = int(time.time())
           else:
                glycolCurrentTime = int(time.time())
    
      if glycolTimingPulse != 0:
           glycolStartTime = int(time.time())
           print 'glycol start time', glycolStartTime
           waitTimerglycol = glycolStartTime + 2
           waitTimerCountglycol = int(time.time())
           glycolCount = 0
           print 'glycol count', glycolCount
           while (glycolCount < 15) and (waitTimerCountglycol <= waitTimerglycol) :
                if gpio.input(25) == True:
                     if gpio.input(25) == False:
                          glycolCount += 1
                          print 'glycol count', glycolCount
                          waitTimerglycol = int(time.time()) + 1
                     else:
                          waitTimerCountglycol = int(time.time())
                else:
                     waitTimerCountglycol = int(time.time())
          glycolEndTime = int(time.time())
           print 'glycol end time', glycolEndTime
           if glycolCount == 15:
                glycolSecsPerLiters = glycolEndTime - glycolStartTime
                glycolLitersPerHour = (3600.0 / glycolSecsPerLiters) * 30
                glycolGalPerHour = glycolLitersPerHour * 0.26417205235815
                return glycolGalPerHour
           else:
                glycolGalPerHour = 0
           return glycolGalPerHour
      else:
           glycolGalPerHour = 0
           return glycolGalPerHour
    
    while True:
      glycolGalPerHour = glycolFlow()
      print "glycol flow =", glycolGalPerHour, "Gal/Hour"

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube