Arduino Engine Control Unit and Stellaris Launchpad LM4F120 Engine Simulator

The ECU which I am developing is an embedded real time system which is designed to control a single cylinder spark ignition fuel injected gasoline engine. There are a number of ways to approach developing this type of system, and a mixture of these are likely used in industry

  • Develop code on the embedded device and continuously test on the engine 
  • Develop code all in one go on the embedded device and perform a final test on the engine (you would have to be pretty confident in your code for this to be worthwhile)
  • Create an engine simulator in software on a PC and write your ECU software on the PC to control it, then program the ECU to an embedded device to test on a real engine
  • Develop model based code in software like Matlab Simulink, build an engine simulator and ECU separately in the software, then program it to a ‘target’ to run on an embedded device
  • Develop an engine simulator  on an embedded device and develop the ECU on an embedded device to control it

I’m going with the last option.

This allows me to see things happening in real time on the device which I am actually going to use for my ECU, and is as close to testing with a real engine as I can get at home. I plan on developing the engine simulator to model the real engine as accurately as possible so that I can ensure that my ECU works as expected before even trying it on the real engine.

Currently, I’m using a Texas Instruments Stellaris Launchpad as an engine simulator. I am programming this using an Arduino style IDE called Energia which allows very rapid development, although limits control over the hardware a little. The Stellaris Launchpad is based on an MSP430 micro controller, and has a wide variety of interfaces, analogue inputs, analogue outputs and GPIO pins.

For the ECU I’m using an Arduino Mega 1280 clone (Roboduino). Again, this has a wide range of inputs and outputs, with something like 50 GPIO pins. Although using an Arduino means that development is abstracted an additional layer away from the hardware, it does allow for very rapid development of embedded systems. The first objective of this project is to develop a working ECU, and an Arduino is probably one of the quickest ways to get there. Low level hardware and register control is still possible, and even necessary for some elements of development (timers for example) and I intend on moving to more powerful architectures later on in the project. I have done some development with a Freescale PowerPC, and already build an ECU in this architecture, but development is not quite as quick as with an Arduino.

So far, I’ve achieved the following:

  • Engine simulator
    • Engine simulator calculates engine speed and torque from throttle position and load. It outputs a missing tooth pulse waveform corresponding to the engine speed.
    • It receives a fuel input pulse and measures the length. It uses this to calculate the corresponding Air to Fuel Ratio. The AFR is used to calculate a lambda value, which is then output on an analog output pin for the ECU to read.
    • The AFR affects the maximum torque output of the engine
  • ECU
    • ECU can detect missing teeth in the crank angle sensor wave form and keep track of crank angle in 6 degree resolution (will probably improve this to 3 degree or greater)
    • Calculate engine speed from crank angle sensor
    • ECU outputs a fuel pulse with variable duration and timing
    • Output an ignition pulse with variable duration and timing
    • Lambda feedback control of fuel pulse width

