# Draw a dc-dc buck converter in LaTeX using CircuitikZ

A dc-dc buck converter is a dc voltage converter used to transform an unregulated dc input into a lower controlled dc output. This transformation is achieved by the use of semiconductors devices that turn on and off at high switching frequency.

In this tutorial, we will learn how to draw a dc-dc buck converter in LaTeX using CircuiTikZ package. The idea is to recreate the circuit diagram shown in Fig. 1, which is published recently in IEEE Xplore.

Fig. 1: Buck converter circuit diagram (published in IEEE Xplore 2019)

## What motivates me to use TikZ

Some of the drawbacks of adding an image directly into a LaTeX document are:

• The font is not the same as the rest of the document,
• The font size is affected when the image is scaled,
• If you would like to modify your illustration you have to go back to the used drawing tool, more distraction.

Well, we assume that the image in question is a good one, like mine 😈

For that, I prefer to use TikZ to draw my illustrations and benefit from its features such as precision, reusability and automation. And when it comes to drawing circuits in LaTeX, circuitikz is the best option.

My first illustration, December 2011.

## CircuiTikZ, minimal code

The CircuiTikZ package can be loaded as follows:

\documentclass[border=0.2cm]{standalone}
\usepackage{circuitikz}

\begin{document}

\begin{circuitikz}[american]
\end{circuitikz}

\end{document}


It is based on PGF/TikZ, which means no need to upload TikZ package twice.

The circuit code is added inside the CircuiTikZ environment which is an alias for tikzpicture. As an option, we have chosen american style for the electrical components.

## The Origin, a successful journey depends on it!

Choosing the starting point of your illustration (the origin) is important as it makes positioning easy to deduce. Sometimes, to go faster, I draw my illustration down in a paper and add coordinates to it. The latter are adjusted later by trial and error method until I get a satisfactory result. Here is my hand drawn of a dc-dc buck converter:

I have chosen the starting point at the bottom left of the circuit diagram. From there, I will draw a dc source then 👉 a switch 👉 an inductor 👉 a resistor 👉 a ground and go back to the starting point.

Along this path, I save coordinates to use them later to draw the diode and the capacitor ($a_1, a_2, a_3$ and $a_4$). Ready for details, Let's go!

## DC/DC buck converter circuit diagram

Before going further with details and for curious minds, Here is the buck converter schematic drawn in LaTeX using the CircuiTikZ package:

Buck converter circuit diagram (re-created using CircuiTikz )

And the corresponding code is:

\begin{circuitikz}[american]

\ctikzset{
resistors/scale=0.7,
capacitors/scale=0.7,
diodes/scale=0.7,
inductors/coils=6
}

\node[nigfete,rotate=90,label=S] (switch)at (1.7,3){} ;

\draw (0,0) to[battery1,invert,l=$v_{in}$] ++(0,3) -- (switch.D);

\draw (switch.E) -- ++(1,0) coordinate(a1);

\draw (a1) to[cute inductor,l=L, i>^=$i_L$]
++(3,0) coordinate(a2);

\draw (a2) -- ++(1.5,0) to[R,l_=R,v^>=$v_o$,i>_=$i_o$] ++(0,-3) -- ++(-1.5,0) coordinate(a3);
\node[ground] at (a3) {};

\draw (a3) -- ++(-3,0) coordinate(a4);

\draw (a4) -- (0,0);

\draw (a3) to[C,invert,*-*,l=C,v<=$\:v_c$] (a2);

\draw (a4) to[D*,l_= D,*-*] (a1);

\end{circuitikz}


In CircuiTikZ, the electrical components are separated in two main categories:

1. One that are bipoles and are placed along a path. In this example, it correponds to resistor, diode, DC voltage source, inductor and capacitor
2. Components that have any number of poles or connections and are placed as nodes. In the buck converter case, it corresponds to the transistor and the ground elements.
• Transistor (nigfete): The transistor can be added using node command at any coordinate. It has four predfined connectors as shown in the next illustration where we can link paths to it.

In the circuit code, we have added the transistor as a node at the point with coordinates (1.7,3) and we named it (switch) to get access to its four connectors. This corresponds to line (10) of the code:

\node[nigfete,rotate=90,label=S] (switch)at (1.7,3){} ;


### CircuitikZ rotate a component:

By default, the transistor orientation is shown above and to get the right one, we have rotated it by 90 degrees using the option rotate=90

In addition, the switch has a label S, which is fixed by the option label=S.

• DC voltage source (battery1): It belongs to the first components category which are placed along the path. It is drawn from the origin to the point (0,3) and then linked to the transistor through its connector (switch.D):
 \draw (0,0) to[battery1,invert,l=$v_{in}$] ++(0,3) -- (switch.D);


### CircuitikZ flip component:

By default, the DC voltage source polarity is inverted. To flip it, we add the option invertcheck the following illustrative example:

The DC voltage source has the label vin, which is defined by the option l=$v_{in}$. We can specify the label position (right or left of the electrical component) using l^=$v_{in}$ or l_=$v_{in}$

• Then, we have to move from the right of the transistor (switch.E) by 1cm along the x axis and save the coordinate using the command coordinate as follows:
\draw (switch.E) -- ++(1,0) coordinate(a1);

• From the point (a1), we draw an inductor along the x axis (3cm) and there we save the second point (a2):
 \draw (a1) to[cute inductor,l=L, i>^=$i_L$]
++(3,0) coordinate(a2);


The inductor shape is obtained using the option (cute inductor). With the same manner as the DC voltage source, we have added the label to the inductor using the option l=L. Here is a detailed list of different current directions:

### Flip current direction:

• From the point (a2), we draw a straight line along the x axis and from there we draw a resistor (using the option R) along the y axis as follows:
 \draw (a2) -- ++(1.5,0) to[R,l_=R,v^>=$v_o$,i>_=$i_o$] ++(0,-3) -- ++(-1.5,0) coordinate(a3);

• The ground is added as a node at the point named (a3):
\node[ground] at (a3) {};

• From the point (a3), we have drawn a straight line to the origin and we have saved the point (a4) to be used later to draw the diode:
 \draw (a3) -- ++(-3,0) coordinate(a4);
\draw (a4) -- (0,0);

• Now, we draw the diode between the points (a1) and (a4), and the capacitor between the points (a2) - (a3) :
\draw (a3) to[C,invert,*-*,l=C,v<=$\:v_c$] (a2);

\draw (a4) to[D*,l_= D,*-*] (a1);

• We have added the option *-* to both components to highlight the connection points.

### Change components size

Components can be scaled using the command \ctikzset as follows:

\ctikzset{
resistors/scale=0.7,
capacitors/scale=0.7,
diodes/scale=0.7,
inductors/coils=6
}


Without components scaling

With components scaling (70%)

At this level, we have reached the end of this tutorial. Read the rectifier tutorial or share with us your thoughts, reach us we will be happy to hear from you!