Notifications
Clear all

Custom large LED speedometer - need help counting rpm sensor pulses

6 Posts
2 Users
0 Likes
681 Views
Garauld
(@garauld)
Member
Joined: 3 years ago
Posts: 4
Topic starter  

Hi everyone, I am somewhat of a newby playing with Python (and MicroPython) and could use some help on this new project.  I recently built a custom tracked vehicle (snowcat) and wish to add a speedometer on the dash.  I thought it would be cool to use a Pico to drive a series of 2.5" tall 7-segment digits mounted in a custom 3D-printed housing and lit up with a bunch of mini leds fed by the GPIO.  I have a PNP sensor ready to measure the driveshaft rpm.  I might see a max pulse of 381 Hz.  My plan was to have an interrupt break a 1/2 second sleep mode buried in a while True loop and add a count to the pulse totalizer.  After 1/2 sec, I would calc the speed based on the pulse count and light up the appropriate digit segments.  Sounds easy but I cannot get the pulse totalizer to work.  Is there a better way to do this?  Thanks in advance for any help on this.  I plan to offer the housing stl files afterwards in case someone else might like to build the display.

"If you can't be handsome, you may as well be handy" R.G.


   
Quote
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 

I'm a newbie like yourself, but I did watch a video where someone mentioned you could measure RPM using a hall sensor, e.g.:

https://www.instructables.com/RPM-Measurement-Using-Hall-Sensor-and-Arduino/

Note that I don't know what a PNP sensor is.


   
ReplyQuote
Garauld
(@garauld)
Member
Joined: 3 years ago
Posts: 4
Topic starter  

A PNP is a type of magnetic proximity sensor.  I just need to know how to accurately count the GPIO incoming voltage pulses over say, a 1/2 second. 
I had a global variable called "pulses" in my interrupt code that I planned to increment every interrupt.  Unfortunately, I get the error: "local variable referenced before assignment" whenever the interrupt occurred.

"If you can't be handsome, you may as well be handy" R.G.


   
ReplyQuote
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 

If you can post some code, we might be able to help. If the variable is global and is being used in the interrupt function, then you have to declare it as global in the function:

my_var = 3

def my_func():
global my_var;
print(my_var)

   
ReplyQuote
Garauld
(@garauld)
Member
Joined: 3 years ago
Posts: 4
Topic starter  

Thanks YurkshireLad - I got it working using your suggestion.  Is there a cleaner way to do this?

from machine import Pin 
import utime

pulse_in = Pin(28, Pin.IN, Pin.PULL_DOWN) #set up sensor input
scan_time = 1 #secs between display updates

def add_count():
global pulses
pulses += 1
print(pulses)

def int_handler(pin):
pulse_in.irq(handler=None)
print("input pulse occurred")
add_count()
pulse_in.irq(handler=int_handler)

pulse_in.irq(trigger=Pin.IRQ_RISING, handler=int_handler)

while True:
pulses = 0
utime.sleep(scan_time)
print("total pulses: " + str(pulses))
MPH = str(round(50 * pulses / 381 / scan_time, 1)) + " mph"
print(MPH)
break

 

This post was modified 3 years ago by Garauld

"If you can't be handsome, you may as well be handy" R.G.


   
ReplyQuote
Garauld
(@garauld)
Member
Joined: 3 years ago
Posts: 4
Topic starter  

Here's an image of the led assembly - the two large digits are 2.5" tall.  There will be a colored plastic "window" covering the leds - colors tbd.  I took one of the leds and ground a couple 45° flats on the end - this seemed to effectively disperse the light across the length of the segment pocket.  We'll see how well it works out once I get the parts printed.

image
This post was modified 3 years ago by Garauld

"If you can't be handsome, you may as well be handy" R.G.


   
ReplyQuote