The first method is using "pre-defined interval".
The second method is using "calculated interval".

Before we dive straight into the code let me show you the setup of this project. What we need are RaspberryPi, hall effect sensor or reed switch and small magnet. You can use either one of the sensor and the code would be pretty much the same.
The hall effect sensor that I'm using is of Unipolar type. Depending on how you setup the magnet on the spinning mechanism, it will decide which type of hall sensor is suitable for you. Unipolar sensor can only get triggered on one side of the sensor. Bipolar sensor has the ability to detect magnet in both directions.

Reed sensor is a much simpler device to use for this project . I will show you how to use both sensors.
As this project is just a prototype for the wheel of our motorcycle/car, I'm using a cpu fan as a replacement for the real wheel.
The magnet is mounted on the flat surface of the fan.

Hall effect sensor has 3 pins or legs. There are VCC, Ground and Output.
Connect the VCC to 3.3V . (correction! I wrote 5V earlier)
Ground as usual.
And output to any GPIO that you wish. If everything is connected, you are good to go.

Using calculated interval method is better and more precise than pre-defined interval.
Here, we make use of the time.time() module in Python to precisely measure time duration of each interrupt interval.
We are using add_event_detect() to take advantage of the GPIO pin interrupt event handler.
Here we use one callback function called "calculate_elapse(channel)". This function will get called anytime interrupt event happens.

TRY to grab this concept in order to understand this code.

We are using ONE magnet mounted on one of the fan blade and ONE hall effect sensor
mounted on a static surface on our fan chassis.

Therefore, we could come up with an assumption that 1 complete rotation will be made whenever 1 interrupt event occurs.

From that assumption, we could determine the time duration of 1 complete rotation in our callback function.

So, lets take a look at the callback function in the code. It is named "calculate_elapse(channel)". In that function, we have
3 main global variables. Global variables are made so that their values can be used outside that local function which
in our case, our local variable is the callback function named "calculate_elapse(channel)". The globals are:

- pulse
- start_timer
- elapse

1) pulse : a counter variable used to count the number of interrupts. it can also be used to calculate total distance travelled
by the wheel.
2) start_timer : a time variable that stores the initial timestamp before an interrupt occurs and stores the current timestamp
after the interrupt has occurred!
3) elapse : time duration of 1 complete rotation by subtracting the timestamp during interrupt instance with start_timer which holds
the initial timestamp before that interrupt occurred.

After we have calculated the elapse, we immediately set our new start_timer with the current timestamp so that it can be used to
calculate our next elapse value if new interrupt occurs.

Now that we have our global elapse value, we can move on to next function which is "calculate_speed(r_cm)". this function will be called
in our main loop. This function takes in one parameter which is the radius of wheel in centimeter (you could use meter if you wish).
As the global elapse value is now accessible in this function, we can calculate the speed of the wheel using simple mathematical
formula.

First, we have to make sure that our elapse value is not Zero to avoid DivisionByZero error.

Round Per Minute (rpm) can be calculated using : rpm = 1/elapse * 60
where 1 is referring to 1 complete rotation and 60 is referring to 60 seconds.

Yazidapple wrote:Hall effect sensor has 3 pins or legs. There are VCC, Ground and Output.
Connect the VCC to 5V .
Ground as usual.
And output to any GPIO that you wish. If everything is connected, you are good to go.

This is ok only if the hall has a OC output. If it's a RC output you can damage your pi powering it with 5V

Here, we make use of the time.time() module in Python to precisely measure time duration of each interrupt interval.

probably if you want to "precisely measure time duration of each interrupt interval" you should use pigpio.
Pigpio timestamps interrupts when they occurr, in your code you use the (not so) precise python time.time() when the callback is executed.

Yazidapple wrote:Hall effect sensor has 3 pins or legs. There are VCC, Ground and Output.
Connect the VCC to 5V .
Ground as usual.
And output to any GPIO that you wish. If everything is connected, you are good to go.

This is ok only if the hall has a OC output. If it's a RC output you can damage your pi powering it with 5V

Here, we make use of the time.time() module in Python to precisely measure time duration of each interrupt interval.

probably if you want to "precisely measure time duration of each interrupt interval" you should use pigpio.
Pigpio timestamps interrupts when they occurr, in your code you use the (not so) precise python time.time() when the callback is executed.

thanks for the feedback bro. i'll try to improve this code and the setup! still learning anyway

the accuracy is quite acceptable for small and simple project where you just wanna create a simple speedometer to calculate the speed of your bike / motobike. as mentioned our friend above, pigpio method is the most accurate implementation.

gurudattakr123 wrote:How can we change this code so that rpm and kmph must come back to 0 when wheel is not rotating?

well probably the program works based on callback, so if no signal is received, no calculation is done.
You can try playing with pigpio's watchdogshttp://abyz.co.uk/rpi/pigpio/python.html#set_watchdog
you should setup, in the callback, a watchdog on the GPIO so that if no callback is called within the timeout, the watchdog will call it and you'll return 0
Obviously in every callback you should cancel the previous watchdog and setup the new one.

The calculation methods I can comprehend,
however........ I want to use a ble sensor instead of an GPIO sensor for my project (I have a Major dislike towards Cords when it can be done Wireless).
I get data from my ble sensor but can't use it nor calculate the RPM from it.
The point for me is:
I have my sensor mounted Under my bike pedal, it sends data to my Rpi3 on my bikes handlebar.
My Rpi3 has a Blinkt! hat on it and I want it to respond to my pedling stance.
example:
20-45 Rpm on my pedal/crank -->> Blinkt! goes Red flashing
46-60 Rpm on my pedal/crank -->> Blinkt! goes Green flashing.
60-90 Rpm on my pedal/crank -->> Blinkt! goes Blue flashing

I wish to use it for a Constant stance in biking.

can you please help me out with this?
I Really don't know where y missing link is.

thnx in advance.

Life is not just "good' when you open your eyes, it's Really GOOD when you realize you have another day to LIVE!

The calculation methods I can comprehend,
however........ I want to use a ble sensor instead of an GPIO sensor for my project (I have a Major dislike towards Cords when it can be done Wireless).
I get data from my ble sensor but can't use it nor calculate the RPM from it.
The point for me is:
I have my sensor mounted Under my bike pedal, it sends data to my Rpi3 on my bikes handlebar.
My Rpi3 has a Blinkt! hat on it and I want it to respond to my pedling stance.
example:
20-45 Rpm on my pedal/crank -->> Blinkt! goes Red flashing
46-60 Rpm on my pedal/crank -->> Blinkt! goes Green flashing.
60-90 Rpm on my pedal/crank -->> Blinkt! goes Blue flashing

I wish to use it for a Constant stance in biking.

can you please help me out with this?
I Really don't know where y missing link is.

thnx in advance.

what kind of BLE sensor are you using? is your Pi receiving any data from it? mind to elaborate further?