90,000 rpm motor control

Hi All,

I have built a pulsed dc motor that currently runs up to 90,000 rpm. An Arduino Mega2560 controlling it. Immediately, I must confess that I'm not a programmer but I can muttle my way through. The software that I'm using and have slightly modified is open source [edit: actually software is GPLv3 Free Software as pointed out by justjed in a later post]. In fact the whole project is open source. I think that the code could be improved for speed if a real programmer took this on. Currently there is about a 200us delay between trigger input to processor output.

If possible I would like to add a lcd with keypad in order to see rpm and change parameters such as pulse delay and duration. I'm looking at this one: http://www.ebay.com/itm/SainSmart-1602-LCD-Keypad-Shield-Arduino-Duemilanove-UNO-MEGA-1280-2560-R3-/270887248179?pt=LH_DefaultDomain_0&hash=item3f12236133 .

My question is will the Arduino handle the additional overhead? I'm not as worried about the keypad input as the display overhead. I already have a ps2 keyboard attached and it works ok but of course if a display/keypad will work it would be a bonus to remove ps2 keyboard. I don't need to update the display (rpm/menu) frequently (every 1 second?). The rpm reading is not critical to operation but would be a nice built in feature. The code is available for anyone interested.

Thanks for any help. Jim

Consider a LCD that sits directly on the serial port. Then, messages are only a a Serial.print command away. With Arduino 1.0, the serial port is no longer blocking the CPU the way it used to. See Mark Sprouls shield description for what I mean. A similar shield can be bought at Unified Microsystems for $35. But I would prefer Marks shield because it's open source... see if he can sell you a PCB and you then populate it with the components of your choice.

I can't tell what pins the SainShield uses... it's not in the shieldslist either. HTH

That e-bay thing looks rather like the Phi-2 Interactive Shield Kit. Forum member liudr designed that, and builds them. So if this sort of thing matters to you, you can buy from him, or through a vendor, and support some people who contribute to this community. Plus, you'll get the library, and support. There's a demo here.

Thanks for the replies.

Constantin - From your response, presumably the serial lcd is both easier and faster than a parallel lcd with Arduino 1.0. Given that, the only problem I have with the serial lcd option is that for some reason my code won't compile with Arduino 1.0. It compiles fine with 0023. I haven't addressed that issue yet. Ultimately I will have to revisit that problem so I might as well deal with it sooner rather than later. Can't remember the errors I was getting off the top of my head. If I can't figure it out I'll start another thread.

justjed - As far as buying from a forum member... that's a good idea. It makes sense to support the support.

I'm not going to make a decision quite yet- not in that big of a hurry. I'll kick back and wait to see if there is any other input on the matter. I'd like to make the right purchase the first time. In the mean time I have plenty to do with the project.


One good reason to get the code to compile under Arduino 1.0 is that the serial port is non-blocking. Thus, your control system can presumably work happily away while the buffer is holding any keyboard inputs or the serial port is displaying outputs. I'm thinking of buying a shield like this just for debugging purposes - a inexpensive way to monitor multiple Arduinos communicating with each other.

Thanks for mentioning my shield justjed!

The OP contacted me via PM. If you want to reduce your Arduino load, here is a way: use a phi-panel serial LCD keypad or backpack (plug in a matrix keypad) on one of the MEGA's hardware serial ports. Then use the phi-panel's on board functions to render menus etc. Your arduino just needs to send the menu/list to the serial LCD and the choice will come back via serial, all the keypad handling and LCD scrolling through the menu is handled by the serial LCD on board processor.

Someone used my panel and made an HVAC project:


If your project is demanding, you can have a timer to only sense the keypad 20 times a second. Since the keypad talks to serial, its results are buffered against lost, so are info intended to display on the LCD buffered.

Timer code:

int keypad_last_read;
int update_time=50; // Update every 50 ms or 20 times a second
void setup()

