NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Project Help and Ideas » Engine speed control. n00b!

June 02, 2009
by dangraham
dangraham's Avatar

I have assembled the kit and have temp-sensor & servo-squirter working well.

What I need to achieve is reading the engine RPM accurately, possibly using a Hall Effect sensor on the fly wheel. The engine is part of a 5KW AC generator set; the old mechanical governor is very inadequate! I was considering using the AC frequency as rpm signal but thought this unwise due to the 2 Hz per rpm. I am thinking of using the Hall Effect sensor with 32 small magnets placed equally around the 16” diameter fly wheel. This would give 32 pulses per rpm and should allow better accuracy.

I am starting with the servosquirter and tempsensor projects and building from here. I will be using the servo connected to the fuel control of the engine. I need to maintain the engine speed to 1500rpm or 800 pulses per second on the hall sensor.

I need pointing in the right direction on how to create the code to read the digital input from the hall sensor and display this to lcd. While also controlling the servo to increase or decrease fuel to the engine rather than the serial port.

I don’t think I will have a problem assembling the circuit or hardware; the main problem is the code! I am still learning C and need a few pointers.

One other issue an am having is how to integrate the tempsensor code with the servosquirter code so I also have the temperature. I am getting errors compiling the code and not sure why.

Do you have any further info on the maglev project? I think a lot of the systems I am trying to produce will be covered by this project and would help a lot.

Please let me know if you need any other info.

Great kit by the way and good instructions, if I can get this project figured out I will be so happy with this!

June 04, 2009
by dangraham
dangraham's Avatar

Ok how about starting with the temp sensor input moving the servo. I have not decided on the hall sensor yet because I am trying to work out the calcultations and a bit lost.

Can someone explain to me how I would go about using the tempsensor input to move the servo?


June 04, 2009
by AVRGirl
AVRGirl's Avatar

Hi dangraham,

I don't know what layout your project has, but have you thought of measuring your rpm optically. Essentially you have a disk with cutouts to allow light through. As the disk spins, you can count the pulses of light. This would weigh less and you could 'cut' as many holes as you like. If you are going to be adding a disc to your engine/generator this would be another option for you.

Again, without seeing your layout - you may also be able to use your hall effect device directly off a cog in the gear box.

Keep us posted!

June 05, 2009
by mcai8sh4
mcai8sh4's Avatar

Like AVRGirl mentioned - an optical sensor sounds like a good idea - it might be quite easy too. I think the old style mice (ones with the ball) have these little sensors in them. Rip one apart and see if you could salvage one.

I don't know this from experience, but I'm sure I read about it somewhere... I'll see if I can find a link.

June 05, 2009
by mcai8sh4
mcai8sh4's Avatar

Have a quick read at THIS it may be of help.

June 05, 2009
by dangraham
dangraham's Avatar

Hey guys, thanks for the reply. I have considered optical sensor as my other choice. I thought it would be easier / more reliable to stick magnets to the fly wheel, rather than the slotted disc. I was also considering painting the surface of the fly wheel black and using segments for sliver foil then the light and sensor could just face the fly wheel, do you think this would work?

I have just hacked an old HP ball mouse and can see the 3x optical sensors. Now all I need to do is figure out how to read this with my nerdkit??? Do I follow the principle of this

For those that are interested in my project here are some stock images of the single cylinder engine (Petter AVA1 Series 2) and cheap Chinese ST5 alternator (I had to completely rewire all of the internals, on our first two test runs the wires on the rectifier rattled loose! got to love the dodgy Chinese workmanship!)

If you look at the picture of the engine you will notice the injector pump, this has a spring and is connected to the mechanical governor. This is the part i want to digitally control. In its standard form the engine is not good at maintaining a constant 1500 rpm with this governor.

I will get some pics of my setup over the weekend and post them up so you can see my efforts. It has been disassembled at the moment to relocate.

Can someone explain the steps I need to follow to create my program? Once I have the sensor hooked up I need the program to maintain a speed (1500 rpm) by connecting the servo to the fuel pump.

There will be future expansion required, I need a start-up routine. This would involve holding the decompression valve open. Cranking the engine from a brushed PM DC motor to a set speed then closing the decompression valve and monitoring speed etc.

Thanks for the help guys; it is nice to be in a friendly forum where your projects can grow. Not like some of the other forums where people are just not interested or want you to do things a different way.


June 05, 2009
by mcai8sh4
mcai8sh4's Avatar

Dan, this sounds like an interresting little project.

I can't really help you with getting the sensor to read yet - I'll continue to think but I don't know enough to really be of any use.

I've just been having a think about how this is going to work from a programming point of view. I'm new to all this so bare with me...

Say your using hall effect (or whatever) with 2 magnets - at 1500 RPM you would get 50 readings every second (or 1 every 0.02 sec). Maybe you could setup an interrupt for every time the pin changed state, AND used the example from the real time clock to measure 100th sec. Then when the input pin changes increase a counter. Then every second compare the counter variable against '50' (for desired speed) if it's higher reduce speed, if lower increase speed, reset counter.

