# Speed counter

#### Mejlby

##### Member
speed counts

Hello everyone
is there anyone who can help me with a program to count the speed of a fan. i want to use 2 pcs 7 segment to read speed. the speed will be a maximum of 50 rpm
i hope someone has a good idea. see attached drawing

every time the fan has turned a rotation, the hall sensor will send a 5V signal and the faster the fan is running around I want to count the speed per minute

Regard
Mejlby

#### Attachments

• 32.1 KB Views: 17
Last edited:

#### papaof2

##### Senior Member
Perhaps start with an accurate source of time, such as the DS1307 or similar? To know how many revolutions per minute, you need an accurate minute to frame your revolution count. You will need a PICAXE that supports I2C to talk with the DS1307 and most other clock chips. The DS1307 keeps time to about 2 seconds per day so should be adequate for your needs and it's under \$5US.

Use an interrupt to report the magnet passing the Hall sensor. If still within the same minute, increment the count. Check the time each second (there's also a pulse every second from the DS1307 if you choose to use it) and at the end of the minute display the recorded count and zero the counter for the next minute.

If you want more frequent speed updates than once a minute, you'll take the count over a known period, perhaps 10 seconds, then calculate the RPM from that count: revolutions in 10 seconds * 6 = revolutions in 60 seconds. This will not be as accurate as the once a minute updates.

If you want better accuracy, use multiple magnets to have multiple pulses each revolution and do the math on that number. 6 pulses per revolution would greatly improve that accuracy of the short term RPM display.

#### Eng460

##### Well-known member
Picaxe has two functions, either of which can be used to measure the speed of the fan..

Look up “Count” and ”Pulsin” in section 2 of the manuals. You will quickly see that both of these operate extremely fast, and neither require the complexity of including a clock.

Then you need some maths to convert the output of your chosen function to rpm. Look at the early section of section 2 on Picaxe maths. It has some wrinkles, but it will,get there.

Your Hall effect sensor will make an adequate speed sensor. They are really fast, even used to time ignition pulses in IC engines, but 50 rpm is really really slow for a Picaxe. I believe you will need more pulses from each revolution, or Picaxe just gives up and assumes the device is stopped before the next pulse arrives. However, a magnet on each blade might just work, and will give more options to make sure the fan stays balanced. Attaching the magnets will be your biggest challenge. You will really need more than ten and preferably a hundred pulses for a measurement of any accuracy so you will have to include some form of rolling averaging to get any accuracy from the number of pulses Picaxe can count before getting bored.

You will get plenty of help here with the maths side of the programme. It is simple enough, but the integer maths limitations make it a bit tricky at first. Have a go, then ask a more specific question. Use the simulation to see the answers you are getting.

Initially, for the display, just use your computer and learn to use “sertxd” with the programming jack still connected until you are happy with the results.

I have covered the path for the easy bits. Others will have to help with displaying the speed on 7 segment displays. I have not tried that yet, and have not got my head around the procedure. There have been recent threads that will help if you search back using the search function.

By the way, to give you an idea of the speed capability of Picaxe, try the little example program in the manual for “count“ to count how many times a switch bounces before it finally makes contact. You will be amazed at the result. And you will suddenly understand switch de-bouncing.

Eng460

#### datasmith

##### Well-known member
While everyone else is looking at the electronics, I'm thinking that you'll need a counterweight on the fan.

papaof2's comment on putting a magnet on each blade for accuracy would certainly provide that as a dual purpose.

On the 6-blade fan in your diagram you could split the difference and put a magnet on every other blade.

#### Eng460

##### Well-known member
I just checked the maximum time period for “count”. It is about 65.5 seconds, (65535 mS) so at 0.8 seconds per rev, you could get about 80 pulses each execution period of about 65 seconds, and likely to cause balance problems. Also slow to update as only one count per minute.

Much better would be more magnets. Two would give 160 pulses, and six would be better again, providing you can attach them well and maintain balance.

A higher number means the error caused by +/-1 count is less significant. Three as suggested by datasmith would give a good number of pulses and slightly easier to implement successfully.

Eng460

#### rq3

##### Senior Member
speed counts

Hello everyone
is there anyone who can help me with a program to count the speed of a fan. i want to use 2 pcs 7 segment to read speed. the speed will be a maximum of 50 rpm
i hope someone has a good idea. see attached drawing

every time the fan has turned a rotation, the hall sensor will send a 5V signal and the faster the fan is running around I want to count the speed per minute

Regard
Mejlby
Does it have to be a magnet and hall effect sensor? This looks like a perfect job for an optical switch, with the fan blades chopping the beam from an LED to trigger a phototransistor. Much simpler, and no balancing issues for the fan.

#### AllyCat

##### Senior Member
Hi,

COUNT has a number of disadvantages: You need to wait a long time (e.g. a minute) to get a measurement within 1 rpm, or add several magnets, and the PICaxe can't do anything else whilst it's counting (without going to the complication of multi-tasking).

IMHO it's much better to use PULSIN; at worst you may need to measure the "pulse" and "gap" times separately to find the time of a single full revolution. Then the rpm is simply found by dividing a "constant" by the time of one revolution. Hint: you'll probably want the time in ms. This has been discussed previously on the forum, but the search isn't great.

Do you have a particular PICaxe in mind? A 20M2 (or higher) could drive all the display segments separately. Or multiplexing can save some pins (and probably wiring). Again there's been plenty of discussion on the forum, but just ask when you decide the method(s) you want to use.

EDIT: Actually, your fan is turning so slowly that PULSIN will probably often "timeout". So you may need to count the number timeouts (to add to the reported time when it doesn't timeout), or simply measure the time of one revolution using a software polling loop.

Cheers, Alan.

Last edited:

#### hippy

##### Senior Member
i want to use 2 pcs 7 segment to read speed. the speed will be a maximum of 50 rpm
What would be the slowest speed of the fan ? At 1 RPM you would only be able to update its speed once ever minute with just one sensor, at 12 RPM your update rate would not be any better than every 5 seconds. And it would be half that if you can't start timing before it comes round again.

This project has all the issues which measuring bicycle speed has, albeit that you are only measuring RPM and not converting that to traditional speed, distance over time. But all the principles are the same.

With a maximum 50 RPM you are going to hit problems with both COUNT and PULSIN, would be better off letting a variable increment and wait for a polled signal or interrupt, then process that variable count.

#### Eng460

##### Well-known member
We have not heard back from the original poster, so it is hard to know which way to go to be helpful.

For what it’s worth, personally I do prefer pulsin which I use to detect the speed of small engines, so far, up to 2000 rpm. And yes, I use a photo interupter device which has both the LED light source and a photo detector transistor in the one neat package, not magnets and Hall detector, for reasons of balance, and to not impose any load on the engine. However the gap between the light source and detector is only about 3 mm, so will not fit the fan blades. It needs a flat disk into which I have cut 4 equal blades to interrupt the light. Not to easy to attach to a small fan, but it might be possible to make up something with separate components. Some care would need to be taken to exclude daylight etc. Slow fan speeds are catered for by more gaps in the disc. Even an O8M2 can easily measure the speed more than once within each revolution, but would have trouble driving the seven segment displays. Perhaps use two devices.

I can elaborate on my setup of people are interested, but hoped to see where the original poster is up to before too much detail.

Eng460