How to make animation to show how the electric current flow works?

I have a circuit, I want to show how the current move in this circuit, i use this code:

\documentclass[tikz]{standalone}

\usepackage{tikz}
\usepackage{animate}
\usepackage{circuitikz}

\begin{document}
\begin{animateinline}[auto play]{1}
\multiframe{2}{i=0+0.2}{
\begin{circuitikz}
\draw (0,0)
to[V,v=$U_q$] (0,2) % The voltage source
to[short] (2,2)
to[R=$R_1$] (2,0) % The resistor
to[short] (0,0);
\draw (2,2)
to[short] (4,2)
to[R=$R_2$] (4,0)
to[short] (2,0);
\draw (0,0)
to[thick,color=red, -*] (0,\i) % The voltage source
to[thick,color=red, -*] (\i,2)
to[thick,color=red, -*] (2,{2-\i}) % The resistor
to[thick,color=red, -*] ({2-\i},0);
\draw (2,2)
to[thick,color=red, -*] ({2+\i},2)
to[thick,color=red, -*] ({2+\i},0)
to[thick,color=red, -*] ({4-\i},0);
\end{circuitikz}}
\end{animateinline}
\end{document}


in this animations I will explain the Ohm's law, and show how the current move in the conductor and slow down through a resistor

Update I modified the code, that work well, but i need to show how the current slow down through a resistor, can anyone help me to complete this code?

\documentclass[border=30pt, tikz]{standalone}
\usepackage{tikz,tikz-3dplot}
\usepackage{xparse}
\usepackage{animate, calc}%%%%%%%%%%%%
\usepackage{animate}
\usepackage{circuitikz}

\ExplSyntaxOn
\NewDocumentCommand { \xifnum } { }
{
\fp_compare:nTF
}
\ExplSyntaxOff

\begin{document}
\foreach \x in {0.1,0.2,...,4}{
\begin{tikzpicture}
\begin{circuitikz}
\draw (0,0)
to[V,v=$U_q$] (0,2) % The voltage source
to[short] (2,2)
to[R=$R_1$] (2,0) % The resistor
to[short] (0,0);
\draw (2,2)
to[short] (4,2)
to[R=$R_2$] (4,0)
to[short] (2,0);
\xifnum{\x<2}{\fill[color=red] (0,\x) circle (3pt);}{\fill[color=red] ({\x-2},2) circle (3pt);}
\end{circuitikz}
\end{tikzpicture}
}
\end{document}

• Welcome to TeX.SE. Excuse me, but what is your problem? – Sebastiano Apr 13 '18 at 21:59
• i want to make animation to show how the how the electric current flow move through in a resistor – moradov Apr 13 '18 at 22:03
• I, and I think many of the good LaTeX users, we would like to know if you have problems compiling your code, or you want to add or remove something that does not work well. – Sebastiano Apr 13 '18 at 22:06
• the code I presented above, not compile for me, I presented it as a minimal working example, so that someone can complete or modify it, my goal is to make an animation, with circuitikz – moradov Apr 13 '18 at 22:37
• @Sebastiano Could you compile this code? I cannot. So maybe the question/task is to make it compilable and add more frames such that this becomes a smooth animation. – user121799 Apr 13 '18 at 23:43

I have tidied up your code and continued the animation through R1 with a different speed. I have not used the animate package because it seems that my pdf viewer (evince) is not able to display those animations.

I am using relative coordinates (++) for better maintainability, I am using named coordinates for better readability, I have defined an environment and a command to avoid code duplication, I have defined three commands for easier customization and I have splitted up the for loop into several loops because I think that's easier.

You can customize the speed by changing \stepconductor and \stepresistor. It is advisable to choose them so that 1 is a multiple of them.

You can customize the size of the red dot by changing \currentradius.

\documentclass[border=30pt, tikz]{standalone}
\usepackage{circuitikz}

\newcommand{\stepconductor}{.1}
\newcommand{\stepresistor}{.025}

\newenvironment{mycircuit}{%
\begin{circuitikz}
\draw (0,0) coordinate (V-)
to[V,v=$U_q$] ++(0, 2) coordinate (V+)
to[short]     ++(2, 0) coordinate (R1+)
to[R=$R_1$]   ++(0,-2) coordinate (R1-)
to[short]     (V-);
\draw (R1+)
to[short]     ++(2, 0) coordinate (R2+)
to[R=$R_2$]   ++(0,-2) coordinate (R2-)
to[short]     (R1-);

% ensure that tikzpicture has always the same size (independent of the position of the current)
}{%
\end{circuitikz}%
}
\newcommand{\current}[2]{%
\path (#1) -- node[pos=\p, circle, fill=red, minimum size=2*\currentradius, inner sep=0pt]{} (#2);
}

\begin{document}
\foreach \p in {0, \stepconductor, ..., 1}{
\begin{mycircuit}
\current{V-}{V+}
\end{mycircuit}
}
\foreach \p in {0, \stepconductor, ..., 1}{
\begin{mycircuit}
\current{V+}{R1+}
\end{mycircuit}
}
\foreach \p in {0, \stepresistor, ..., 1}{
\begin{mycircuit}
\current{R1+}{R1-}
\end{mycircuit}
}
\foreach \p in {0, \stepconductor, ..., 1}{
\begin{mycircuit}
\current{R1-}{V-}
\end{mycircuit}
}
\end{document}


I have converted the pdf to a gif with the following command (thanks to Henri Menke):

convert -density 300 -delay 8 -loop 0 -background white -alpha remove main.pdf main.gif

• thank you @jakun, it's amusing animation, i would also like , to add many red dot, and shaw how the current slow down through the resistor R2, and circulate in all the conductor in the same way – moradov Apr 14 '18 at 10:54