I think that would work, but I'm sure a Nerdspert(TM) will be able to give you a more elegant solution.

Keep us all posted on how you get on.

June 05, 2009
by dangraham
dangraham's Avatar

Yeah working out the timings, let alone implementing them is hurting my brain! I was also looking at using the AC HZ (needs to be 50Hz) and working from that. The only problem I see is the frequency is too low to be accurate in detecting speed change. I have a 16" diameter fly wheel and worked out that I can fit 32 small magnets to the wheel. This would give me 800 readings per second and should allow for higher accuracy. I think that by using the small LED size optical sensor and source I would be able to paint / foil the fly wheel and double this to 1600 readings per second. Maybe even triple it; I will have to play with the sensor.

I am going to have a look at the real time clock and code, see if i can get this one working! Hopefully this will help my brain ache!

I still haven't managed to merge the tempsensor & servo squirter code, not sure what I am missing with that. They both work separately but when I try to merge they don't. I will keep trying with this one.

Thanks for the help.

June 19, 2009
by dangraham
dangraham's Avatar

Help needed with sensor. I have decided to paint the flywheel and use reflective sensor. here

In the data sheet it mentions The pulse width is 10 μs maximum with a frequency of 100Hz. I am not sure if this will work? My flywheel has a diameter of 124CM I was thinking of painting 1cm black lines alternating. this would give 62 steps per revolution. seeing as I am aiming for 1500rpm this would be 25*62=1550. will this be too fast for this type of sensor? if so do you think the hall efect would be better?

These sensors are only a few quid so I can experiment but would rather get it right first time. I am not sure but I think the maximum the sensor can handle is 100hz and with my setup I would be creating 1550hz? can anyone confirm this?

thanks guys.

July 09, 2009
by dangraham
dangraham's Avatar

Ok, so after much pondering and head scratching I have made “some” progress. I was looking at the optical sensors as suggested but the resolution is not high enough. The Omron optical sensors only go up to around 100hz and I need 1-2khz (I think higher frequency will give higher accuracy providing I can work it all out)

I looked at the mouse etc (thanks mcai8sh4) but don’t need quadrature encoding because I will only ever go one way, trying to keep things simple for now! So all I need is the pulse cont / speed. So I have decided to go with this little hall sensor Sensor I have also purchased 200 10mm disc magnets. I glued them to an old PC fan for a test setup and it works! I am just as amazed. I also have a new meter that displays Hz so I can see what it’s running at around 106Hz.

Please see pictures here to give you an idea of the setup. Image1 Image2 Image3 Image4 Image5 Image6

Ok so now I will start to glue my 124 magnets to the fly wheel, and position my sensor so it is all working on the generator. The magnets will be placed north/south. Because the hall sensor is a latching switch one pole with switch it on the opposing pole will switch it off. So for one revolution I will have 62 pulses. My target RPM is 1500 so that should work out to 1550 Hz is my target frequency. I just hope the hall switch can keep up because it doesn’t mention max Hz in the datasheet? I am quite confident I will have the hardware assembled in a couple of weeks.

Now on to the hard part, how to I start to code this?? What I want to happen is the pulse count is recorded and the servo compare value is altered accordingly. This is the very basics and there are a lot of other functions that will need to be added in future like real-time clock for auto start-up / shutdown etc. I have been looking at the PID control and think I can get away with just a PI controller but I have no idea how to start assembling the code to run this. Any pointers would be greatly received.

Also how do I read the input from the hall sensor and store the amount of pulses per sampling time? Pull ups, pull downs floating point things? Thanks guys you are helping so much to work through this. Nerdkits all the way!

July 29, 2009
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Dan,

Its good to see you have gotten this far with your project. It looks like you are getting along quite well, so I'm going to offer a few suggestions as you requested.

Your first step should be to see what kind of signal you are getting out of your hall effect sensor when you spin the wheel with the magnets over it. The best way to do this is with an oscilloscope, but failing that you can likely hook it straight into the ADC on your chip and print out the values to the LCD or the serial port. If I were doing this I would likely print out the values to the serial port and make a quick python scrip to graph the results. If your hall effect sensor is outputting a signal large enough for the ADC to read you should be able to watch the spike as a magnet moves over your sensor. It is more likely however, that the signal coming out of your sensor is too small for it to be picked up by the ADC on the chip, after all it only picks up about 5mV per division. You will likely need to amplify the signal. Check out the sound meter tutorial for an example of a single transistor amplifier you can build.

Once you have a signal you can detect on the ADC it becomes a matter of detecting when a magnet passes over your sensor, this should be a matter of thresholding the value from your hall effect sensor. Just detect when it spikes over a certain value. You then probably want to mix parts of the Real Time Clock in so you can keep track of the real time between spikes.

At that point you will be able to figure out how fast the thing is spinning, then you can start trying to control the speed of the motor. You are probably going to run into some roadblocks getting all the code and interrupts to run fast enough, but figuring those out will be most of the fun!

Let us know how you get along with the project.


Post a Reply

Please log in to post a reply.

Did you know that Pulse Width Modulation (PWM) can be used to control the speed of a motor digitally? Learn more...