17 thoughts on “Arduino Engine Control Unit and Stellaris Launchpad LM4F120 Engine Simulator

    1. Scott Snowden Post author

      Well, it kind of depends what you have available or what you’re willing to buy. If you’ve got lots of engine stuff available, then you probably only need to buy the ECU. I would suggest an Arduino board, costing between £25 and £50 I should think. Mine is a fairly old model now, so you might want to go with a later version which is fine because they’ll still be compatible and will be much faster (You may need to check the input and output pin voltages as some are only 3.3 V)

  1. Pablo

    Scott I would like to know what type of motor you used for your project. I want to build a similar project using a raspberry pi I just need the engine parts which I consider is the most difficult to get. Any idea or advice on how to get the engine are welcome.

  2. John

    Hi Scott
    I have recently acquired an aurduino & have begun coding…..may the force be with me.
    Would you let me have a copy of the code you used to run your engine with please. I have transplanted a V 8 into a pick up in Africa which has a 4l60E tranny. The tranny coupled to the aurduino will be my first auto project but I’d like to run the motor as well ultimately. I have it on a Gotech at the moment but would like to do my own version including H2 & O input etc. I’ll run it off an accelerometer or engine mount load cell of sorts which continually samples & advance_retards & rich_leans a little each way to get the best results under current conditions. Obviously there will be parameters that it’ll work within but I rate it will be quite something once working well. It should have a “teach” function as well….. but this later.

    For now I’m learning to code the thing & trying to work out whether I use millis to run an electric pulse motor. I want it to also sample advance_retard pulsing to get best results.
    I figured I could learn off your missing tooth code…..

    If you have something better, please let me have it if you’re ok with that.

    Cheers, John

    1. Scott Snowden Post author


      That sounds like an interesting and ambitious project! It’s a great way to learn if you’ve got a target like that though so good luck with it. You are more than welcome to look at and use my code, it is all available at although their SSL certificate seems broken at the moment, I think it will be fine.

      Depending on the speed of your motor etc. you may also be interested in looking at the microseconds() function, or if you need high accuracy timing, you may wish to use the hardware timers.

      When you say an electric pulse motor, do you mean pulse width modulated? I would suggest using the PWM analogWrite() functions for that if that is the case.

      Good luck!

  3. Falco Vermeulen

    Hello Sir,

    I like your project very much, respect for making that 🙂
    I am a student electronic engineering (automotive engineering), and my final work is a little bit similar to your work.
    I have to generate the signals from some sensors of an engine to the ECU. I can generate these signals easily using LabView, but my problem is that I have to make a ‘stand alone’ device. So it must be a board (like an arduino or whatever, or a custom build board) that operates without being controlled by a pc when operating.
    My problem is that I don’t know how to flash my program from LabView to a microcontroller (that is not on an expensice original NI device).

    So I’m trying to program it in another language now, like in C.
    But I’m having problems with the crankshaft signal. I accually have no idea how to program this…

    So when I see here that you have programmed this signal ; may I ask how you have done this?
    Is your signal like a sine wave with missing teeth or is it a square wave with missing teeth?
    If it’s a square wave it’s indeed not that hard to program, but if it’s a sine wave (like mine) then it’s a bit more difficult.

    Hope to get helpfull answer 🙂

    Many thanks in advance,
    kind regards,
    Falco Vermeulen.

    1. Scott Snowden Post author

      You have a few options here, National Instruments do actually make a device which is similar to the Arduino but compatible with LabVIEW code, but it is still about £300.

      Mine was square wave. Programming a sine wave is not really possible if you only have digital outputs (unless you use PWM and filter the output). If you have a Digital to Analogue converter, then you can do it, but the sine wave signals would need to be frequency modulated over each engine revolution from being the engine frequency * the number of teeth on the crank sensor to give your toothed output, and then the frequency reduced over the missing tooth to freq/2 if you had two missing teeth for example.

      If you are writing in C and you are looking to do an engine simulation rather than a controller, then look at my engine simulation code. You can download it here:

      It is designed for a Texas Instruments LaunchPad, but should be easily ported to Arduino.

      The essential concept is to set up a timed loop and increment a counter on each loop iteration, and to flip an output port if the counter is less than 116, and set it to 0 otherwise. Then reset the counter to 0 if it is greater than 120. This essentially gives a 60 – 2 missing tooth crank sensor type output.

      Good Luck!

  4. Willy

    Hi Scott
    I´m thinking in running a 4 cyl engine using an Arduino as ECU, I’m planning using throttle body injection with one or two injectors, so it’ll would be less complex than sequential injection, and controlling the ignition too. Can you give me and advice on which Arduino board is the best for this, and do you think this project is feasible?

    Cheers, Willy.

    1. Scott Snowden Post author

      I think this certainly sounds like a feasible project, as you say – your method with throttle injection simply varying the pulse width modulation of the throttle signal would be simpler than the timed injection I am using.

      You would have to make a few modifications to the code – change the injection code to vary a PWM output instead, and add handling of ignition for 4 cylinders – this could possibly be done by simply resetting the ignition timer in the ignition interrupt back to the same value instead of disabling it. This would give you multiple ignition events per cycle. You could add a counter that incremented on each ignition even which then disabled ignition after no.4 and was reset to zero after the missing tooth detection.

      I think that you would likely be best to use one of the newer boards as they have a few more features, probably either the Uno or Due would be suitable, but the Due is a lot more powerful, however only allows 3.3V inputs and outputs so it would depend on what other electronics you were using.

  5. Bugaboo

    Hi Scott:

    I have downloaded your code from bitbucket.

    Currently I have a TI Tiva C series TM4C123G LunchPad Evaluation Kit and installed Energia. I opened engine_sim.ino in Energia and tried to compile it, but I am getting following error msg and unable to compile.

    timer_blink.c: In function ‘initTimer’:
    timer_blink.c:22:31: error: ‘TIMER_CFG_32_BIT_PER’ undeclared (first use in this function)
    timer_blink.c:22:31: note: each undeclared identifier is reported only once for each function it appears in

    I am not sure how to proceed. Is there a getting started guide with your code. I am also having couple of Arduino boards and want to set up ECU in one of those using your code. Hopefully, I can get both of them working in tandem.


    1. Scott Snowden Post author

      This appears to be because the timer that I used with my board is not supported by your board, you would need to replace the initTimer function with one that is supported by your board. I’m afraid I don’t have a TM4C123G so can’t test a new timer function for you. I imagine it should just be a case of finding the relevant alternative timer register for your chip.

      You should be able to get the Arduino and TI board to work in tandem, that’s the way that I was doing it.

  6. Andrey

    A couple of guys myself included is working on an open source ECU, would that be something you would be interested to look at or maybe even contribute? I hope I am not too intrusive 🙂

    We are using ChibiOS and stm32f4

    1. Scott Snowden Post author


      Sorry for the slow reply. It’s certainly something I would be interested in, I’m not sure I would be able to contribute too much time at the moment, but would certainly be interested in hearing about it!

  7. Anton

    Hi! Like your work! I just started a simular project and I came across your blog. I guess you wrote som sort of report/dissertation for your MSc. Would that be possible to read?

    1. Scott Snowden Post author

      I did write a dissertation, I intend on putting it online at some point, I just need to get time to do it.


Leave a Reply

Your email address will not be published. Required fields are marked *