Author Archive

C Ternary Operators and are they better

Friday, February 17th, 2023

Recently this was posted in a C for Embedded programming Linkedin group.

Attention C programmers!
Here’s a quick trick that can make your code more efficient and save you time. Did you know that you can use the ternary operator to simplify if-else statements?

For example, instead of writing:

if(x > 0){
y = 1;
y = 0;

You can simplify it to:

y = (x > 0) ? 1 : 0;

This is particularly useful when you have multiple if-else statements within a loop. By using the ternary operator, you can make your code more readable and reduce the number of lines, leading to improved performance.

Give it a try and let me know what you think! 

C programming style for conditional code

Before you get all enthusiastic about this it is important to note that it is unlikely that the compiler will produce different or better machine executable code in the suggested case. It is really worth every software engineer spending time looking at the assembly code to see how little effect code shortcuts have. And this ternary construct is not allowed in some code standards. This paragraph was what I posted to the discussion.

The linkedin thread continued.

One person commented

“A neat trick, although I doubt that better performance would be a benefit. One should beware of premature optimization and overusing this technique. Code readability is king!”

Another added

“In my opinion, the use of ternary operator is often used and abused in places where it actually makes the code less readable. Some coding rules actually prohibit the use of the ternary operator altogether.”

One other person said politely

“sorry to give you this information, but even without any optimization the compiler will produce the same assembly, you can try by yourself using one of the different tools online such as”

It looks like might be a fun tool to allow software people to look at assembler for different compilers.

It is always good to look at the compiled output assembler code. It shows you what you have actually done.

Version Control the Code Tools for high quality software?

Friday, February 17th, 2023

I was traveling before Christmas to get to a couple of meetings. A software guy in one of the meetings had to step out because there was a pressing issue with a serial communications system. He was back in about 20 minutes because he had “fixed it”. Turns out an IDE/Compiler/ToolChain code library had been updated automatically and this had broken the serial comms.

So how are you controlling how you version the toolchain libraries? Do you version the toolchain?

Do you just roll the automatic updates in? Do you do anything? #software #communications #versioncontrol

How do you version control the code tools for high quality software?

One option to control the version of toolchain libraries is to freeze the version used in a project by specifying a specific version number in the project’s build system. This can prevent unexpected changes and breakage caused by automatic updates. Another option is to have a dedicated team responsible for testing and controlling updates to the toolchain libraries, and only roll out updates that have been thoroughly tested and deemed stable. Additionally, organizations could also consider setting up their own internal repository for the toolchain libraries, with version control and a process for testing and releasing updates.

Having clear control and visibility into the toolchain libraries used in a project can greatly increase the stability and reliability of the system. It can also minimize downtime and improve the efficiency of debugging and fixing issues. Overall, having a well-defined version control strategy for the toolchain libraries can lead to a better software development process and improved product quality.

More often than not the silicon vendor-provided code is not available in a repository but comes as some self-installing executable. that checks for updates and installs said updates them without telling you what they are, This is such a problematic low-quality approach. So it was so cool to see this SDK from NXP that is delivered in it’s GitHub repo. Such a good way to do it. Well done NXP.

Version Control the Code Tools

The link to the SDK information is

The GitHub link is

Truly well done to NXP and whoever was in charge of setting this up.

Hopefully the other vendors will take the same approach.

How Long will my Supercapacitor Last?

Wednesday, December 14th, 2022

The thorn in the side of the supercapacitor is its limited lifetime.

Supercapacitors can do many things in the field of energy storage and have be touted as being the future batteries.

However any supercapacitor if not lifetime-engineered properly can fail prematurely.  This leaves your company with potentially large warranty costs.

What is lifetime?

Supercapacitor lifetime stems from chemical reactions in the capacitor causing the capacitance to decrease.  These reactions happen more often when it is hotter and thus lifetime can vary significantally with temperature.  These reactions are governed by the Arhennius equation and appear on the datasheet as a lifetime.  A typical lifetime for a supercapacitor is 1000 hours at 70C.  This means that if your supercapacitor is at 70C it burns through a lifetime in 42 days!  At only 83C some electrolytes can even boil causing your supercapacitor to rupture!  However, if the temperature is decreased a supercapacitor’s lifetime can be increased greatly.  It is all governed by this equation:
Temperature Equation
where t is lifetime, Tn is the nominal temperature, tn is the lifetime at the nominal temperature, Ea is the activation energy, T is temperature and kB is Boltzmann’s constant.  In most cases this can be approximated by “halving the temperature doubles the lifetime”. If you are not using this equation in designing your supercapacitor systems then you better start using it fast!  Note that this equation is for any electrolytic capacitor not just supercapacitors.

Lowering voltage

From the prior equation it can be seen that managing the temperature of supercapacitors can do wonders in increasing their lifetime.  It is a lesser known fact that running them at voltages below the rated voltage also increases their lifetime.


Once you have your product in the field, re-engineering can become costly, so verification of your design and predicting failure rates becomes very important.  This is done by measurement of the capacitors and then statistically analysing the results.

Measurement is important

Measurement of supercapacitors relies on the use of a simple capacitor model with just capacitance and ESR.  This allows measurement by using the fact that the voltage response to a constant current source is a straight line.  From this straight line you can thus measure the capacitance.  When a constant current is applied there is also a small offset due to the ESR, which can be used to measure the ESR.

After measurement

Once you made the measurements of a random group of supercapacitors you can begin to analyse the results.  First of all you calculate the lifetimes that the supercapacitors have experienced up to the measurements based on their capacitance.  Then these lifetime results typically fit a Weibull distribution. Using this fact, you can curve-fit your sampled data to a distribution with certain parameters and calculate the confidence of your fit.  You now have a range of distributions that fit your data.  From this range you simply find the worst case and use it to predict worst-case future failure rates.  This information can be passed on to the accountant in the form of warranty liability.

How can we help?

ELMG Ltd have designed a system which can use this measurement technique to measure supercapacitor’s capacitance during both charging and discharging.  This is done using a controlled constant current source.  This system is highly scalable meaning that it can be used on capacitors ranging from a few farads to hundreds of farads.  Once the measurements are made ELMG can then perform the statistical analysis and come up the results to tell your finance people what you warranty liability is.

Xilinx Training

Monday, November 14th, 2022

ELMG Digital Power is an AMD-Xilinx Partner. As part of this we keep up to date with Xilinx parts, tools and technology with training. Consequently ELMG can provide the best digital control solutions for digital power electronics.

AMD Xilinx Training keeps ELMG Digital Power your best partner for control of power electronics.

We started digital control of power electronics with Xilinx in 1997 so as a result ELMG Digital Power’s long history with Xilinx means that we have exceptional experience and expertise.

ELMG Digital Power for Digital Power Electronics Control expertise.

Softcore processing in FPGA

Friday, November 11th, 2022

Softcore processing in FPGA is a great option to get sequential processing in a RTL design.

Adam Taylor ran a workshop on how to use Xilinx MicroBlaze in FPGAs.

If you are interested in FPGA control for Power Electronics then a soft core is really useful.

Adam is great at Xilinx FPGA and we’d recommend his training to anyone wanting to learn about MicroBlaze on FPGA.

Click the link to view the recording.

Mastering MicroBlaze with Adam Taylor

Enjoy the training.

Energy Storage everywhere and a converter to connect it.

Wednesday, May 4th, 2022

Energy storage is everywhere or is being talked about everywhere. Adding an energy storage power electronic converter to the distribution or customer network allows

  • Load leveling across the day
  • Integration of local renewable sources such as solar and wind
  • Deferment of transmission investment
  • Reduction in CO2 emissions because peaking plants do not need to be run
  • Lowering energy cost by price arbitrage on daily demand variation

Historically the electrical grid has had energy flow from the large remote generator station out through the transmission and distribution network to customer loads. And the voltage profile was set up with transformer turns-ratios for this.

The transmission and distribution grid has historically transmitted power from central generation to the load. Energy storage power electronic converters can aid the integration of renewable generation
The transmission and distribution grid has historically transmitted power from central generation to the load

Energy storage is DC so there needs to be a reversible DC to AC power converter to interface between the battery and the grid. This is where power electronics provides the glue between the

Here at ELMG Digital Power, we’ve been working on grid-connected power converters since 1997 in applications including

  • Harmonic shunt active filters
  • Static VAr compensators from 2kVAr to 500MVAr
  • Solar Inverters
  • Micro Inverters
  • Grid Interactive UPS Inverters
  • Battery Energy Storage Inverters
tropical rain forests can be conserved by minimizing energy use with energy storage power electronic converters
Saving energy with grid energy storage reduces carbon dioxide emissions

Grid connection challenges

Grid connection challenges with an energy storage power electronic converter include grid synchronization usually with a PLL or a DSOGI, Low voltage fault ride through, designing the LCL filter, and connecting converters in parallel.

Grid synchronization for energy storage power electronic converter

The grid frequency and phase can vary, especially if there are faults or disturbances in the network.


The Phase-Locked Loop (PLL) keeps the inverter voltage synchronized to the grid. This allows the inverter digital control system to have the real and reactive power flow as requested. It also makes sure that if the grid frequency isn’t exactly the expected 50Hz, 60Hz or 400Hz then the system will still operate correctly.

The Dual second-order generalized integrator is used to implement an alternate type of frequency lock. It is often considered that DSOGI does a better job in the transient ride through. However, a well-tuned PLL is as effective as a DSOGI.

Sinusoidal grid voltages to synchronize energy storage power electronic converters
Grid voltages are sinusoidal for the most part

Low voltage fault ride through.

It is inevitable that there will be a fault in the AC system. Either two phases will connect together or one phase will connect to the ground or alternatively there can be some combination of phase to phase fault and ground fault. This causes a voltage disturbance that the inverter must “ride through”. To ride through the PLL must stay synchronized and the inverter must control the current as required by the grid code. (We’ll cover that later). So the PLL/DSOGI must be designed to ride through the voltage disturbance.

LCL Filter design

LCL filters are used to minimize the cost of the grid coupling filter. The higher filter has some poorly damped dynamics so synthetic damping is provided by control or via the addition of a small damping resistor. (Some grid codes now require this damping resistor).

LCL filters used in energy storage power electronic converters
LCL filters provide low loss low cost grid connection

Ensuring that the LCL is correctly designed so that it can be controlled with a suitably, low-cost controller means that LCL design is critical to a successful battery energy storage converter. The AC grid impedance range specified for the converter is really important.

Contact us for energy storage converters

Paralleling converters

It is a product manager’s dream to be able to put energy storage power electronic converters in parallel in any combination. If this is to be a possibility then all of the

  • LCL filter design
  • Controller choice,
  • PLL design
  • Parallel connection

need to be managed for parallel connection from the very beginning of the development. It is best to limit the required parallel connection combinations to a minimum as each requires verification and validation testing.

Battery energy storage interfacing to the battery

The key issues in interfacing an inverter to battery energy storage are

  • The allowed ripple current and voltage for the battery and what this means for the grid side LCL filter and voltage imbalance.
  • The charge and discharge rates allowed for the batteries
  • How to manage and coordinate with the battery management system (BMS)

Battery Types

There are two principal types of batteries used in energy storage

  • Lithium Ion
  • Lead Acid

Contact us for energy storage converters

Lithium Ion

Lithium-Ion (Li-Ion) batteries work unsurprisingly, by the movement of lithium ions. The energy density is high. The key safety issue with lithium-ion cells is the risk of fire from excess temperature. Li-Ion cell safety needs to be actively managed.

State of charge and state of health systems for Li-Ion systems are widely available.


Lead-acid batteries have lead and sulphuric acid. The energy density of lead-acid batteries is lower than Li-Ion.

State of charge and state of health systems for lead-acid batteries are less effective than those for Li-ion.

Grid codes

The AC grid is one of the most reliable machines in the world. Blackouts in most countries occur irregularly. This reliability is the result of long experience and conservatism by system operators.

Battery energy storage converters connected near the load end of the network are an unmanaged and uncontrollable (in grid operator terms non-dispatchable) energy source. To ensure the AC grid machine stays reliable there are rules for connecting power and energy generation to the AC grid network. Energy storage is a generation source so it is covered by the rules for generators. These rules are called the Grid Code. Each country’s grid code is different as each country has a different AC network.

Contact us for energy storage converters

Power Ramp Rates

The most common and most useful rule for generating is that the real and reactive power ramp rates must be limited. The power cannot go up or down too fast. What does too fast mean? Typically the ramp rate limit is zero to maximum power in tens of minutes.

Reactive Power

Adding power to the grid causes the voltage to rise. And the grid was designed to supply power to the load rather than get power from the load. So the battery energy storage real power output may well cause the voltage to rise. This can be counteracted with reactive power draw to keep the voltage down. This reactive power requirement means that the inverter may well need a higher current rating and so will cost more.


Some Grid Codes have the requirement that each inverter be connected to a system operator communication network where the network operator can control the real and reactive power dispatch or how the real and reactive power are controlled together. These communication network standards are well established in mature solar markets, such as Germany.

Safety Disconnection

The principal safety requirement for the grid code is to disconnect the energy storage system if the AC grid fails. This is to ensure that anyone working on the AC lines is safe from electric shock. This anti-islanding is a requirement for all grid codes.

saving oil with energy storage power electronic converter
Grid energy storage allows the integration of renewable energy and the reduction in the use of fossil fuels

Energy storage power electronic converter FAQs

How do you implement anti-islanding?

The simplest way is with a frequency or phase angle perturbation. The best way is with a grid impedance change detection system.

Why do they keep changing grid codes?

The key reason is that adding more embedded generation and storage is threatening the reliability of the grid so they are changing the rules.

What is the best way to design an LCL filter for a grid-connected battery energy storage converter?

Well, this is a massive question. The best answer is to start with the low voltage fault ride-through (LVFRT) and the PLL and then work backward from there. A current control bandwidth target of 1kHz is useful.

Three-level or two-level inverter for an energy storage power electronic converter?

Get some help before you start if you are designing a three-level inverter. Common mode is easier to deal with for three-level. Modulation is less easy.

Can ELMG Digital Power Design us a Grid Connected Battery Energy Storage Converter for 1MW?

Yes. Any power from 1kw, 10kw, 20kw, 100kw 1MW, 10MW to 100MW.

Can you do MV or HV battery energy storage power electronic converter?

Yes, this is possible and we have worked on 11kV and 66kV MV UPS converters and their static transfer switches. Battery banks are best below 1500Vdc and so a multi-level multiple converter solution might be a good idea. Or alternatively, transformer coupling for the step up is also a useful and economically competitive approach.

Is thyristor, IGBT SiC Mosfet, or Si Mosfet the best switch choice?

Thyristors and IGBT are the most rugged so are more robust in grid-connected battery energy storage converters. SiC os Si Mosfets are also suitable choices.

Contact us for energy storage converters