12th Anniversary Sale 12% Off in July & August with the voucher code "SUMMER12" *excludes trade customers
Support Forum

Share your projects and post your questions

Register   or   Sign In
The Forum

Counting Pulses

The Expander Pi is an Analogue, Digital and RTC Expansion Interface for the Raspberry Pi

31/08/2020

Posted by:
harvey

harvey Avatar

Wondering if anyone knows there is any sample projects where someone has demonstrated counting pulses? I have a water flow device, purchased on Amazon ( https://www.amazon.com/dp/B07DLZH8P2/ref=dp_prsubs_1 ). Wondering what the simplest way to connect this to my project which when done will include monitoring temperature and pressure in addition to water flow.


On a side note, my son and I created a really nice temperature monitoring sample. We included calibrating a thermistor and plotting a graph that shows the curve of the voltage to temperature values are correct. We used a LittleFuse p/n USP 10978 and applied the Steinhart–Hart equation to prove our tests were accurate. Is this something I can post here for other users that might be trying to create a similar project?


Best,
Harvey

31/08/2020

Posted by:
andrew

andrew Avatar

Location:
United Kingdom

andrew Twitter  andrew Website  

Hello Harvey

The code below shows a simple counter program that will increment a counter variable every time pin 1 on the digital IO bus is pulled high to 5V.


from __future__ import absolute_import, division, print_function, \
unicode_literals
import time

try:
import ExpanderPi
except ImportError:
print("Failed to import ExpanderPi from python system path")
print("Importing from parent folder instead")
try:
import sys
sys.path.append('..')
import ExpanderPi
except ImportError:
raise ImportError(
"Failed to import library from parent folder")


def main():
'''
Main program function
'''
# Create an instance of the IO class called iobus.
iobus = ExpanderPi.IO()

# Create a counter variable
counter = 0

# Set all pins on the IO port A to be inputs with internal pull-ups disabled.

iobus.set_port_pullups(0, 0x00)
iobus.set_port_direction(0, 0xFF)

while True:

if (iobus.read_pin(1) == 1):

counter += 1 # increment counter

# wait for pin to go low again
while (iobus.read_pin(1)):
# sleep 200ms before checking the pin again
time.sleep(0.2)

# print the counter value to screen
print("Count: " + str(counter))

if __name__ == "__main__":
main()


You may need to add a 10K resistor between pin 1 and ground to pull the pin low between pulses, otherwise, it could be left in a floating state where it may cause false triggers.

Depending on how fast your flowmeter pulses you may need to decrease the time.sleep(0.2) value inside the while loop to allow more than 5 pulses per second.

You are welcome to post your temperature monitoring sample on this forum, I am sure other forum users will find it useful.

Sign in to post your reply


Note: documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view.
Download Adobe Acrobat Reader or other PDF reading software for your computer or mobile device.