News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Simple Pwm Detection?

Started by Maxi, Jun 14, 2025, 06:57 AM

Previous topic - Next topic

ken_k

#20
Thoughts only.
One pulse per wheel revolution is quite a low resolution for detecting slow speeds and stopped, the time windows have to be annoyingly large.
Could the sensor be moved to a drive shaft with a different gear ratio?
4 to 1 would be substantial improvement.
Magnets stuck to a drive shaft and a hall sensor can be a solution in some cases.
Doppler or radar/ultrasonic module looking at a drive shaft?
Hacking into the vehicles own systems.

Good thing its not powered by a hot bulb two stroke. Move to 3 minutes..
https://www.youtube.com/watch?v=pYeEotP_c3M
BTW my dads sister was driving one of these in WW2, dad was driving another tractor on the other side of the field. Dad heard his sister almost stall the bulldog tractor, she stopped the machine, dad could tell by the sound the engine was running backwards he unhooked his plough and started to race across the field towards her... too late she popped it onto gear and backed the tractor into the plough, not good.

Fanie

What an awesome tractor !  And it will last forever at that low rpm too.

Four magnets stuck to a shaft N S N S can easily detect movement very accurately if you use a linear hall sensor... where the most inaccurate position will be TDC of the N or S of the magnets.  That is why you need a 2nd hall sensor at 90 deg so if the one hall sensor reaches a high (or low) in the sine wave, you switch to reading the 2nd linear sensor that will be in the slope of the sine wave.  It is used like this in electric motors (like wheel motors) and shaft encoders.

Stephen Moss

It seem to me that @Maxi has tried to describe what they are doing in several different ways, as a speed sensor, a wheel sensor and a doorbell, which could be confusing the issue as I think they are all the same thing.

I think they have a sensor that gives either a positive or negative going pulse, with an output of either 0V or 5V (not 0-5 as that suggest variable voltage), although it would help if they would clarify if the output is a more digital 0V & 5V or more analogue single infinitely variable between 0V and 5V as that will help determine what may be the best solution.
I also think the reference to PWM is probably incorrect as given the sensor description I think the high to low time ratio is constant, and proportional to the overall time (frequency as @Maxi put it) which changes with speed.

As other have suggest the CCP module would probably be one solution although not one I have experience of and may be to complicate for @Maxi to understand as they stated they are looking for a simple solution.

Thus assuming the sensor output is digital the other already offered solution of using the IOC (interrupt on change) or two timer methods already suggested but I think from their comments that @Maxi may not have a clear understanding of how the examples given work so perhaps this will help...

1) Set a Timer to reach its full count at a specific time period (say 2 second), that will set the relevant Timer Interrupt flag TMRxIF and enable the corresponding Timer interrupt bit by setting the relevant TMRxIE interrupt enable bit (where x = timer number).

2a) If the sensor output is used as the clock input of another timer, when the first Timer creates an interrupt your code will jump to your Interrupt Service Routine (ISr) where it should read the count of the second timer (increment by every pulse). If the result is greater than 0 then the the vehicle is/has been moving/bell been pressed, if the result = 0 then the vehicle has stopped/no bell press.
Set the value of the second timer to 0 by writing to its register (i.e., Timer2 = 0), clear the interrupt of the first timer by clearing its interrupt bit (i.e., TMR1IF = 0)

2b) If the sensor output is instead connected to a Port pin that has IOC enabled, this is similar to clocking the Timer except that in in you ISr you need to check which interrupt was fired (which interrupt flag = 1, could be one or both so check IOC first).
If it was the IOC interrupt, increment a variable (equivalent to the incrementing the second timer count) and clear the IOC interrput flag.
If it the the Timer check the value of the Variable, if it is 0 then the vehicle has stopped/no bell press, otherwise it is/has been moving. Reset the variable value to 0, and clear the timer interrupt flag.

The difficulty will be in setting the timer interrupt interval if you are looking to detect low RPM, particularly if you are using a fast clock as the PIC may not have sufficient Pre and Post scallers to make the Timer interval long enough.

An even simpler solution than using Timers if you struggle with them and/or the program is quite simple, primarily only needs to indicate whether or not there is movement due to the 2 second delay in doing anything else due to the For-Next loop would be something like...
Dim Time as Word
Dim Count as Byte
Dim Moving as Bit   'Bolean indication if movement has stopped
Dim True as Bit = 1
Dim False as Bit = 0

Main:
For Time = 0 to 2000       'Check sensor state every millisecond for a period of approximately 2 seconds
   If GetPin (insert number) = 1 then
      Inc Count
   End If

   DelayMs 1   'Wait for 1 MillSecond
Next

If Count = 0 then
      Moving = False  'Stopped
   Else
      Moving = True   'Moving/has moved
      Count = 0       'Reset the count
End IF

Goto Main

Replace setting the State of the Moving variable with any applicable code. 

Maxi

Thank you very much, sorry I can write a little late.
I don't have perfect English and my biggest mistake in this story is that I wrote '0-5 volt pwm' confused people, the correct one should be '5 volt level pwm signal?'

I improved a few points thanks to your answers,
if the wheel does not turn for 5 seconds, the relay will now pull.
Now we make sure that the vehicle comes to a complete stop.
Since the details of the signal in milliseconds are not important at all, I did not want to write information on that side because we do not calculate speed.

But I saw that everyone was very helpful 'really'. As a result, I used @top204 and @Pepe's codes and as I said, I waited 5 seconds, so the problem of the reading range of the single sensor on the wheel was solved.

@Stephen Moss's code is actually the same code without interrupt and all of them work perfectly in proteus. Now I started to make the physical circuit.
Thank you to everyone who wrote and responded