TikZ: How to draw an optical encoder wheel?

For example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
  \def\innerRadius{30mm}
  \def\outerRadius{40mm}
  \def\wheelMarks{50}
  \def\cRadius{2mm}
  \def\cLen{8mm}
  \coordinate (c1) at (0, 0);

  % Wheel markings
  \fill
    let \n0={360/\wheelMarks} in
      \foreach \i in {1, ..., \wheelMarks} {
        let \n1={\i*\n0} in
          (c1) -- (\n1:\outerRadius)
          arc(\n1:\n1 + \n0/2:\outerRadius)
          -- ($(c1) + (\n1 + \n0/2:\innerRadius)$)
          arc(\n1 + \n0/2:\n1:\innerRadius)
          -- cycle
    }
  ;

  \draw
    % Wheel circles
    (c1) circle[radius=\outerRadius]
         circle[radius=\innerRadius]
    % Center markings with circle and cross
         circle[radius=\cRadius]
    ($(c1) - (\cLen, 0)$) -- ++(2*\cLen, 0)
    ($(c1) - (0, \cLen)$) -- ++(0, 2*\cLen)
  ;

\end{tikzpicture}
\end{document}

Result


another solution to make absolute encoder

enter image description here

\documentclass[border=5mm,tikz]{article}
\usepackage{mwe}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}

\usepackage{xcolor}
\begin{document}


\begin{tikzpicture}
\pgfmathsetmacro{\Ra}{4}

\foreach \nb in {0,1,2,3,4,5}{
\pgfmathsetmacro{\Rext}{\Ra+\nb*0.8}
\pgfmathsetmacro{\Rint}{\Rext-0.8}
\draw (0,0) circle (\Rext);
\pgfmathsetmacro{\nbfentes}{2^\nb}
\pgfmathsetmacro{\pas}{180/\nbfentes}
\foreach \aa in {1,...,\nbfentes}{
\draw[fill,rotate=2*\aa*\pas] (0:\Rint) -- (0:\Rext) arc (0:\pas:\Rext) -- (\pas:\Rint ) arc (\pas:0:\Rint);
}
}

\end{tikzpicture}

\end{document}

Another solution is to use dashed circle in place of a loop.

\documentclass[tikz,border=7mm]{standalone}
\usetikzlibrary{angles}

\begin{document}
  % parameters to set
  \def\minR{3}
  \def\maxR{4}
  \def\numMarks{50}
  % some calculations
  \pgfmathsetmacro{\midR}{(\minR+\maxR)/2}
  \pgfmathsetmacro{\dR}{\maxR-\minR}
  \pgfmathsetmacro{\dM}{2*pi*\midR/(2*\numMarks)}
  % the picture
  \begin{tikzpicture}
    \draw circle(\minR/4) (-\minR/2,0) -- (\minR/2,0)  (0,-\minR/2) -- (0,\minR/2);
    \draw circle(\minR) circle(\maxR);
    \draw[line width=\dR cm,dash pattern=on \dM cm off \dM cm] circle(\midR);
  \end{tikzpicture}
\end{document}

enter image description here

Tags:

Tikz Pgf