void loop()
  //do your stuff
  if (millis()-keypad_last_read>update_time)
    if (Serial2.available()) key=Serial2.read());
    //Do something about the key press

You can do the same for displaying to serial LCD, which I think is non-blocking with arduino 1.0 so you're free to do your stuff once you call serial.print().

Here are links to three models, a backpack version (use a matrix keypad $2.25 on ebay), a compact integrated panel, a full size panel:




Let me know if you have other questions.

liudr - thanks for the quick and informative response. Funny that it came full circle back to you. As you know justjed pointed me in your direction but it didn't register that you were the same person I had independently found while researching serial v. parallel here on the forum. Looks like I'll be making a purchase from you but I still have to look over your suggestions and determine which choice makes sense to me.

Thanks to all for the help. Tonight I will once again try to get it to compile with Arduino 1.0. When that is complete I will take the plunge. :D

Either use a serial LCD and Arduino 1.0 so you have non-blocking write to the LCD, or run the motor control code in an ISR so that it can interrupt the LCD code. Without seeing the code, I can't say whether this second approach is practical or not.

dc42 - It does use an ISR.

For those interested I have attached the code that I am using. It is open source [edit: actually software is GPLv3 Free Software as pointed out by justjed in a later post] and I am not the author. It seems to work well. I have modified the code a little and have tinkered with making the delay adjustment automated according to rpm/period. The attached sketch relies on manual control of pulse duration and delay via keyboard, potentiometer or fixed with the code. I found it here: Alternative Energy research by Zero Fossil Fuel - Hydrogen, Solar, Wind

If anyone does look at it… Can you tell me why it doesn’t compile with Arduino 1.0?
My other question is can this code be improved? For example it uses digitalWrite commands. Will replacing them with digitalWriteFast improve the performance? Or other improvements.

freq_counter_v1_3.zip (24.5 KB)

If it uses an ISR to do the critical timing then you may be able to use a standard parallel LCD and the LiquidCrystal library.

To compile it under Arduino 1.0, at the very least you need to replace the line #include "WProgram.h" by #include "Arduino.h".

dc42 - already tried replacing the #include "WProgram.h" with #include "Arduino.h". The code I attached is in it's original form. My working copy is at home. If my memory serves me right I remember "thinking" the problem lies with one of the .h files included with the original sketch. I could be wrong about that. The project is not here where I am now. I will recheck the arduino.h, then I will try compiling the original again and then my working copy tonight. I read somewhere that someone needed both wprogram.h and arduino.h for their project to function with Arduino 1.0. Is this possible? Another thing to try tonight. Worse thing that could happen is my computer explodes.... :astonished:

Do believe I'm going to try the serial approach. liudr's lcd's look good. On-board micro-controller will help combined with your (dc42) suggestions- "Either use a serial LCD and Arduino 1.0 so you have non-blocking write to the LCD, or run the motor control code in an ISR". Doing both ISR and hopefully soon non-blocking write. I must also thank everyone else who have pretty much said the same about using serial. I'm heading in the correct direction now. Just need to get Arduino 1.0 to compile.


You've got a very well-written program to start with. Awesome! It's already sing ISR so you don't have to. After glancing through it, I thought the program handles everything in ISR and provides function calls you can use to set or read rpm, I think. You don't have to have serial LCD but getting MY serial LCD is just like getting a well-written program. You can focus on using it, but not how to use it. The panel essentially takes over these interface needs so you can focus on your project, not on how to make a menu :)

mnsman: Just need to get Arduino 1.0 to compile.

Why don't you post the first few error messages it provides, together with the lines of code that those messages refer to?

liudr - can’t take credit for the code as you probably read in my earlier post. Hats off to the person who wrote it and made it open source [edit: actually software is GPLv3 Free Software as pointed out by justjed in a later post]. It’s reassuring that you think it looks like good code. My question still stands. Can it be improved for speed? I’m not sure how the Arduino community handles open source code. In other words where should the code reside and how are improvements tracked etc? This is nice code for people like me who want to control pulsed dc motors rather than let them run freely. As far as I know the code has only been placed on the web site I put a link too. BTW I will be buying one of your lcd’s very soon. Thanks.

