Reputation: 13
I am working on a project in which I am required to measure the RPM of wheel using a hall effect sensor and a Raspberry Pi. I have developed a script to do so but it does not give me required result. If a magnet comes near the sensor it directly shows the high value or else it shows 0. I want a script that will show me results like 40,39,38,36,9,8,0 every second just like a bike speedometer.
What should I do?
Here is the script that I have made.
import RPi.GPIO as GPIO
import time
from time import sleep
import datetime
j=10000
sensor=12
ts=datetime.time()
w=0
new_time=0
old_time=0
temp=0
GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensor,GPIO.IN,pull_up_down=GPIO.PUD_UP)
while j:
if (GPIO.input(12)== 0):
new_time=time.time()
old_time=temp
delta_t=new_time-old_time
temp=new_time
w=60/delta_t
v=0.5 * w * 0.10472 * 3.6
print (v)
time.sleep(0.1)
else:
time.sleep(1)
print("0")
j=j-1
These are the results that I am getting
7.73038658487e-09
0
0
5.14198236739
85.7996578022
88.3574855171
88.6053761182
0
9.71547048724
86.4257204462
0
9.57686374353
0
0
0
3.46828868213
86.5939003971
87.7296673227
85.2723314196
87.1933291677
86.5891584273
85.6995234282
86.6861559057
85.5173717138
86.9547003788
87.3698228295
87.2335755975
0
9.6387741631
0
0
0
3.4536179304
82.6103044073
83.5581939399
83.8193788137
82.5174720536
84.0056662004
82.471707599
83.8201193552
86.8997440944
82.6851820147
0
Upvotes: 0
Views: 1833
Reputation: 494
You will get a lot of 0 because you print a 0 in the else.
I don't know if python is the best language to do this control, it depends how fast your wheel is and how long stay GPIO.input(12)== 0
.
If it's too fast you will lose lots of revs.
May be you have to do an average of the last N measures. Or, instead of look how long takes the wheel to do a complete rev, measure how many revs has done in the last N seconds.
May be you get enter in the GPIO.input(12) == 0
case for the same rev because GPIO.input(12) stais 0 too long. To count as a new rev has to change GPIO state:
last_state = 1
while j:
if (GPIO.input(12) == last_state)
last_state = GPIO.input(12)
if (GPIO.input(12)== 0):
new_time=time.time()
old_time=temp
delta_t=new_time-old_time
temp=new_time
w=60/delta_t
v=0.5 * w * 0.10472 * 3.6
print (v)
time.sleep(0.1)
else:
time.sleep(1)
j=j-1
I'm not a python programmer, you'll have to check time units and grammar:
revs = 0
last_state = 1
end_time = time.time() + 10
while time.time() < end_time:
# count revs:
if (GPIO.input(12) == last_state)
last_state = GPIO.input(12)
if (GPIO.input(12) == 0):
revs = revs + 1;
# print revs every 0.5 secs:
if (time.time() - old_time > 0.5)
delta_t = time.time() - old_time
old_time = time.time()
w = 60 / delta_t
v = revs * 0.5 * w * 0.10472 * 3.6
print(v)
revs = 0
else
time.sleep(0.01)
Upvotes: 1