Arduino Nano and Visuino: Measure Motor Speed (RPM) With Optocoupler and Encoder Disk

by BoianM in Circuits > Arduino

17229 Views, 48 Favorites, 0 Comments

Arduino Nano and Visuino: Measure Motor Speed (RPM) With Optocoupler and Encoder Disk

Arduino Nano and Visuino: Measure motor speed (RPM) with Optocoupler and Encoder disk

When building a robot car, it is important to control the speed of the rotation of the wheels. Before we can control the speed however, we need to be able to measure it. One of the easiest and most popular ways of measuring the speed is with Optocoupler, and Encoder Wheel. Most of the Smart Car kits come with Encoder Wheels included, and I already made Instructable on how to connect Optocoupler to Arduino . Now I will show you how you can use the Optocoupler to measure the rotation speed of the wheel.

Components

573666aa50e1b6e809000611.jpeg
57366b8e4fbadee84a001037.jpeg
57366d264fbadee84a00103b.jpeg
  1. One Arduino compatible board (I use Arduino Nano, because I have one, but any other will be just fine)
  2. One Optocoupler module with LM393 comparator with digital output (Picture 2)
  3. One Smart Car chassis with at least one DC motor, and rotary encoder wheel (Picture 3)
  4. 3 Female-Female jumper wires

Connect the Optocoupler to the Arduino Board

5736653e45bceb59260008e7.jpeg
5736654f45bceb59260008e8.jpeg
56a6a62867400cfc2b000911.jpeg
573665e84936d47261000c55.jpeg
573670874936d47261000c76.jpeg
  1. Connect Power(Red wire), and Ground(Black wire) to the Optocoupler Module (Picture 1 and 2)
  2. Connect Data(Gray wire) to the digital pin of the Optocoupler Module (Picture 1 and 2)
  3. Connect the other end of the Ground wire(Black wire) to Ground pin of the Arduino board(Picture 3)
  4. Connect the other end of the Power wire(Red wire) to the 5V power pin of the Arduino board(Picture 3)
  5. Connect the other end of the Data wire(Gray wire) to Digital pin 2 of the Arduino board(Picture 4)
  6. Picture 5 shows where are the Ground, 5V Power, and Digital 2 pins of the Arduino Nano.

Mount the Optocoupler on the Smart Car

573666044fbaded08c000186.jpeg
57366628937ddb2aaf001a20.jpeg
5736661845bceb5213001513.jpeg

Mount the Optocoupler on the Smart Car so the Encoder Wheel will be inserted in the slot of the Optocoupler (Pictures 1, 2 and 3)

Start Visuino, and Select the Arduino Board Type

SelectBoardTolsEdited.png
Select BoardNano.png

To start programming the Arduino, you will need to have the Arduino IDE installed from here: http://www.arduino.cc/.

Please be aware that there are some critical bugs in Arduino IDE 1.6.6.

Make sure that you install 1.6.7 or higher, otherwise this Instructable will not work!

The Visuino: https://www.visuino.com also needs to be installed.

  1. Start Visuino as shown in the first picture
  2. Click on the "Tools" button on the Arduino component (Picture 1) in Visuino
  3. When the dialog appears, select Arduino Nano as shown in Picture 2

In Visuino: Add and Connect Frequency Meter Component

ComponentFrequencyMeter.png
ComponentFrequencyMeterConnect1.png
ComponentFrequencyMeterConnect2.png
  1. Type "freq" in the Filter box of the Component Toolbox then select the "Frequency Meter" component (Picture 1), and drop it in the design area
  2. Connect the "Out" output pin of the Digital[ 2 ] channel of the Arduino component to the "In" input pin of the FrequencyMeter1 component (Picture 2)
  3. Connect the "Out" output pin of the FrequencyMeter1 component to the "In" input pin of the Serial[ 0 ] channel of the Arduino component (Picture 3)

Generate, Compile, and Upload the Arduino Code

GenerateCtopped.png
ArduinoIDE.png
  1. In Visuino, Press F9 or click on the button shown on Picture 1 to generate the Arduino code, and open the Arduino IDE
  2. In the Arduino IDE, click on the Upload button, to compile and upload the code (Picture 2)

And Play...

5736668c15be4de6c7000d62.jpeg
VisuinoDiagramCrop.png
StartSerial.png
StartSerialText.png
StartSerialScope.png

On Picture 1 you can see the completed and powered project.

  1. In Visuino select the Serial Port and click the Connect button (Picture 3)
  2. In the Serial Terminal you will see the distance measured by the sensor (Picture 4)
  3. If you click on the Scope Tab you can also see the values plotted in the Scope (Picture 5)

Congratulations! You have learned how to measure the speed of the motor rotation with Optocoupler and Encoder wheel.

On Picture 2 you can see the complete Visuino diagram.

Since the encoder wheel has multiple holes it generates multiple pulses per rotation. My wheel has 20 holes, so it generates 20 pulses. To properly convert the result to RPM (Revolutions Per Minute) you will need to divide the measured value by 20. Look at the next Step to see how to do this.

In Visuino: Add and Connect Divide by Value Component

ComponentDisconnect.png
ComponentDivide.png
ComponentDivideConnect1.png
ComponentDivideConnect2.png
ComponentDivideProperty.png
  1. Right click over the connection between the "Out" pin of the FrequencyMeter1 component and the Serial Port (Picture 1)
  2. From the menu select the "Disconnect link" to disconnect the "Out" pin of the FrequencyMeter1 component (Picture 1)
  3. Type "divid" in the Filter box of the Component Toolbox then select the "Divide By Value" component (Picture 2), and drop it in the design area
  4. Connect the "Out" output pin of the FrequencyMeter1 component to the "In" input pin of the DivideByValue1 component (Picture 3)
  5. Connect the "Out" output pin of the DivideByValue1 component to the "In" input pin of the Serial[ 0 ] channel of the Arduino component (Picture 4)
  6. In the Object Inspector set the value of the "Value" property to "20" (Picture 5)

And Play...

VisuinoDiagram2Crop.png
Arduino Nano and Visuino: Measure motor speed (RPM) with Optocoupler and Encoder disk
  1. Generate, Compile, and Upload the Arduino code as you did in Step 6
  2. If you power the project and connect to Arduino over the serial port, as you did in Step 7, you will see the speed displayed in RPM

Congratulations! You have learned how to measure the speed of the motor, and convert it to RPM (Revolutions Per Minute).

The Picture shows the complete Visuino diagram. On the video you can see the running project before the Divide By Value is added. The final project looks and works the same way, except the displayed values will be divided by 20.

Also attached is the Visuino project, that I created for this Instructable. You can download and open it in Visuino: https://www.visuino.com