Calculate Pulses per Revolution for a DC Motor With Encoder

In this tutorial, we will learn how to calculate the number of pulses per 360 degree revolution for a DC motor with a built-in encoder. The motor that we will work with looks like the following image, however you can use any motor that looks similar to it:

1_dc_motor_encoder_wheels

Real-World Applications

When we know the number of pulses that an encoder outputs for each 360-degree turn of a motor, we can use that information to calculate the angular velocity of the wheels (in radians per second). 

When we know the angular velocity of the wheels on a robot and the radius of the wheels, we can calculate how fast the robot is moving (i.e. speed) as well as the distance a robot has traveled in a given unit of time. This information is important for helping us determine where a robot is in a particular environment.

Prerequisites

  • You have the Arduino IDE (Integrated Development Environment) installed on either your PC (Windows, MacOS, or Linux).

You Will Need

This section is the complete list of components you will need for this project (#ad).

OR

  • Self-Balancing Car Kit (which includes everything above and more…Elegoo and Osoyoo are good brands you can find on Amazon.com)

Disclosure (#ad): As an Amazon Associate I earn from qualifying purchases.

What is a Pulse?

When a motor with a built-in encoder rotates, it generates pulses, which are alternating electrical signals of high voltage and low voltage. Each time the signal goes from low to high (i.e. rising), we count that as a single pulse.

time-intervalJPG

Our goal is to take our motor and measure the number of encoder pulses (often referred to as “ticks”) it generates in a single 360 degree turn of the motor.

Set Up the Hardware

The first thing we need to do is set up the hardware.

Here is the wiring diagram:

jgb37_dc_motor_with_encoder-1
  • The Ground pin of the motor connects to GND of the Arduino.
  • Encoder A (sometimes labeled C1) of the motor connects to pin 2 of the Arduino. Pin 2 of the Arduino will record every time there is a rising digital signal from Encoder A.
  • Encoder B (sometimes labeled C2) of the motor connects to pin 4 of the Arduino. The signal that is read off pin 4 on the Arduino will determine if the motor is moving forward or in reverse. We’re not going to use this pin in this tutorial, but we will use it in a future tutorial.
  • The VCC pin of the motor connects to the 5V pin of the Arduino. This pin is responsible for providing power to the encoder.
  • For this project, you don’t need to connect the motor pins (+ and – terminals) to anything since you will be turning the motor manually with your hand.

Write and Load the Code

Now we’re ready to calculate the number of encoder pulses per revolution. Open the Arduino IDE, and write the following program. The name of my program is pulses_per_revolution_counter.ino.

/*
 * Author: Automatic Addison
 * Website: https://automaticaddison.com
 * Description: Count the number of encoder pulses per revolution.  
 */

// Encoder output to Arduino Interrupt pin. Tracks the pulse count.
#define ENC_IN_RIGHT_A 2

// Keep track of the number of right wheel pulses
volatile long right_wheel_pulse_count = 0;

void setup() {

  // Open the serial port at 9600 bps
  Serial.begin(9600); 

  // Set pin states of the encoder
  pinMode(ENC_IN_RIGHT_A , INPUT_PULLUP);

  // Every time the pin goes high, this is a pulse
  attachInterrupt(digitalPinToInterrupt(ENC_IN_RIGHT_A), right_wheel_pulse, RISING);
  
}

void loop() {
 
    Serial.print(" Pulses: ");
    Serial.println(right_wheel_pulse_count);  
}

// Increment the number of pulses by 1
void right_wheel_pulse() {
  right_wheel_pulse_count++;
}

Compile the code by clicking the green checkmark in the upper-left of the IDE window.

Connect the Arduino board to your personal computer using the USB cord.

Load the code we just wrote to your Arduino board.

Now, follow the following steps in the image below.

open-serial-monitorJPG

When you open the Serial Monitor, the pulse count should be 0.

2_starting_pulses

Using your hand, rotate the motor a complete 360-degree turn.

Here is the output. We can see that there were 620 pulses generated. 

3-ending-pulses

Thus, this motor generates 620 pulses per revolution.

That’s it. Keep building!

How to Install ROS Melodic on the NVIDIA Jetson Nano

In this tutorial, I will show you everything you need to know to install ROS Melodic on the NVIDIA Jetson Nano. The official tutorial is here and my quick tutorial is here, but I will run through all the steps below.

Prerequisites

You have set up your NVIDIA Jetson Nano Developer Kit.

Directions

In the Start Menu, go to Preferences -> Software & Updates. Then click on it.

Make sure main, universe, restricted, and multiverse are all checked. Then close the window.

2021-04-02-140620

Now open up a new terminal window, and type (or copy and paste) the following command:

sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'

The command above sets your computer up to accept software from packages.ros.org. 

Now we need to set up the secure keys so that our system accepts what we are going to download. 

For the next step, update the package list on your system.

sudo apt update

Now type:

sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654

Now do a full desktop install of ROS. The command below installs all the software, tools, algorithms, and robot simulators for ROS. After you type the command and press Enter, press Y and hit Enter when asked if you want to continue. It will take a while to download all this stuff, so feel free to take a break while ROS downloads to your system.

sudo apt-get update
sudo apt install ros-melodic-desktop-full

Type Y and press Enter to complete the installation.

Set up the environment variables.

echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
source ~/.bashrc

Note that anytime you want to see what environment variables ROS is using, you can type the following command:

export | grep ROS

This command will show you the ROS distribution you are using, the version of Python ROS is using, and a bunch of other stuff.

The two key variables are as follows:

ROS_MASTER_URI: Shows the URL where the roscore is in execution. This is often your own local computer.

ROS_PACKAGE_PATH: Shows the path on your computer where the ROS packages are.

Install some other tools that you will work with in ROS. After you type the command below, press Y and Enter to complete the download process.

sudo apt install python-rosdep python-rosinstall python-rosinstall-generator python-wstool build-essential

Press Y and hit Enter.

Now initialize rosdep. This is a tool that is required before you can use ROS.

sudo apt install python-rosdep
sudo rosdep init
rosdep update

Here is the last step of the installation process. Check which version of ROS you have installed. If you see your ROS version as the output, congratulations you have successfully installed ROS!

rosversion -d
2021-04-02-144436

How to Install and Demo the Webots Robot Simulator for ROS 2

In this tutorial, I will show you how to install, build, and demo the ROS 2 package for Webots, the free open-source 3D robot simulator created by Cyberbotics Ltd. Installing Webots was a bit difficult, so it is important you follow the steps below click-by-click, command-by-command so that everything installs properly. Here is what the demo looks like:

Prerequisites

ROS 2 Foxy Fitzroy installed on Ubuntu Linux 20.04 (if you are using another distribution, you will need to replace ‘foxy’ with the name of your distribution).

Install

Below are the exact steps I took to install, build, and demo Webots successfully.

First, open a new terminal window, and create a new workspace. You can call it any name, but we will call it “webots”. Inside the workspace, we will create a source (i.e. src) directory. This is where your packages will go.

mkdir -p ~/webots/src

Navigate to the workspace.

cd ~/webots/src

To install the Webots software, open a new terminal window, and type the following commands:

sudo apt-get install ros-$ROS_DISTRO-webots-ros2

Type Y and then Enter to continue.

Next, we need to clone the repository into our workspace.

git clone https://github.com/cyberbotics/webots_ros2.git

Let’s see if the package is in the source folder of our workspace.

ls
1-in-our-workspaceJPG

Open a new terminal window, and try to run the demo.

ros2 launch webots_ros2_demos armed_robots.launch.py

You will be asked if you want to install the latest version of Webots.

Type Y and press Enter.

It will take some time to install.

2-install-webotsJPG

You will see this demo pop up.

3-demo-armJPG

Press CTRL+C in all terminal windows to shutdown the demo.

Go to the following directory:

cd webots/src/webots_ros2/webots_ros2_importer/webots_ros2_importer
git clone https://github.com/cyberbotics/urdf2webots.git
cd urdf2webots
pip3 install -r requirements.txt

At this stage, I was having install errors when I tried to build the package. So here is what I did next.

Remove the webots package. Either go to the file explorer in Linux and delete the webots_ros2 folder manually, or you can run the following commands to delete the folder.

cd ~/webots/src
rmdir webots_ros2

Now, download the package again.

cd ~/webots/src
git clone --recurse-submodules -b $ROS_DISTRO https://github.com/cyberbotics/webots_ros2.git webots_ros2

Build

Go to the root of the workspace.

cd ~/webots/

Check the dependencies.

rosdep update
rosdep install --from-paths src --ignore-src --rosdistro $ROS_DISTRO
colcon build

Add the sourcing of the workspace to the bashrc file.

gedit ~/.bashrc

At the bottom of the bash file, add the following line:

source ~/webots/install/setup.bash

Demo

How to Run the Robotic Arm Demo

Open a new terminal window, and type the following command.

ros2 launch webots_ros2_demos armed_robots.launch.py

When you’re done watching the demo, press CTRL + C in all terminal windows.

That’s it. Keep building!