To all - I got Arduino 1.0 to compile. I didn’t do anything different that I know of. The problem occurred when I first added the Arduino. It must have been something stupid that I did because I didn’t have a clue what I was doing. There has been a big learning curve as everyone has experienced at some point. I don’t think that you’ve heard the last from me because there are still many features beyond the tach/menu that I want to add and after all I am new at this. For instance: Code automatic delay adjustment for rpm, Code skip pulse function; Code multiple pulses (per revolution) function; Code load control function. These would be nice features to add to an open source motor controller. I also plan on adding an H-bridge and an additional drive coil to the motor which will require additional coding. After it’s all said and done I hope the Arduino will handle what I’m throwing at it.

I’m well on my way to coding the automatic delay but it’s not perfected yet. That is what I’m currently working on. At least now I can use Arduino 1.0 and add a serial lcd.

Bottom line on the compile problem… just make sure the code attached above has the WProgram.h removed and Arduino.h added in it’s place as dc42 already mentioned.

For anyone really interested in the project I have a youtube channel mnsman1 http://www.youtube.com/user/mnsman1. My videos aren’t the best productions but at the very least I give details how to reproduce the motor and I will eventually add videos about the addition of the Arduino and the code. As I said previously the entire project is open source.

Thanks again everyone for the input.

Can it be improved for speed? I’m not sure how the Arduino community handles open source code. In other words where should the code reside and how are improvements tracked etc?

The typical method would be for the person who makes changes to deliver a patch to the maintainer. If you would supply a link to the location of the original, I can look at the license and perhaps advise further.

justjed http://alt-nrg.org/Muller.html link and attachment of code in my previous post on: February 03, 2012, 08:08:15 PM

BTW I've tried contacting the author and he doesn't seem to be very responsive at least to questions. His initial and only response to me was that it wouldn't work for my motor because it is too fast for the Arduino. Surprise. Further questions went unanswered.

mnsman: link and attachment of code in my previous post on: February 03, 2012, 08:08:15 PM

Oh, well I missed that -- was just looking at the body of your post.

Anyways, it's GPLv3. A better term to use would be Free Software, as opposed to "Open Source". While many people make little to no distinction between the two, there are differences, and anything published under the GPL is "Free Software".

I advise that you read the entire license: http://www.gnu.org/copyleft/gpl.html

If the author will not accept patches, then there's little that you can do, except maintain your own fork.

As far as the original author not responding to questions, that's understandable. And a small project like that won't have a support forum either, though maybe the author is on some Arduino forum, somewhere. But determining who that is will be problematic, because the .pde file doesn't do a good job of identification. I note that there is no Copyright notice, though the line at the top referencing ZeroFossilFuel might be intended as such, and the author just forgot to include the word "Copyright". You can do a whois lookup on hydrtec.com, and see it registered to someone in Great Britain, but that's not especially informative.

Did you specifically e-mail saying that you wished to submit a patch? A patchfile can be very simple: http://stephenjungels.com/jungels.net/articles/diff-patch-ten-minutes.html -- scroll down to 'Creating patches with diff'.

If your changes result in the code being not useful in the way the original author is using it, then I'm not surprised at a patch being rejected. In this case, a fork is the only thing to do.

Justjed - Thanks for that clarification. Somehow I got it in my head that it was "open source" and didn't distinguish it from free. It looks like a fork to me. No patch submitted. Attempted to ask a few uninformed questions. Understandable he didn't answer. Just as well it forced me to try it despite him denying it would work. Furthermore it forced me to study it and make my own changes. Now I'm learning cpp and the arduino. It's all good. Perhaps in the future if I make meaningful changes that may benefit others I will email it to the author and let him do as he pleases. He's probably already developed it further because he has a pulsed dc motor of his own on his youtube channel.