Graphics, Figures & TablesCircuit with capacitors and switches

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Diabloi
Posts: 7
Joined: Thu Mar 10, 2022 4:55 pm

Circuit with capacitors and switches

Post by Diabloi »

Hello,
I would like to implement this circuit in latex, but i have no idea how to do it.
Screenshot from 2023-04-25 22-45-18.png
Screenshot from 2023-04-25 22-45-18.png (59.64 KiB) Viewed 2695 times
I tried to do in circuitikz but i don't find this switches and i got many problem on how to draw. Someone can help me to draw an example like this?
I need to implement much more circuits, i need to start from an example that is useful for me.
Thanks in advantage! :D

After different try and errors I got this code, but I didn't really understand the philosophy.
\begin{tikzpicture}
\draw % in
(0,0) to [short,o-] ++ (0,0) coordinate (in)
%-- ++ (0, 0)
to [nos,l=$x$,-*] ++ (0,-1) coordinate (x)
-- ++ (+1,0)
to [C,l=$C_2$,i<^=$\frac{q}{3}$,-*] ++ (0,-2)
;
\end{tikzpicture}

Roberto

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Circuit with capacitors and switches

Post by rais »

A starting point you'll have:

Code: Select all

\documentclass{article}
\usepackage{circuitikz}
\newcommand*\mpf[2]{% as in`my part format'
  \ensuremath{\mathrm{#1}_{#2}}%
}%\mpf{type}{number}
\newcommand*\mcf[1]{\mpf{C}{#1}}% shortcut for C..
\newcommand*\msf[1]{\mpf{S}{#1}}% shortcut for S..

\begin{document}
\begin{circuitikz}[straight voltages]
% ground:
\draw (0,0) node[sground] (gnd) {};
% By going upwards, the labels are set to the left of their components
\draw (gnd) to [short, *-] ++(-1.5,0) % junction at (gnd), then straight line to left
  coordinate (gndleft)
  ++(0,1) %space for S5
  coordinate(s4s5) % as (lower) connecting point for C1
  ++ (0,1) %space for S4
  coordinate (outleft)
  -- ++(1.5,0) coordinate (out)
  to [short, -o] ++(0,-1) node[below]{$V_{\mathrm{out}}$};
\draw (gnd) -- ++(1.5,0)
  coordinate (gndright)
  ++ (0,1) %space for S7
  coordinate (s6s7) % as (lower) connecting point for C2
  ++ (0,1) %space for S6
  coordinate (outright)
  -- (out)
  ++ (0,1) % space for S3
  coordinate (s2s3) % as (upper) connecting point for C1
  ++ (0,1) %space for S2
  coordinate (s1s2) % as (upper) connecting point for C2
  ++ (0,1) %space for S1
  coordinate (in)
  to [short, -o] ++(0,0.25) node[above]{$V_{\mathrm{in}}$};
\draw [black!20] %subdued switches:
  (gndleft) to [nos, l=\msf{5}, a=2] (s4s5)
  (s6s7) to [nos, l=\msf{6}, a=2] (outright)
  (out) to [nos, l=\msf{3}, a=2] (s2s3)
  (s1s2) to [nos, l=\msf{1}, a=2] (in);
\draw %closed switches:
  (s4s5) to [short, l=\msf{4}, a=1, *-] (outleft)
  (gndright) to [short, l=\msf{7}, a=1, -*] (s6s7)
  (s2s3) to [short, l=\msf{2}, a=1, *-*] (s1s2);
\draw (out) to [short, *-] ++ (0,0);
\draw (s4s5) -- ++(-1,0) 
  -- ++(0,1) %by using this upwards line, C1 will be placed left of S3,
%i.e., C1 won't be too close to S4, hence the voltage can be put to its right
  to [C, l=\mcf{1}, v=$\frac{V_{\mathrm{in}}}{3}$] ++ (0,1) %C1
    |- (s2s3);
\draw (s6s7) -- ++(1,0) -- ++(0,1) %same vertical position for C2 as for C1
  to [C, l=\mcf{2}, v=$\frac{2 V_{\mathrm{in}}}{3}$] ++ (0,1) %C2
    |- (s1s2);% lets the upper connecting point of C2 connect perpendicular
%to the junction between S1 and S2---first up, then left in this case
\end{circuitikz}
\end{document}
KR
Rainer
Diabloi
Posts: 7
Joined: Thu Mar 10, 2022 4:55 pm

Circuit with capacitors and switches

Post by Diabloi »

Thanks for the fast replay. :D
I create more space between the switches. I added the current arrow, but is to close on the capacitor, how can i shift in the middle?

Code: Select all

\documentclass{article}
\usepackage{circuitikz}
\newcommand*\mpf[2]{% as in`my part format'
  \ensuremath{\mathrm{#1}_{#2}}%
}%\mpf{type}{number}
\newcommand*\mcf[1]{\mpf{C}{#1}}% shortcut for C..
\newcommand*\msf[1]{\mpf{S}{#1}}% shortcut for S..

\begin{document}
\begin{circuitikz}[straight voltages]

% ground:
\draw (0,0) node[sground] (gnd) {};
% By going upwards, the labels are set to the left of their components
\draw (gnd) to [short, *-] ++(-2,0) % junction at (gnd), then straight line to left
  coordinate (gndleft)
  ++(0,1.5) %space for S5
  coordinate(s4s5) % as (lower) connecting point for C1
  ++ (0,1.5) %space for S4
  coordinate (outleft)
  -- ++(1.5,0) coordinate (out)
  to [short,i=${2q/3}$, -o] ++(0,-1) node[below]{$V_{\mathrm{out}}$};
\draw (gnd) -- ++(1.5,0)
  coordinate (gndright)
  ++ (0,1.5) %space for S7
  coordinate (s6s7) % as (lower) connecting point for C2
  ++ (0,1.5) %space for S6
  coordinate (outright)
  -- (out)
  ++ (0,1.5) % space for S3
  coordinate (s2s3) % as (upper) connecting point for C1
  ++ (0,1.5) %space for S2
  coordinate (s1s2) % as (upper) connecting point for C2
  ++ (0,1.5) %space for S1
  coordinate (in)
  to [short, -o] ++(0,0.25) node[above]{$V_{\mathrm{in}}$};
\draw [black!20] %subdued switches:
  (gndleft) to [nos, l=\msf{5}, a=2] (s4s5)
  (s6s7) to [nos, l=\msf{6}, a=2] (outright)
  (out) to [nos, l=\msf{3}, a=2] (s2s3)
  (s1s2) to [nos, l=\msf{1}, a=2] (in);
\draw %closed switches:
  (s4s5) to [short, l=\msf{4}, a=1, *-] (outleft)
  (gndright) to [short, l=\msf{7}, a=1, -*] (s6s7)
  (s2s3) to [short, l=\msf{2}, a=1, *-*] (s1s2);
\draw (out) to [short, *-] ++ (0,0);
\draw (s4s5) -- ++(-1.5,0) 
  -- ++(0,1) %by using this upwards line, C1 will be placed left of S3,
%i.e., C1 won't be too close to S4, hence the voltage can be put to its right
  to [C, l=\mcf{1}, v=$\frac{V_{\mathrm{in}}}{3}$] ++ (0,1) %C1
    |- (s2s3);
\draw (s6s7) -- ++(1.5,0) -- ++(0,1) %same vertical position for C2 as for C1
  to [C, l=\mcf{2},i_>=$q/3$, v=$\frac{2 V_{\mathrm{in}}}{3}$] ++ (0,1) %C2
    |- (s1s2);% lets the upper connecting point of C2 connect perpendicular
%to the junction between S1 and S2---first up, then left in this case
\end{circuitikz}
\end{document}
here the other code:

Code: Select all

\begin{circuitikz}[american] %increase the thickness of the line
%Grid for initial drawing.
%Comment next three lines once typesetting finished
\def\figHt{7};
\def\figWd{10};
\draw[step=0.5,very thin, black!20] (-0.5,-1) grid (\figWd,\figHt);
\foreach \x in {0,...,\figWd} {\node [anchor=north] at (\x,-1) {\x};}
\foreach \y in {-1,...,\figHt} {\node [anchor=east] at (-0.5,\y) {\y};}


%Instantiation of components
\draw (1,1) to[C,l^=$C_1$,-] (1,3);
\draw (9,4) to[C,l^=$C_2$] (9,1);

%interconnections
\draw (5,5.5) to[short, -o] (5,6) node[anchor=east]{$V_{in}$};
\draw (5,6) to[nos,l_=$S_1$,color=gray,-](5,4);
\draw (5,4) to[nos,l_=$S_2$,color=gray,-](5,3);
\draw (5,4) to [short,i_<=$q/3$,*-] (9,4);
\draw (5,3) to [short,i_>=$q/3$,*-] (1,3);
\draw (5,3) to[nos,l_=$S_3$,color=gray,-*](5,2);
\draw (3,2) -- (7,2);
\draw (3,2) to[nos,l_=$S_4$,color=gray,-*](3,1);
\draw (1,1) to [short,-*] (3,1);
\draw (3,1) to [nos,l_=$S_5$,color=gray,-](3,0);

\draw (7,2) to[nos,l_=$S_6$,color=gray,-*](7,1);
\draw (9,1) to [short,-*] (7,1);
\draw (7,1) to [nos,l_=$S_7$,color=gray,-](7,0);
\draw (3,0) to[short, -] (7,0);

\draw (5,0) to (5,0) node[sground]{};
\draw (5,2) to[short,i_>=$q2/3$,*-o] (5,1) node[anchor=west]{$V_{out}$};

\end{circuitikz}
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Circuit with capacitors and switches

Post by rais »

You could increase the height for the capacitor, but that would also lengthen its voltage arrow.
Simply add a short and put the current information there:

Code: Select all

\documentclass{article}
\usepackage{circuitikz}
\newcommand*\mpf[2]{% as in`my part format'
  \ensuremath{\mathrm{#1}_{#2}}%
}%\mpf{type}{number}
\newcommand*\mcf[1]{\mpf{C}{#1}}% shortcut for C..
\newcommand*\msf[1]{\mpf{S}{#1}}% shortcut for S..

\newlength\switchdistance %to keep the S4..S7 block symmetrical
\setlength\switchdistance{2cm}

\begin{document}
\begin{circuitikz}[straight voltages]

% ground:
\draw (0,0) node[sground] (gnd) {};
% By going upwards, the labels are set to the left of their components
\draw (gnd) to [short, *-] ++(-\switchdistance,0) % junction at (gnd), then straight line to left
  coordinate (gndleft)
  ++(0,1.5) %space for S5
  coordinate(s4s5) % as (lower) connecting point for C1
  ++ (0,1.5) %space for S4
  coordinate (outleft)
  -- ++(\switchdistance,0) coordinate (out)
  to [short,i=${2q/3}$, -o] ++(0,-1) node[below]{$V_{\mathrm{out}}$};
\draw (gnd) -- ++(\switchdistance,0)
  coordinate (gndright)
  ++ (0,1.5) %space for S7
  coordinate (s6s7) % as (lower) connecting point for C2
  ++ (0,1.5) %space for S6
  coordinate (outright)
  -- (out)
  ++ (0,1.5) % space for S3
  coordinate (s2s3) % as (upper) connecting point for C1
  ++ (0,1.5) %space for S2
  coordinate (s1s2) % as (upper) connecting point for C2
  ++ (0,1.5) %space for S1
  coordinate (in)
  to [short, -o] ++(0,0.25) node[above]{$V_{\mathrm{in}}$};
\draw [black!20] %subdued switches:
  (gndleft) to [nos, l=\msf{5}, a=2] (s4s5)
  (s6s7) to [nos, l=\msf{6}, a=2] (outright)
  (out) to [nos, l=\msf{3}, a=2] (s2s3)
  (s1s2) to [nos, l=\msf{1}, a=2] (in);
\draw %closed switches:
  (s4s5) to [short, l=\msf{4}, a=1, *-] (outleft)
  (gndright) to [short, l=\msf{7}, a=1, -*] (s6s7)
  (s2s3) to [short, l=\msf{2}, a=1, *-*] (s1s2);
\draw (out) to [short, *-] ++ (0,0);
\draw (s4s5) -- ++(-1.5,0) 
  -- ++(0,1)
  to [C, l=\mcf{1}, v=$\frac{V_{\mathrm{in}}}{3}$] ++ (0,1) %C1
  |- (s2s3);
\draw (s6s7) -- ++(1.5,0) -- ++(0,1) %same vertical position for C2 as for C1
  coordinate (c2bot) %just for demo
  to [C, l=\mcf{2}, v=$\frac{2 V_{\mathrm{in}}}{3}$] ++ (0,1) %C2
  coordinate (c2top) %just for demo
  to [short, i_>=$q/3$] ++ (0,2)
  |- (s1s2);% lets the upper connecting point of C2 connect perpendicular
%to the junction between S1 and S2---first up, then left in this case
\draw [red] (c2bot) -- +(0.5,0) (c2top) -- +(0.5,0); %just for demo
\end{circuitikz}
\end{document}
Note that the to[..] operation does not work with the |- path construct.
Should you see a vertical line protruding over the upper horizontal connecting line for the capacitor, your total height of line below C, height of C, plus line above C is already greater than the vertical space between (s6s7) and (s1s2) is available (respectively (s4s5) to (s2s3) for C1).
I've added two red lines to indicate the capacitor's height. Once you're satisfied with the result, remove the lines marked `just for demo'.
I've also added a new length to make it easier to keep the lines for S4/S5 and S6/S7 symmetrical to (gnd) -- and Vin, Vout, and gnd on one line.

KR
Rainer
Diabloi
Posts: 7
Joined: Thu Mar 10, 2022 4:55 pm

Circuit with capacitors and switches

Post by Diabloi »

Thanks for the help, I will try your technic. maybe it is easier. :D
Post Reply