Brushed Motor ESC using PIC12F675 microcontroller
There are a number of other projects that have been documented for brushed motor ESCs. The good starting point for other ESCs is this rcgroups thread. The information in the Microchip AN847 is also very useful.
Most of these designs are very similar, the variation between designs is typically in the PIC used to control the ESC, the precise FETs used, and the number of features that the ESC offers. Some variation is also probably due to the availability and cost of parts.
There were two main reasons that I embarked on this project:
- None of these projects used the 12F675, and this PIC has a number of interesting features. The main one is that being a Flash programmable part it makes a good vehicle for testing ideas and permitting the ESC to be reprogrammed and various ideas tested.
- None of the other designs offered exactly what I was looking for and in addition there was a certain challenge in programming a 12F675 to do this task.
Why build your own ESC
Some of the reasons that you may consider using the information here include:
- You may well be able to get the mix of features you want at a reasonable price. Building your own ESC offers opportunity to get exactly what you want.
- Repair of a DIY ESC may be easier (in Australia repair of ESCs is nearly impossible and expensive). For example, if the output FET fails it can be replaced at modest cost.
- It may provide an additional area of interest for those who like to tinker. You may also want to learn how an ESC works.
- If based on a Flash PIC (eg. 12F675) you can modify the ESC software at no cost. This would included modifying the cut-off voltage, brake type, etc. You could even modify the software to have the ESC record information regarding the flight.
- You can pick and choose the output FET for required current level while still getting the same ESC facilities.
Why not to build your own
There are a number of very good reasons why you should not build and fly your own ESC. These reasons wouldn’t stop you from playing with this idea to learn how it all works.
- Reliability of the BEC (and to some extent the ESC) is an important safety issue. Modern commercial units with all SMT parts have a high mechanical reliability.
- There may be very little financial advantage if you have access to reasonably priced ESCs. Just the time taken to build the ESC is worth something, perhaps you would prefer to spend if flying!
- Purchasing items such as MOSFETs on a one-off basis is relatively expensive. You may also need to select some components for this design based on what is available to you at reasonable cost.
- The DIY ESC is probably going to be heavier than the SMT based commercial ESCs. This design built with standard components weighs approximately 25gms – see below.
What this ESC offers
This ESC design offers the following features:
- Safety startup, requires throttle be off to arm ESC.
- Signal loss stop, stops motor on loss of valid signal.
- Low voltage cutoff, stops motor on low battery voltage.
- Variable rate PWM output to reduce noise a low speeds and increase efficiency at high speeds.
- Programmable throttle response curves with two sample curves.
- Slow start to protect gearboxes (or fast start if desired)
- Brake, either hard or soft (optional).
- Accurate throttle pulse width measurement.
- Accurate software PWM with rates between 7.6kHz and 2kHz.
- 64 motor speeds for smooth speed control.
- Automatic low and high throttle setting.
- Both braking and non-braking zero throttle positions for use in car applications (optional).
- Lost Model Alarm (optional).
- Glitch counter (optional).
- Reverse output (optional).
Low Voltage Cutoff
The ESC monitors the battery supply voltage and turns off the motor when the supply voltage drops below a preset point. The code as supplied operates the cutoff at 6V, this is considered a relatively safe point. The LM2940 BEC will operate down to 5.6V so stopping the motor at 6V leaves some margin for error. Once the cutoff has detected 6V or less the throttle must be reset to idle before the motor will operate.
The ESC checks the battery voltage every time a servo control pulse is received, or about every 20msec. These readings are averaged over 4 consecutive samples to get a more stable battery voltage reading. Under high load conditions it is possible to obtain a very low reading as the FET switches on, the averaging reduces the chance of a false voltage reading triggering the LVC. If desired the sample size can be increased. An alternative would be to increase the filter capacitor on the voltage sense input, however a software solution permits the filtering parameters to be changed without a hardware change.
The ESC can be configured with two different low voltage points. In this mode the GP3 input (jumper W1) selects which cutoff voltage is in effect.
Automatic low and high throttle setting
When the ESC is powered on the throttle is assumed to be in the ‘low’ position. After power on you must move the throttle to ‘high’ and then back to ‘low’. The ESC will then arm and sound two short ‘beeps’ via the motor. This feature assumes that the ‘high’ setting has a longer servo pulse width than the ‘low’ setting. So Futaba transmitters will still require servo reverse on the throttle channel.
After the ‘beeps’ have sounded (1.5 seconds) the ESC enters normal operating mode and will wait for ‘low’ throttle. This means that if the throttle is advanced during the ‘beeps’ the ESC will not spin up the motor. So throttle should not be advanced until the ‘beeps’ have finished.
The slow start operates by advancing the throttle 1 step every time the received throttle setting is higher than the current setting. In practice the means that to go from idle to full throttle takes about 1.2sec (64 * 0.02sec). If the received throttle is lower than the current ESC setting the ESC will wait until two consecutive lower throttles and then immediately reduce the throttle to the selected value.
This behaviour stops one faulty receiver pulse from ‘blipping’ the engine. With slow start a faulty ‘low’ throttle causes the ESC to have to build up to the correct setting using the slow start, for smooth flight this is to be avoided where possible. A single pulse filter appears to significantly reduce this problem.
If desired the ESC can be constructed with the slow start turned off. In this case the the ESC will raise the throttle immediately a higher throttle setting is received.
The soft-brake option uses the throttle PWM engine to gradually applying the brake one step every time the idle throttle is received (about every 20ms). This means it takes approximately 1.2sec for the brake to be fully applied. Flight testing shows that this works well with the gearbox not being subject to sudden changes in motor speed.
The brake enable/disable via a jumper is an option of this ESC software. This is provided mainly to demonstrate that control of the ESC features via a jumper is possible. One possible use of a jumper is for disabling the brake. It may be convienent to disable the brake without changing the PIC in some aircraft or possibly when testing and evaluating the brake.
Programmable Throttle Response Curves
Most standard ESCs convert the throttle setting into PWM rates using a simple linear mapping. So that 50% throttle corresponds to a 50% PWM. However, a 50% PWM rate is actually delivering only 25% of the full power to the motor. This is the standard configuration of this ESC also.
The design of the software for this ESC makes it very easy to alter the mapping from throttle to PWM rate so that 50% throttle corresonds to 50% motor power.
Of course, other throttle response curves are also possible. Because the generation of the PWM engine is performed by a Perl program you do not need to be able to program PIC assembler to try out your own throttle response curves. (You do however need to be able to write Perl code that converts the throttle value between 0 and 1 into a PWM on rate between 0 and 1.)
The throttle response curve also determines when the brake is applied (if the ESC is configured with a brake). This permits applications where some of the throttle positions have the motor off and the brake off, and other positions have the motor off and brake on.
Braking and non-braking zero throttle positions
The ESC supports motor off settings with and without the brake applied. (This may be of use on R/C car applications.)
Typically the throttle at its lowest setting has the motor off and brake on, then as the throttle is increased it changes to motor off and brake off, and then increasing further powers up the motor. When the throttle is reduced from the motor on setting it first enters a region where the motor will coast (ie. motor off and brake off) and then if the throttle is reduced further the brake will then be applied.
Lost Model Alarm
The LMA is triggered by either loss of throttle input from the receiver for more that 10 seconds or the throttle input being set to a sufficiently negative input (ie. the throttle trim moved as low as possible).
Suggested hardware for the alert is a simple Piezo Transducer the ESC will produce the appropriate AC drive for the transducer. Alternaticly a piezo transducer with inbuilt siren can be used and the ESC produces a simple on/off output. Depending on the volume level that you require the piezo can be driven directly from the PIC, or via a buffer transistor from the unregulated battery voltage.
The LMA siren is driven out of the GP1 pin on the PIC with an active high siren signal.
The glitch counter can be selected as long as there are no other options using the GP3 input (jumper W1) to the PIC.
The glitch counter counts the number of faulty receiver pulses and ‘beeps’ the count out on either the motor, or piezo sizen if the LMA facility is configured.
- If there have been no glitches a single long beep will sound.
- If there have been more than 99 glitches two long beeps will sound.
- For between 1 and 9 glitches than number of short beeps will sound. For example, 4 glitches will sound four short beeps.
- For between 10 and 99 glitches the LMA will beep out the tens and then the units with a pause between them. A unit of zero is indicated by a long beep. For example, 13 glitches would be 1 short beep, a pause, and then 3 short beeps; 10 glitches would be 1 short beep, a pause, and then 1 long beep.
The reverse output is available when there is a spare output pin on the PIC. This reverse implementation is a simple reverse function that is designed primarily for cars (or boats). As such the reverse facility should only be enabled with the the car throttle map (THROTTLEMAP = 3).
The reverse output will toggle state when the throttle is held in the brake position for more that 5 seconds. The output can be used to drive a relay, or could be used as a control input to an H-bridge for electronic reverse.
The output signal (GP1) is low (0 volts) for ‘forward’ and high (+5 volts) for ‘reverse’.
Why use the 12F675?
The Microchip 12F675 offers a number of interesting facilities that are used in this design. These facilities permit a much better ESC to be designed than is possible with a 12C5xx PIC, without the complexity of designs using larger PICs.
- Internal 4MHz oscillator. Not having to rely on an external crystal, or similar, for the processor clock keeps the RFI produced by the PIC to a minimum.
- Two timers. The 12F675 has two timers and what is more important one of them is a 16-bit timer with external count enable control. This feature permits accurate timing of the throttle pulse width to within 1usec. In addition the 12F675 offers interrupts on the 8-bit timer that permit accurate software PWM in parallel with other operations.
- A/D Converter. The on chip A/D converter simplified the low voltage cutoff detection.
- Comparator. The 12F675 also offers a comparator that can be used as an inverter when required (and pinout limits permit).
- Flash Programmable. The 12F675 can be reprogrammed and therefore is an excellent PIC for experimenters.
There are a number of important challenges in designing an ESC:
- Accurate timing of the throttle control pulse.
- Accurate PWM generation to ensure the motor speed is even.
- Obtaining a high enough PWM rate at low motor speeds to ensure that the motor does not operate in discontinuous mode and produce unacceptable noise and vibration.
- Maximising power efficiency. This is assisted at high motor speeds by reducing the PWM rate to minimise the switching losses.
Radio control control pulses are 1.500msec wide at the centre position and with a swing of approximately 0.500msec in either direction for full stick movement. A reasonable assumption for throttle controls is that throttle ‘off’ is a pulse width of 1.150msec and ‘full on’ is 1.850msec.
If you wish to obtain 64 possible motor speed settings then each motor speed represents 0.012msec of control pulse width and the ESC must measure the pulse with sufficient accuracy and resolution to permit determination of the throttle position.
There are two main techniques for measuring the throttle pulse width: a software counter or a hardware counter. If a software counter is used great care needs to be taken to ensure that there is sufficient measurement resolution, and operating a software PWM at a reasonable rate at the same time is almost impossible. The best method is to use a hardware counter, and in this case the 12F675 offers an externally gateable 16-bit counter with 0.001msec resolution.
The major difficulty with performing PWM in software is obtaining an accurate stable frequency while the PIC is performing other tasks. Normally interrupts would be used to obtain reasonable accuracy, however, interrupt response and entry/exit overheads make it difficult to use interrupts when either the FET on or off time is short.
This ESC uses a combination of software timing and timer based interrupts to generate the PWM.
This is achieved by having the PWM code written by another piece of software. A Perl program contains all the details regarding the processor timing and generates PIC code to implement the PWM engine. This permits the selection of PWM frequency on a continuously variable basis, and also the implementation of user designed throttle profiles.
Further details regarding this PWM generation software is below.
The PWM engine uses software timing to generate short pulses and the timer with interrupts to generate long pulses. This permits the main ESC application to be written without concern for precise timing.
Is it any less expensive?
Under some circumstances there are considerable savings to be had by building your own ESC, however, most of the time there isn’t much cost advantage.
The following table shows some current (7 December 2003) prices for commercial ESCs (purchased by mail order) and DIY prices.
|Purchase 18A ESC (JETI JES-180)||A$50|
|Purchase 8A ESC (GWS-ICS-300)||A$30|
|Build 13A ESC no brake (1A BEC)||A$24|
|Build 13A ESC with brake (1A BEC)||A$30|
|Build 25A ESC no brake (1A BEC)||A$30|
|Build 25A ESC with brake (1A BEC)||A$36|
The major cost item are the FETs, and the ability to obtain good quality (ie. low resistance, high current) FETs at a reasonable price is the most important issue.
Software Development Environment
This software was developed with the MPLAB IDE from MicroChip.
If you plan to use this ESC design and build one yourself you will need to download the MPLAB IDE. There are too many ESC options for all the versions to be available on this Web page. Common versions are provided here, but it is almost certain that you will want to adjust some of the parameters to suit your requirements – after all that is half of the reason for building your own ESC.
After you have installed the MPLAB IDE proceed as follows:
- Create a directory somewhere on your Windows system. the MPLAB IDE does not like path names that are too long (greater than 30 characters or so) – so stick to a simple short name. For example: C:\PIC\ESC
- Copy the
carpower.incfiles from the links below into your diectory.
- Start the MPLAB IDE.
- Select Project->Project Wizard.
- Click Next.
- Select ‘PIC12F675’ as the device, click ‘Next’.
- Select ‘Microchip MPASM Toolsuite’ as the Active Toolsuite, click ‘Next’.
- Enter a name for your project and then use the ‘Browse’ facility to set the Project Directory to the directory you created at the first step, click ‘Next’.
- Add the 4 files that you copied to the project directory to the project by double clicking each file name in the left hand pane, the files will appear in the right hand pane. Do NOT check the boxes next to the files. Click ‘Next’.
- Click ‘Finish’.
- The workspace will open and you will see a project window that shows one Source File and two Header Files.
- You should now be able to assemble the PIC code by pressing F10 – this will produce a file called ‘esc.hex’.
- To alter the paramaters double click on ‘esc.asm’ in the project window and the assembler source will now appear in a new window, make the changes you require in this window.
- After you have made you changes press F10 to assemble the code.
The MPLAB IDE has full support for simulation that permits you to fully check the operation of the ESC. However, the use of these facilities is beyond the scope of this document. Users who just want to build a ‘custom’ version by modifying standard parameters of the ESC should have no problem just using the IDE to assemble the code.
Note: when the code is assembled the appropriate circuit type (one of ESCA, ESCB or ESCC) will be placed as text into the .HEX file along with the software version and the module name. This text is intended to be visible when you load the .HEX file into the PIC programmer software and allows you to check that the PIC is configured for the circuit type you have.
For more detail: Brushed Motor ESC using PIC12F675 microcontroller
Current Project / Post can also be found using:
- home made esc circat
- how to make esc
- home made esc
- easy diy esc