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 707 subscribers
  • Views 3387 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
Parents
  • 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
Reply
  • 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
Children
  • 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
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to e14 Contributor

    Great, I'll look forward to testing.

     

    It's a bit worrying for accuracy when you say that:

    if the flow stops before it can calculate flow then it will return zero

    Is this a question of processing speed? If so then (from what I've read) C++ may be more suitable.

    • 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

    well what is happening is that i'm actually using two different flow meters at the same time so i'm altering between the two to get the instantaneous speed of them because that's what i need to do the calculations that i require. but if you need to do something else then you can modify the program to do what you wish.

     

    as long as you rate of flow is somewhere within the capacity of the flow meter that you are using then you should not have any issue.

    • 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