## Closed-loop approach: setpoint, RPM counter, and feedback control

DC motors are cool. However, it is not always an easy task to accurately control their speed. To that purpose, we’re gonna be developing a closed-loop RPM control. The benefits of a closed-loop control to regulate the motor speed are:

• Accurate speed control
• The speed is kept constant even if the shaft load changes during its operation
• The model of DC motor that you have doesn’t really matter: it will spin at the desired RPM.

The idea we are going to implement here is the following one:

You can see how the real RPM are fed back into the controller by substracting them to the desired ones. The result is what control systems engineers call “the error signal”

 Purchase suggestionIf you need a small DC motor, I suggest you to grab this cheap 10 pcs. kit from Amazon. Their working voltage can go from 3 up to 6 volts DC and they can spin at up to 17000 RPM.

## Driving a motor from an Arduino I/O pin

There are several ways to achieve this. The simplest, and most straight-forward one is to use an NPN transistor and PWM its base through a 220 Ohm resistor. Needless to say that we will introduce a lot of noise in the motor (and the circuit, in general) if we choose to go with this option:

VCC will, in general, be provided by a power supply other than Arduino’s one. This is done because DC motors normally require more current than Arduino’s internal power supply can provide. Do not connect VCC to the 5V pin in your Arduino: you might damage your board.

Also, note that D1 can be replaced by a 1N4148 small signal diode (as a rule of thumb) if the motor draws less than 100mA (which is usually not the case). Any 1N400X will be fine here.

The selected NPN transistor for the job is a 2N2222, but if your motor draws more than 200 mA, or you exceed the power dissipation specified under the “Maximum Absolute Ratings” of this transistor datasheet, you are strongly advised to replace the 2N2222 by another one that supports more current such as the TIP 120.

If we were to implement this circuit in a breadboard, it would look like this:

Let’s say that this approach is enough for our project. For reduced noise and better control, consider using an L293D or an L298 H Bridge DC motor controller. Find out more here.

## The RPM counter: LED/Photodiode selection and delay measurement

How can we determine the RPM at which the DC motor is spinning. Well, the general idea here is to place an infrared LED (Ir-LED) which will emit a light beam, that will be received by a photodiode when a slotted, spinning disk passes through that beam. The photodiode output will be LOW at all times except when the disk allows the light to pass. If we measure the distance from peak to peak, we can calculate the period of the wave and, thus the frequency, which can then be converted into RPM.

### So, do we use a photodiode or a phototransistor?

Photodiodes are based on a single P-N junction, and they are generally less sensitive but have faster response than phototransistors. Phototransistors use a regular NPN transistor with its base exposed. Photons excite the base and act as a base current injected to a regular NPN transistor. They are slower, but more sensitive. As we are counting RPM (a fast process), the choice seems (and it is) obvious. I have decided to use the TEST2600 (datasheet) from Vishay Semiconductors (R).

TEST2600 photodiode, from Vishay Semiconductors (R).

The problem here, is that the disk might be able to spin fast enough so that the photodiode doesn’t have time to produce a spike before the next revolution is completed. That is the delay of our system (if we don’t take into account the microcontroller processing time). We can try to roughly calculate the frequency at which our photodiode needs to be able to operate (the minimum bandwidth of the system) for our RPM counter to work:

Let’s consider that a DC motor can spin, at low voltages, at approximately 1500 rpm; the frequency would then be:

$$f={n}/{60}={1200}/{60}=20 Hz$$

So we need around $20 Hz$ of minimum bandwidth. Let’s see how can we test that low frequency.

### Measuring the delay

The delay can be measured by sending a pulse to the IR-emitting diode and using a scope to determine the phase difference between the input and the output. We will use the classic NE555 timer to generate a square wave, that will be sent to the IR LED (AM4457F3C). The square wave will be received by the photodiode, which will generate a current that will generate a voltage spike across the resistor of the magnitude:

$$V_{out}=R_{2}·(I_{dark}+\beta L)$$

L is the amount of received light, and $\beta$ a linearization constant that depends on the photodiode. The circuit configuration is the following one (use this GREAT 555 calculator):

If we run this experiment, what we observe by measuring the output (between $R_2$ and the emitter (E terminal) of U2) is the following:

We see that we get 0.4 V of offset at the output due to the current that flows across the photodiode at 0 received light $I_{dark}$, but we get nice edges with almost 0 delay.

## Building the widget

We now need to keep constantly switched on the led and create the square signal by spinning a slotted disk between the emitter and the receiver. The disk I made out of a cardboard box is this one:

I then used a small cardboard box to place the Ir-LED and the photodiode on it, and the disk on the lid, so that when you close it, the disk lies between both:

I then placed a potentiometer and  an OLED screen to show the setpoint, the control PWM applied and the current RPM:

## Coding and wiring it

We need to connect several things to our Arduino Nano, find the summary here:

Pin numberPin ConnectionPin Mode
2Photodiode output (R2 voltage)Digital Input
32N2222 base through 220 ohm resistorPWM Output
6OLED_DCSPI (OLED)
7OLED_CSSPI (OLED)
8OLED_RESETSPI (OLED)

The code for the RPM counter is the following one (please note that I tried to code it with interrupts but it yielded strange results due to the noise produced by the ‘PWMed’ DC motor):

## Final result

And here we have the final result!