0% found this document useful (0 votes)
10 views

Build a Robot Car With Speed Sensors Using Arduino Interrupts

Uploaded by

samuelmuasya763
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Build a Robot Car With Speed Sensors Using Arduino Interrupts

Uploaded by

samuelmuasya763
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

Welcome to the Workshop! Come and explore the new website features.

DroneBot Workshop
Arduino, Electronics, IoT, Raspberry Pi and Robots – Welcome to the Workshop!

Search 

Home Arduino Raspberry Pi ESP32 Electronics Robots Forum YouTube


About

Build a Robot Car with Speed Sensors

Table of Contents [hide] Change Text Size


1 Introduction 90% 100% 110% 120%
2 Robot Car Chassis
3 Making Plans
4 Optical Interrupter Sensor
5 Add an LM393 Comparator
6 Calculating Wheel Speed
6.1 Step 1 – Preparing the Chassis
6.2 Step 2 – Prepare the Motors
6.3 Step 3 – Mount the Motors on the Chassis
6.3.1 Style 1 – Acrylic Mount
6.3.2 Style 2 – Aluminum Mount
6.4 Step 4 – Install the Wheels, Rotary Encoders and Sensors
6.5 Step 5 – Install the Castor
6.6 Step 6 – Install your Components
6.6.1 Install the Battery Holder
6.6.2 Mounting the Arduino
Let's keep in touch!
6.6.3 Mounting the L298N Motor Driver
6.7 Step 7 – Wire it Up!

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 1/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

7 Interrupts Please subscribe to the newsletter


7.1 Types of Interrupts and keep up to date with what is
7.2 Coding for Interrupts
happening in the workshop.
7.3 Better Interrupt Code
8 Testing the Speed Sensors
Zero spam, no sales - just useful
9 Robot Car Sketch
9.1 Playing with the code information!
10 The End – Not Really! Subscribe Today!
10.0.1 Parts List
10.0.2 Resources

A rite of passage for every budding robotics experimenter is to


build a small robot car. This task is simplified by using one of
those robot car chassis kits which can be picked up on eBay and
Amazon for less than 15 dollars. And while these kits are a great
value they lack instructions, especially for using the small speed
encoder disk that comes packaged with them. Let’s see how to
make use of that disk and build a robot car with speed sensors. Latest Articles

Arduino GIGA R1 WiFi

Use a PS3 Controller with an


ESP32

Using ChatGPT to Write Code for


Arduino and ESP32

Using the ESP32 DAC (and making


a Fruity Instrument)

Mecanum Wheel Robot Car with


ESP-NOW Remote Control

Introduction
Many of the robotics projects that I’ve been working on lately
have made use of a very common “Robot Car Chassis”. These
tiny units have a lot of advantages for robotics experimenters,
especially ones like myself whose expertise lies with electronics
and software and not with mechanics:

They are very inexpensive. I’ve picked some of these kits


up for less than 15 dollars.

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 2/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

They are surprisingly powerful and can carry a decent


Table of Contents
payload
They are pretty easy to assemble and modify
They are quite durable so your robot can survive a few 1 Introduction
crashes while you are in the testing phase.
2 Robot Car Chassis

A few of the robotics projects that I’ll be presenting to you here 3 Making Plans

at the Dronebot Workshop rely upon these types of robot car 4 Optical Interrupter Sensor
chassis so I thought it would be a good idea to show you how to
5 Add an LM393 Comparator
put one together. And I also want to use the opportunity to
show you how to use those two strange black “slotted disks” 6 Calculating Wheel Speed
that come with each of these kits. 6.1 Step 1 – Preparing the
Chassis
Those disks are actually “encoder wheels” and they can be used
with an opto interrupter-based sensor to measure speed and 6.2 Step 2 – Prepare the Motors
distance travelled. I haven’t seen many instructions for using
6.3 Step 3 – Mount the Motors
them with the robot car chassis so in this article (and the
on the Chassis
accompanying video) I’ll give you all the information you need to
6.3.1 Style 1 – Acrylic Mount
put them to work.
6.3.2 Style 2 – Aluminum
So let’s get rolling!
Mount

6.4 Step 4 – Install the Wheels,


Rotary Encoders and Sensors

6.5 Step 5 – Install the Castor

6.6 Step 6 – Install your


Components
6.6.1 Install the Battery
Holder

6.6.2 Mounting the Arduino

6.6.3 Mounting the L298N


Motor Driver

6.7 Step 7 – Wire it Up!


Robot Car Chassis 7 Interrupts
There are a few variations on the robot car chassis but for the 7.1 Types of Interrupts
most part they all consist of the following items: 7.2 Coding for Interrupts
The Chassis Base. This is an acrylic plastic base, precut 7.3 Better Interrupt Code
with several holes to mount components.
Motor Mounts. Some of the kits use acrylic for this as well, 8 Testing the Speed Sensors
others use aluminum brackets. Mounting the motors is
9 Robot Car Sketch

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 3/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

probably the thing that confuses most users so I’ll illustrate 9.1 Playing with the code
that in detail.
DC Motors. These 6-volt motor have a surprising amount 10 The End – Not Really!
of torque for their size.
Wheels. A car isn’t much use without wheels! These 10.0.1 Parts List
chassis kits use two plastic wheels, with tire treads that
work well on both smooth surfaces and carpet. 10.0.2 Resources
Rear Castor. As there are only two wheels a castor is
provided for balance and stability. The castor comes with
mounting hardware.
Encoder Wheels. These two plastic disks are meant to
mount to the motor shafts, on the opposite sides from the
wheels. They have a series of slots in them as they are
designed to be used with an optical source-sensor to
provide feedback on motor speed and wheel position. I’ll
show you how to use them today.
Battery Holder. These kits come with a holder for 4 AA
cells, to provide 6 volts for the motors. I often replace
these with a 5 cell holder as the L298N H-Bridge controller
that I usually use to drive the motors will drop 1.4 volts, so
I consider the 4 cell holder to be “spare parts”.
Mounting Hardware. All of the nuts, bolts and spacers you
will need to put the chassis together. You’ll need to supply
more hardware to mount your own components on the
base.
Misc Parts – It’s amazing what else some of these kits
come with. Common additions are power switches, wires
and pan-and-tilt mechanisms for mounting sensors or
cameras. One of my kits even supplied a small screwdriver.

Not bad for an under 15 dollar purchase!

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 4/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

Making Plans
Before starting any project it’s a good idea to plan out what
you’ll be doing. In the case of the robot car chassis this means
determining what components and sensors you’ll be using in
your design and where on the chassis you want to mount these
items.

A couple of considerations you might want to take into account


when planning;

What components will your design use?


What is the total weight of all of your components? Best to
keep this under a kilogram (2.2 pounds) as heavier loads
will cause performance issues and rapid battery drain.
Are there any sensors (i.e. ultrasonic, optical) that will need
to be mounted in specific positions? Are there already
mounting holes on the chassis to attach these, or do you
need to drill a few?
Remember, you can mount components under the chassis
as well as above it. For those under the chassis you need to
be sure they are securely mounted and have enough
clearance to not risk touching the ground.
Weight distribution is very important. Try to avoid placing
a lot of weight on one side of the car chassis. Ideally the
weight should be equally distributed. If you have
specifically heavy components it’s a good idea to keep
them need the center of the wheelbase.
Will you be using the speed encoders? If so remember that
you’ll need to mount the optical source-sensor in the slots
provided, so leave enough room for it.

Once you’ve determined how you’ll mount your components it’s


time to begin assembling the robot car chassis. But before we
do that let’s first take a look at the optical sensor that is used to
measure the wheel position and speed.

Optical Interrupter Sensor


Each of theses robot car chassis kits comes with a pair of
encoder wheels, which are small black plastic disks with a series
of slots cut through them. These are designed to mount onto
each motor shaft opposite the wheel, so they will spin at the
same speed as the wheel does.

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 5/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

What’s missing from the kit is the sensor itself (to be fair most
of these kits don’t contain any electronic components, just the
motors and hardware, so the omission is acceptable). The
chassis, however, is precut to accept an H206 slot-type opto
interrupter. This is the key to getting the speed sensor to work.

If you are not familiar with opto interrupters (also referred to as


an “opto isolator” or “optical source-sensor”) don’t worry, they
are actually pretty simple devices and they are used in a variety
of applications. In fact you’re probably using one right now
without even being aware of it, as a mouse with a scroll wheel
and most non-laser printers make use of them.

An opto interrupter consists of a source of light, usually an


infrared LED, and a phototransistor sensor. The light source is
mounted facing the sensor with a gap between them. In the
case of the H206 that gap is about 6 millimeters.

In operation the LED is illuminated and it shines onto the


phototransistor, which detects its light and allows current to
pass from the collector to emitter. Essentially a phototransistor
is like a regular bipolar transistor except instead of reacting to
current applied to the base it reacts to photons of light.

If a solid non-transparent object is placed in the slot between


the LED and phototransistor it will interrupt the light beam,
causing the phototransistor to stop passing current.

In our application the opto interrupter will be positioned with


the rotating encoder wheel in the gap between the LED and
transistor. As the wheel spins the slots in the wheel will allow
pulses of light to reach the phototransistor, causing it to switch
on and off in time with the wheel rotation.

Each pulse will represent a slot in the encoder wheel, so if your


encoder wheel has 20 equally-spaced slots (a pretty common
value) then each pulse indicates that the wheel has turned 18
degrees (360 degrees divided by 20).

Add an LM393 Comparator


You could just purchase a couple of H206 opto interrupters and
wire them up to your microcontroller, however you’d probably
wouldn’t be satisfied with the results. That’s because in the real
https://dronebotworkshop.com/robot-car-with-speed-sensors/ 6/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

world the output pulses directly from the phototransistor are


poorly formed and as a result your code would end up giving a
lot of errors.

What is needed is a way to clean up the output a bit and


generate some nice clean 5-volt pulses suitable for using with
an Arduino or other microcontroller. And the perfect
component to do that is a “comparator”.

A comparator is a device that has two inputs and one output.


One of the inputs is a “reference voltage” input, the other input
is where you would connect the output of the phototransistor.
The output of the comparator is digital, so it can go either high
(5-volts) or low (Ground).

The key is the reference voltage. If the input (in our case from
the phototransistor) is below the reference voltage then the
digital output of the comparator remains low. If the input equals
or exceeds the reference voltage then the output goes high.

To put it another way, a comparator is a good way to clean up a


“weak” or “dirty” digital signal, as well as a way of determining if
an input voltage has reached a preset threshold.

The LM393 is a dual comparator, meaning two independent


comparators in the same tiny package. It’s perfect for the job of
cleaning up the output from the opto interrupter.

Because the combination of the H206 and LM393 is so common


there are a number of small inexpensive sensor modules
constructed with these two components (plus a handful of
resistors and capacitors). These sensors are often called
“LM393 Speed Sensors” although the name is a bit of a
misnomer as the LM393 is just one of the components.
However as it’s a common name I’ve chosen to stick with it.

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 7/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

Calculating Wheel Speed


Calculating the speed that the wheel spins is pretty simple
really. If we use the example of an encoder wheel with 20 slots
then for every 20 pulses from the LM323 Speed Sensor our
motor has spun the wheel one revolution. Knowing that we can
count how many pulses we get in a second and use that to
determine the actual wheel speed.

Notice that I’m saying “wheel speed” and not “motor speed”,
they are different as the motor has internal gearing that slows it
down.

As a simple example if I measure exactly 20 pulses every second


then our wheel is spinning a one revolution per second.
Multiplying that by 60 gives us the speed in RPM, which in this
case is 60 RPM.

Once we determine the wheel speed we can use the wonderful


power of mathematics to calculate a couple of other useful
parameters:

We can get the actual speed (in Kilometers or Miles per


hour) of our robot car.
We can determine how far our robot has travelled.
We also know that the wheel is actually spinning, as
opposed to being stuck and not moving.

To get those first two parameters we need to know one other


thing – the circumference of the wheel itself. There are two
ways to get that value:

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 8/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

1. Measure it! Use a flexible measuring tape or a piece of


string wrapped around the outside of the wheel and see
how long it is.
2. Calculate it. Multiply the diameter of the wheel by pi
(3.14) to get the circumference.

You can probably see why this value is so important, the


circumference of the wheel is equal to the distance that the
wheel will travel in one rotation (assuming you have perfect
traction with the surface you’re travelling on).

I measured one of the wheels in the kit I’m working with and it
had a diameter of 66.1 mm. So using the formula I arrive at the
following:

66.1 x π = 207.6

So my wheel has a circumference of 207.6 mm. A tape measure


wrapped around the wheel confirms that this is the correct
value.

You could also do the math using imperial measurements if you


really insist, but the metric measurements are much easier to
work with.

Putting the Chassis Together

Let’s gather together all of our components and the robot car
chassis parts and start assembly. In addition to the robot car
chassis kit itself we will need the following items:

An Arduino Uno (or clone). This is the “brains” of our


simple robot. While other Arduino’s would also suffice I’d
really recommend you use an Uno so that the sketches I’m
presenting will work “as-is”. If you must use a different
Arduino you may need to adjust the code to reflect the
different pins used for the two interrupts.
An L298N Dual H-Bridge Motor driver. This is a common
way of driving DC motors with a microcontroller or
microcomputer. I’ve covered the L298N before on the site.
Optical speed sensors based upon the H206 opto
interrupter and LM393 dual comparator. There are
several suitable models, you’ll find these on eBay as well as
at your local electronics shop (if you have one).
Wires and Jumper Wires. You’ll need some 18 – 20 gauge
wire to hookup the motors (some kits provide these) as
well as some male to female jumper wires to hook the
Arduino to the sensors and motor driver.
Hardware. Nuts, bolts and spacers – the kits include all the
hardware for mounting the main parts but you’ll have to
https://dronebotworkshop.com/robot-car-with-speed-sensors/ 9/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

provide your own to hold down the Arduino and motor


driver board.
A Solderless Breadboard – This is optional but it’s an easy
way to wire up the 5-volt power distribution for your
robot. Also, I plan on keeping this robot to add extra items
to it so the breadboard will come in handy for future
expansion.

You’ll also need to do a small bit of soldering to attach the wires


to the motors. You might also use a bit of heat-shrink tubing to
insulate and strengthen the connections to the motor, but that’s
optional.

Step 1 – Preparing the Chassis


Our first step is to prepare the chassis for the components we’ll
be using. Lay out your components on the main chassis plate to
determine the best arrangement. Then see if you need any
additional mounting holes, if you do mark their positions on the
protective backing that is affixed to the chassis plate.

Now drill the holes (if required),

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 10/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

Once the plate has been prepared you can peel off the
protective backing from both sides of the chassis plate. You can
also remove the backing from the acrylic motor holders if your
chassis uses them.

Step 2 – Prepare the Motors


Most of these robot car chassis kits have motors with no leads
connected to them. Some of the chassis kits will provide wires
for this purpose,with others you’ll need to supply them yourself.
If you do supply the wire use 18 or 20 gauge if you can to
prevent any power loss. Ideally you’ll use different color wires
to distinguish between positive and negative.

The best way to attach the wires is to solder them. Mounting


the motors in a clamp or vise might make it easier to hold the
motor steady while you solder the wire to it. Be careful as the
motor terminal connections are a bit fragile.

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 11/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

Again you may want to put a bit of heat-shrink tubing over the
connections after they are soldered to make them more solid
and to insulate them, but this step is optional.

Step 3 – Mount the Motors on the


Chassis
There are two different styles of motor mounts used in these
inexpensive robot car chassis kits. I’ll describe both of them for
you.

Style 1 – Acrylic Mount


This style of mount uses a couple of “t-shaped” acrylic pieces,
along with a couple of corresponding slots on the chassis base.
To use them pass them through the slots and mount the motor
between them. Then pass some screws (supplied with your kit)
through the mounts and the motor to hold everything together.

On some kits you only get one acrylic piece for each motor, if
this is the case with your kit then it mounts on the inside of the
chassis. Again you secure it with a couple of screws.

Style 2 – Aluminum Mount


This type of mount makes use of a couple of aluminum blocks,
one per motor. You attach them to the motors first and then
fasten them to the chassis with a couple of small screws (the

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 12/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

aluminum blocks have a coupe of threaded holes for this


purpose).

You kit probably came with a minimal instruction sheet that


should illustrate the motor mount arrangement. Have a peek at
it if you aren’t sure how everything fits together.

Step 4 – Install the Wheels, Rotary


Encoders and Sensors
We aren’t going to get very far without wheels so now it’s time
to install them!

The wheels simply press-fit onto the motor shaft, note that both
the motor shaft and the mounting hole on the wheels are “D
shaped” so they need to be lined up correctly. Once you have
them lined up press the wheels on as far as they will go.

Next we install the opto interrupter-based sensors. There are a


couple of different styles of these but they all press-fit into the
square slots provided on the base. Although they fit pretty
snugly I usually drill a mounting hole in the proper position as
well so that I can secure them with either a screw and nut or
with a tie wrap (as I did in the demo robot in the video).

The rotary encoders also press-fit onto the motor shaft, on the
opposite side from the wheels. You will need to slide the disk
back and forth until you arrive at a position that lets it spin
without binding on the robot car chassis or on the opto
interrupters. I found them to be a bit loose so I secured mine in

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 13/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

place with a dab of hot glue after I established the correct


position. Epoxy would also work here as well.

So now we have wheels but our chassis won’t stand up correctly.


To fix that we’ll need to install the support castor.

Step 5 – Install the Castor


The final step in assembling the basic robot car chassis is to
instal the castor. This is mounted with four spacers, each spacer
is threaded to accommodate a screw at each end.

Now that the castor is installed the robot car chassis is


complete. However it really doesn’t do anything yet so you’ll
want to perform one more step and install your components
onto it.

Step 6 – Install your Components


Now that we have completed the robot car chassis itself it is
time to install our components.

Obviously this step will be different for everybody, as it really


depends upon what components you plan on using with your
robot. I’ll show you how I installed the Arduino, the motor
controller and the LM393-based optical speed sensor.

Install the Battery Holder


I’m going to actually use a 5-cell battery holder instead of the 4-
cell holder supplied with the kit, as I know I’ll be losing 1.4 volts
due to the voltage drop in my L298N H-Bridge driver. If you
choose to use the original battery holder it installs in an
identical fashion.

You’ll also need to decide how you plan to power the Arduino
Uno. The easiest (but not necessarily the best) way is to use a 9-
volt battery plugged into the Arduino’s 2.1 mm power jack. You
can purchase 9-volt power clips already soldered to 2.1 mm
plugs or you can just solder up one yourself.

If your kit came with a power switch (some do, some don’t) you
can use it to switch the Arduino power supply. The other
batteries are used to power the motors so when the motors
aren’t being driven they shouldn’t draw any current (assuming
you use the Arduino supply for the L298N boards logic power).
https://dronebotworkshop.com/robot-car-with-speed-sensors/ 14/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

Mounting the Arduino


The easiest way to install an Arduino Uno on the robot car
chassis is to use some plastic or metal spacers. These can be
purchased from eBay, Amazon or your local electronics store.

If you don’t have spacers you can use a couple of different


methods instead:

A screw and three nuts for each mounting hole. This is a


pretty good arrangement but it takes a lot of alignment
and tightening to get it right.
Slice an old disposable ballpoint pen up into small spacers.
You can mount these with a screw and a single nut.

The Arduino will also need some power. In this simple design I
will be using a 9-volt battery to power the Arduino but of course
there are better options available. Those 5-volt USB power
banks work well and have the added advantage of being
rechargeable.

Mounting the L298N Motor Driver


The motor driver mounts in the same way that the Arduino did,
using spacers. Make sure to align the board so that the wires
from the motors can reach the terminals on the driver board.

In my design I decided to place the motor driver boards under


the chassis, even with the heatsink on the driver there is still
adequate clearance. The chassis has a large hole in it that is
ideal for passing the wires used to connect the L298N motor
driver module to the Arduino.

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 15/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

Step 7 – Wire it Up!


Finally we can wire up the robot car. The following diagram
shows all of the electronic components used in the car and how
they are hooked together.

The design makes use of the Arduino Uno’s two “hardware


interrupt” pins. This is the key to getting everything to work so
let’s examine interrupts further.

Interrupts
Interrupts are a very important concept in coding for any
computer, be it a tiny Arduino or a full sized desktop computer
running Windows, Linux or OSX. You’ve probably used them a
few million times, perhaps without knowing it.

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 16/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

A good example of using interrupts is getting data from a


keyboard. One way of getting keyboard data would be to write a
program that constantly keeps checking the keyboard to see if a
key has been pressed. This will work, but not without
disadvantages:

Its wasteful. You spend a lot of time querying the keyboard


only to find out there is no data.
You can miss data. If a key is pressed while your program is
performing another function it may miss a keypress
altogether.

Interrupts provide a much better way of doing this.

Instead of querying (or “polling”) the keyboard constantly the


program for the most part ignores the keyboard. But when a key
is pressed on the keyboard it generates a “hardware interrupt”,
a digital signal that tells the computer “stop whatever you are
doing and take care of this”. The computer then runs some code
called an “interrupt handler”, which in the case of the keyboard
determines which key you actually pressed. It then takes
whatever action is required to handle that keypress.

This is much more efficient because the computer doesn’t waste


time checking the keyboard until it is notified that a key has
actually been pressed.

Types of Interrupts
You might notice that I used the term “hardware interrupt” in
the previous example, and if it leads you to suspect that there
may be other types of interrupts then you are correct.

First of all there are both Hardware and Software interrupts.


The 8-bit AVR processors that power the Arduino boards are
not capable of Software interrupts (unlike the processor in your
computer which is) so we’ll limit our discussion to Hardware
interrupts.

You can further divide hardware interrupts into two categories


– internal and external. Internal hardware interrupts are
generated by the internal timers in the AVR processors, you’ve
probably used them without realizing it as timing functions like
“delay()”, “millis()” and “micros()” make use of them. The “tone()”
function also uses internal interrupts, as does the Servo library.

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 17/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

External hardware interrupts, as the name would imply, are


generated externally from the processor. In an Arduino these
interrupts can be generated in two ways.

External Interrupts from a change in state of one of the


predefined external interrupt pins.
Pin-change interrupts from a change in state of any one of
a predefined group of pins.

The term “change of state” simply means when a digital signal


changes value from 0 to 1 or vice-versa.

In our design we will be using the first one, external interrupts


from a change in state of one of the predefined external
interrupt pins. Those “predefined external interrupt pins” differ
depending upon which Arduino board you are using, as follows:

Arduino Model Digital Interrupt Pins

Uno, Nano, Mini, other 328-based 2,3

Mega, Mega2560, MegaADK 2, 3, 18, 19, 20, 21

Micro, Leonardo, other 32u4- 0, 1, 2, 3, 7


based

Zero all digital pins, except


4

As our robot car chassis experiment will be using an Arduino


Uno we have two interrupt pins available to us, pins 2 and 3.

Internally the AVR processors that the Arduinos are based upon
use interrupt numbers starting at zero, as opposed to using the
pin numbers. Here is a chart that explains how the pin numbers
are related to some of the Arduino models:

Arduino INT INT INT INT INT INT


0 1 2 3 4 5

Uno 2 3
https://dronebotworkshop.com/robot-car-with-speed-sensors/ 18/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

Mega2560 2 3 21 20 19 18

Micro 3 2 0 1 7

Coding for Interrupts


The key concept for coding for interrupts is that every interrupt
needs an “Interrupt Service Routine” or “ISR”, a special function
that is run when an interrupt is detected. These are written like
regular Arduino functions with the following caveats:

An interrupt service routine (ISR) needs to run quickly, so


it usually has a minimal amount of code.
There are certain functions and commands that don’t run
well in an ISR, specifically functions that make use of the
Arduino’s internal timers. Examples are the tone and servo
functions.
Sometimes the variables used within an ISR are seen by
the Arduino IDE compiler as being unused. If this happens
the code won’t run, the solution is to declare them to be
“volatile”

In order to make the ISR run when an interrupt is detected it


needs to be attached to the specific hardware interrupt that it
services. You do this using the Arduino attachInterrupt()
command. This command has the following format;

attachInterrupt(interrupt_number, ISR, mode)

The “interrupt_number” is (what else?) the number of the


interrupt, on the Arduino Uno this would be either 0 or 1.

The “ISR” refers to the name of the Interrupt Service Routine


that we coded to handle this interrupt.

The “mode” defines when the interrupt is triggered, and it can


have the following values:

LOW – triggered when the interrupt whenever the pin is


low,
CHANGE – triggered when the interrupt whenever the pin
changes value
RISING – triggered when the pin goes from low to high,
FALLING – triggered when the pin goes from high to low.

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 19/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

So the following statement will trigger an ISR called “My_ISR”


when interrupt zero goes from low to high:

attachInterrupt(0, MY_ISR, RISING);

Better Interrupt Code


The previous example is perfectly valid but it requires us to
know which interrupt number our interrupt pin is attached to.
Since this varies depending upon which Arduino model we use it
isn’t necessarily transportable between Arduino types.

A better way to code this would be to also use the Arduino


“digitalPinToInterrupt()” function. This function translates the pin
number to the Interrupt number, taking into account which
Arduino model you are compiling code for.

The “digitalPinToInterrupt” function has one input parameter, the


pin number you’re using. It outputs the Interrupt number. Using
it our previous code statement would read as follows, assuming
that we are coding for an Arduino Uno.

attachInterrupt(digitalPinToInterrupt(2), MY_ISR, RISING);

As the Uno has Interrupt 0 attached to Pin 2 the


digitalPinToInterrupt function will return a zero in this case.

I’ve barely scratched the surface when it comes to interrupts,


it’s a very important code concept that you should try and learn
more about as it will open up a whole new world of applications
you can design. One excellent source of information regarding
interrupts is the forum maintained by Nick Gammon in
Australia. I highly suggest you check it out

Testing the Speed Sensors


Ok enough talk, it’s time for some code!

The following code will test the speed sensors and display the
motor rotation speed in RPM on the Arduino serial monitor.
You can watch the experiment in the YouTube video associated
with this article or give it a try yourself. In the video I just
hooked the motors directly to a 6-volt battery to spin them at
full speed and I attached the sensors as follows:

Sensor 1 output to pin 2 (Interrupt 0) of the Arduino Uno


Sensor 2 output to pin 3 (interrupt 1) of the Arduino Uno
https://dronebotworkshop.com/robot-car-with-speed-sensors/ 20/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

The two sensors are powered from the 5-volt output from the
Uno. If you use something other than an Uno you’ll need to
consult the Interrupt pinout chart to get the correct pins

1 /*
2 Optical Sensor Two Motor Demonstration
3 DualMotorSpeedDemo.ino
4 Demonstrates use of Hardware Interrupts
5 to measure speed from two motors
6
7 DroneBot Workshop 2017
8 http://dronebotworkshop.com
9 */
10
11 // Include the TimerOne Library from Paul Stoffregen
12 #include "TimerOne.h"
13
14 // Constants for Interrupt Pins
15 // Change values if not using Arduino Uno
16
17 const byte MOTOR1 = 2; // Motor 1 Interrupt Pin - INT 0
18 const byte MOTOR2 = 3; // Motor 2 Interrupt Pin - INT 1
19
20 // Integers for pulse counters
21 unsigned int counter1 = 0;
22 unsigned int counter2 = 0;
23
24 // Float for number of slots in encoder disk
25 float diskslots = 20; // Change to match value of encode
26
27 // Interrupt Service Routines
28
29 // Motor 1 pulse count ISR
30 void ISR_count1()
31 {
32 counter1++; // increment Motor 1 counter value
33 }
34
35 // Motor 2 pulse count ISR
36 void ISR_count2()
37 {
38 counter2++; // increment Motor 2 counter value
39 }
40
https://dronebotworkshop.com/robot-car-with-speed-sensors/ 21/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts
41 // TimerOne ISR
42 void ISR_timerone()
43 {
44 Timer1.detachInterrupt(); // Stop the timer
45 Serial.print("Motor Speed 1: ");
46 float rotation1 = (counter1 / diskslots) * 60.00; // ca
47 Serial.print(rotation1);
48 Serial.print(" RPM - ");
49 counter1 = 0; // reset counter to zero
50 Serial.print("Motor Speed 2: ");
51 float rotation2 = (counter2 / diskslots) * 60.00; // ca
52 Serial.print(rotation2);
53 Serial.println(" RPM");
54 counter2 = 0; // reset counter to zero
55 Timer1.attachInterrupt( ISR_timerone ); // Enable the t
56 }
57
58 void setup()
59 {
60 Serial.begin(9600);
61
62 Timer1.initialize(1000000); // set timer for 1sec
63 attachInterrupt(digitalPinToInterrupt (MOTOR1), ISR_coun
64 attachInterrupt(digitalPinToInterrupt (MOTOR2), ISR_coun
65 Timer1.attachInterrupt( ISR_timerone ); // Enable the ti
66 }
67
68 void loop()
69 {
70 // Nothing in the loop!
71 // You can place code here
72 }

After compiling the code and uploading it to the Arduino open


your serial monitor. If everything is working you’ll see the motor
speeds displayed in RPM.

One thing you will notice about this sketch is that the loop
doesn’t contain any code! This might seem surprising as a
standard Arduino Sketch has most of its code within the loop. I
did this intentionally to show you that the interrupts are not
dependent upon any code within the loop. If you wish try adding
some of your own code inside the loop and watch it run while
the speed sensors still display the motor speeds.

Robot Car Sketch


Finally we come to the sketch to run our robot car!

1 /*
2 Robot Car with Speed Sensor Demonstration
3 RobotCarSpeedSensorDemo.ino
4 Demonstrates use of Hardware Interrupts
5 to control motors on Robot Car
6
7 DroneBot Workshop 2017
8 http://dronebotworkshop.com
9 */
10
11 // Constants for Interrupt Pins
12 // Change values if not using Arduino Uno

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 22/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts
13
14 const byte MOTOR_A = 3; // Motor 2 Interrupt Pin - INT
15 const byte MOTOR_B = 2; // Motor 1 Interrupt Pin - INT 0
16
17 // Constant for steps in disk
18 const float stepcount = 20.00; // 20 Slots in disk, chan
19
20 // Constant for wheel diameter
21 const float wheeldiameter = 66.10; // Wheel diameter in m
22
23 // Integers for pulse counters
24 volatile int counter_A = 0;
25 volatile int counter_B = 0;
26
27
28 // Motor A
29
30 int enA = 10;
31 int in1 = 9;
32 int in2 = 8;
33
34 // Motor B
35
36 int enB = 5;
37 int in3 = 7;
38 int in4 = 6;
39
40 // Interrupt Service Routines
41
42 // Motor A pulse count ISR
43 void ISR_countA()
44 {
45 counter_A++; // increment Motor A counter value
46 }
47
48 // Motor B pulse count ISR
49 void ISR_countB()
50 {
51 counter_B++; // increment Motor B counter value
52 }
53
54 // Function to convert from centimeters to steps
55 int CMtoSteps(float cm) {
56
57 int result; // Final calculation result
58 float circumference = (wheeldiameter * 3.14) / 10; // C
59 float cm_step = circumference / stepcount; // CM per S
60
61 float f_result = cm / cm_step; // Calculate result as
62 result = (int) f_result; // Convert to an integer (note
63
64 return result; // End and return result
65
66 }
67
68 // Function to Move Forward
69 void MoveForward(int steps, int mspeed)
70 {
71 counter_A = 0; // reset counter A to zero
72 counter_B = 0; // reset counter B to zero
73
74 // Set Motor A forward
75 digitalWrite(in1, HIGH);
76 digitalWrite(in2, LOW);
77
78 // Set Motor B forward
79 digitalWrite(in3, HIGH);
80 digitalWrite(in4, LOW);
81

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 23/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts
82 // Go forward until step value is reached
83 while (steps > counter_A && steps > counter_B) {
84
85 if (steps > counter_A) {
86 analogWrite(enA, mspeed);
87 } else {
88 analogWrite(enA, 0);
89 }
90 if (steps > counter_B) {
91 analogWrite(enB, mspeed);
92 } else {
93 analogWrite(enB, 0);
94 }
95 }
96
97 // Stop when done
98 analogWrite(enA, 0);
99 analogWrite(enB, 0);
100 counter_A = 0; // reset counter A to zero
101 counter_B = 0; // reset counter B to zero
102
103 }
104
105 // Function to Move in Reverse
106 void MoveReverse(int steps, int mspeed)
107 {
108 counter_A = 0; // reset counter A to zero
109 counter_B = 0; // reset counter B to zero
110
111 // Set Motor A reverse
112 digitalWrite(in1, LOW);
113 digitalWrite(in2, HIGH);
114
115 // Set Motor B reverse
116 digitalWrite(in3, LOW);
117 digitalWrite(in4, HIGH);
118
119 // Go in reverse until step value is reached
120 while (steps > counter_A && steps > counter_B) {
121
122 if (steps > counter_A) {
123 analogWrite(enA, mspeed);
124 } else {
125 analogWrite(enA, 0);
126 }
127 if (steps > counter_B) {
128 analogWrite(enB, mspeed);
129 } else {
130 analogWrite(enB, 0);
131 }
132 }
133
134 // Stop when done
135 analogWrite(enA, 0);
136 analogWrite(enB, 0);
137 counter_A = 0; // reset counter A to zero
138 counter_B = 0; // reset counter B to zero
139
140 }
141
142 // Function to Spin Right
143 void SpinRight(int steps, int mspeed)
144 {
145 counter_A = 0; // reset counter A to zero
146 counter_B = 0; // reset counter B to zero
147
148 // Set Motor A reverse
149 digitalWrite(in1, LOW);
150 digitalWrite(in2, HIGH);

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 24/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts
151
152 // Set Motor B forward
153 digitalWrite(in3, HIGH);
154 digitalWrite(in4, LOW);
155
156 // Go until step value is reached
157 while (steps > counter_A && steps > counter_B) {
158
159 if (steps > counter_A) {
160 analogWrite(enA, mspeed);
161 } else {
162 analogWrite(enA, 0);
163 }
164 if (steps > counter_B) {
165 analogWrite(enB, mspeed);
166 } else {
167 analogWrite(enB, 0);
168 }
169 }
170
171 // Stop when done
172 analogWrite(enA, 0);
173 analogWrite(enB, 0);
174 counter_A = 0; // reset counter A to zero
175 counter_B = 0; // reset counter B to zero
176
177 }
178
179 // Function to Spin Left
180 void SpinLeft(int steps, int mspeed)
181 {
182 counter_A = 0; // reset counter A to zero
183 counter_B = 0; // reset counter B to zero
184
185 // Set Motor A forward
186 digitalWrite(in1, HIGH);
187 digitalWrite(in2, LOW);
188
189 // Set Motor B reverse
190 digitalWrite(in3, LOW);
191 digitalWrite(in4, HIGH);
192
193 // Go until step value is reached
194 while (steps > counter_A && steps > counter_B) {
195
196 if (steps > counter_A) {
197 analogWrite(enA, mspeed);
198 } else {
199 analogWrite(enA, 0);
200 }
201 if (steps > counter_B) {
202 analogWrite(enB, mspeed);
203 } else {
204 analogWrite(enB, 0);
205 }
206 }
207
208 // Stop when done
209 analogWrite(enA, 0);
210 analogWrite(enB, 0);
211 counter_A = 0; // reset counter A to zero
212 counter_B = 0; // reset counter B to zero
213
214 }
215
216 void setup()
217 {
218 // Attach the Interrupts to their ISR's
219 attachInterrupt(digitalPinToInterrupt (MOTOR_A), ISR_co

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 25/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts
220 attachInterrupt(digitalPinToInterrupt (MOTOR_B), ISR_co
221
222 // Test Motor Movement - Experiment with your own sequ
223
224 MoveForward(CMtoSteps(50), 255); // Forward half a met
225 delay(1000); // Wait one second
226 MoveReverse(10, 255); // Reverse 10 steps at 255 speed
227 delay(1000); // Wait one second
228 MoveForward(10, 150); // Forward 10 steps at 150 speed
229 delay(1000); // Wait one second
230 MoveReverse(CMtoSteps(25.4), 200); // Reverse 25.4 cm
231 delay(1000); // Wait one second
232 SpinRight(20, 255); // Spin right 20 steps at 255 spee
233 delay(1000); // Wait one second
234 SpinLeft(60, 175); // Spin left 60 steps at 175 speed
235 delay(1000); // Wait one second
236 MoveForward(1, 255); // Forward 1 step at 255 speed
237
238
239 }
240
241
242 void loop()
243 {
244 // Put whatever you want here!
245
246
247 }

This sketch demonstrates how to run the car and take the input
from the two speed sensors to control how far it moves, we can
also control the car’s direction. Note the use of the “volatile”
integers in the pulse counters, this makes sure that the Arduino
IDE doesn’t “throw away” these variables when it compiles the
code.

The code has four functions to control the car:

MoveForward – this moves the car forward


MoveReverse – this moves the car in reverse
SpinRight – this spins the car right
SpinLeft – this spins the car left

Each of the functions has the same input parameters:

Steps – the number of steps in the encoder disk to move


the wheel
MSpeed – the speed of the motors, from 0 to 255

The care movement is controlled within the “while” loop inside


each of the functions (aside from the motor direction
commands all four functions are identical). You’ll note that the
movement of each wheel is calculated independently. This is
done to compensate for speed differences between the motors.

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 26/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

There is also a function called “CMtoSteps” that can be used to


convert centimeters to the number of steps required to move
that distance. This will simplify moving the car to a specific
location.

Playing with the code


The code at the end of the setup routine is just a series of
statements to move the car in various directions, using the
previously described functions. You can experiment by adding
more functions or by changing the parameters (steps, speed)
existing code.

You’ll note that once again I didn’t put anything in the loop, all of
the code runs in the setup routine and as such it will run once
and finish. You can repeat it by pressing the reset button on the
Arduino Uno. If you would prefer that the code runs forever
then put the car movement statements into the loop instead.

The code is basically for demonstration purpose and you can


build upon it to improve it and to add more functions, such as
functions to steer the car at a specific angle. Let me know in the
comments if you come up with anything interesting!

The End – Not Really!


This brings us to the end of the article but it’s really just the
beginning . I’m planning to add more features to our little Robot
Car so keep tuned to the site to see the next evolution of the
design. A few obvious ones are collision avoidance, line
following and remote control.

If there are features you would like to see please let me know in
the comments and I’ll see what I can do to include them.

Until we meet next time happy motoring!

Parts List
Here are some components that you might need to complete the
experiments in this article. Please note that some of these links
may be affiliate links, and the DroneBot Workshop may receive a
commission on your purchases. This does not increase the cost to
you and is a method of supporting this ad-free website.
https://dronebotworkshop.com/robot-car-with-speed-sensors/ 27/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

COMING SOON!

Resources
Robot Car Sketches All of the sketches used in this article.

Nick Gammons Forum for Arduino Interrupts The best site


on the web for Arduino Interrupt Information.

PDF Version – A PDF version of this article, great for printing


and using on your workbench.

Summary

Article Name Build a Robot Car with Speed Sensors

Description Put together a simple 2-wheel robot car


chassis and learn to use Arduino interrupts
to measure wheel rotation and speed.

Author DroneBot Workshop

Publisher Name DroneBot Workshop

Publisher Logo

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 28/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

Tagged on: Arduino Project Arduino Tutorial Robot


Project

DroneBot Workshop December 8, 2017


Arduino, Project, Robots 53 Comments

← Using the HC-SR04 Ultrasonic Distance Sensor with


Arduino

Hacking the Google AIY Voice Kit – Part 1 – Putting it


Together →

Join the discussion

{} [+]

53 COMMENTS   Oldest 

rod  5 years ago

Thank you,
I found very useful the way you get the rpm speed of
every motor using the opto interrupters. I didn’t
know about those little sensors.
I have a question regarding the sketches, though:
wouldn’t you need to define the pin mode
(output/input) for every arduino pin in the setup() ?
Reply

Peter Obermeier  5 years ago

Thank you very much for this workshop. I am retired


now and 3 weeks ago I started to build cars with
Lego and control it via IR Remote and Bluetooth LE

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 29/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

(nRF8001). Have written an iOS App to send


commands to Arduino and Arduino is controlling
the motor and steering servo and receiving sensor
data. But there is still a lot to do. You have given me
two new ideas. 1) to use Interrupts and 2) to use
Speed encoders to measure distance. THANK YOU!
Have ordered the parts and will modify my cars.
Please continue with this workshop because… Read
more »
Reply

Steve Prairie  5 years ago

How can I find code to run the: UCTRONICS WIFI


Smart Robot Car Kit for Arduino with Real Time
Video Camera, Ultrasonic Sensor, Line Tracking,
WIFI Module Remote Controlled by Android App.

Any help is appreciated


Reply

Richard  5 years ago

This is a great article! I now understand what parts I


need to buy and how to use them. Most kits seem to
have the encoder wheels, so I was always confused
that none of the project instructions seemed to use
them! One point of clarification…. The forward and
reverse functions appear to keep both motors
spinning at the same value and then stop them
independently when they have each rotated the
same amount. Assuming one will probably go
slightly faster than the other when set to the same
analogue value, doesn’t that mean that the vehicle
will move in… Read more »
Reply

Andreas Zervas  5 years ago

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 30/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

hi and thank you for this work, it’s a great start for
me.

Some improvements I did in the move routines


code:
1. if you use “II” instead “&&” in “while”, then the
movement finish when both motors end at the same
number of steps.
2. instead of “steps > counter_A” I use “counter_B >=
counter_A” and instead of “steps > counter_B” i use
“counter_A >= counter_B”.. in this case each motor
waits the other to complete same number of steps,
so the movement is straight.
Reply

Shalin Shukla  4 years ago

Thank you,
For your great effort
Reply

Author

DroneBot Workshop  4 years ago

 Reply to Shalin Shukla

You are most welcome!


Reply

Jodi Zainil  4 years ago

Thank You very much. I think its a great free


workshop. Its very very useful for me.
Reply

Eb Swarbrick  4 years ago

Hi, I’ve built up the car using the optical encoders


and it does move but not as expected. The distances
covered are about 1/4 of that as instructed. I’ve
modified the instructions and all movements show

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 31/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

the same problem. The A_counter numbers show


that 1 rev of the wheel produces around 84
interrupts (+ or – 5) and I’ve put a ‘scope on the
encoder outputs and it shows 20 square-wave
cycles per rev. This is for both wheel encoders so it’s
not a matter of a faulty encoder. So where are the
extra interrupts generated? If the wheel
stops… Read more »
Reply

Gennaro  4 years ago

 Reply to Eb Swarbrick

I join
Reply

Rakesh  4 years ago

i would like to show rpm in lcd i2c lcd screen. how


can i do that.
thanks in advance
Reply

Gennaro  4 years ago

I tried many times Robot Car Sketch but


unfortunately it does not work.

the sequence is executed


but the distances are wrong, moreover the two
motors do not rotate at the same speed

does anyone know what the mistake is?


Reply

Carlos  4 years ago

 Reply to Gennaro

Try to use other LM323 Speed Sensor. I


was facing the same problem and after
changing the speed sensor by others it
worked. Also i used the algorithm

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 32/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

suggested by “Andreas Zervas”.


The first sensor that I used was this one:
https://www.dx.com/p/smart-car-speed-
detecting-sensor-module-for-arduino-
2017463#.XF2cCVz7Rph

The second (the one that worked) was


this one:
https://www.dx.com/p/lm393-
comparator-speed-sensor-module-for-
arduino-2017770#.XF2colz7Rph
Reply

Halil  4 years ago

 Reply to Carlos

I did same thing with Andreas Zervas,


but my robot still does not go straight.
Can you help me please?
Reply

Prompt  3 years ago

 Reply to Carlos

Hello Carlos
I use the same sensor as yours. I have
faced the problem that motors just
runs without stopping. I would like
you to tell me which pins from the
sensor you connected to the board,
D0 or A0? Thanks.
Reply

Nabila  1 month ago

 Reply to Carlos

Carlos. Thanks for sharing the


resources, I checked the links to the
2nd one that worked for you, but the
page is not available anymore. would u
plz be kind enough to post a separate
new link or photo of that specific
https://dronebotworkshop.com/robot-car-with-speed-sensors/ 33/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

sensor?
It would be a great help to me!
Reply

Babar MAjeed  4 years ago

Thanks sir your useful information am doing


everything is all right but my one motor did
everything related to your code but other motor
didn’t run please guide me when ill plug out ENA A
pin from L298n then ENB motor run please guide
me
Reply

gora  4 years ago

how do you make the reading faster? i tried to


change the timer but the result is diferent
Reply

Tony  4 years ago

Hello, GREAT article ! I have implemented part of


yourn code to measure the RPM of two wheels.
However when I use this code in my partivular
sketch, which includes the use of a servo for looking
left and right to mesasure distance (using Ultrasonic
module) the servo doesn’t work anymore? So there
seems to be some influence by the nterrupts. Any
idea where I should start looking ? Thanks and kind
regards. Tony
Reply

Andy W  4 years ago

Great video and tutorial, got me going on the 2WD


robot car, thank you! Mine was driving in quite a
curve and troubleshooting has shown me that
Arduino Uno pin 5 generates PWM at a higher

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 34/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

frequency and higher duty cycle than pin 10 so pin 5


is not a great choice to pair with pin 10 for the enA
and enB lines that should be matched for equal
motor speeds, see “Notes and Warnings” here:
https://www.arduino.cc/reference/en/language/fun
ctions/analog-io/analogwrite/ On my 2WD robot
I’ve moved enB from pin 5 to pin 11, updated that in
the code and now my robot is… Read more »
Reply

Markus  4 years ago

Hey, I’m currently also working on this “robot”-


chassis and I have a comment on the mounting
positions of your components. You talked about
balancing weight all over your robot to avoid tilting.
But IMHO, placing the batteries on top of the caster
is not an ideal decision for two reasons: 1.) The
balance is not very stable. If the caster wheel points
to the left or right, the car will fall to the other
direction very easily. 2.) The caster wheel should not
take the majority of the weight. As this is not really
important as long as you’re driving… Read more »
Reply

nektarios  3 years ago

great job…i have a question about the calculation of


rpm…the fact that the slots are 20 that cause 21
pulses per cycle …am i correct?
Reply

Prompt  3 years ago

Hello
I did everything according to the tutorial. When I
upload the sketch RobotCarSpeedSensorDemo.ino I
get the following result. Motors run only Forward or
Reverse or Left or Right. Motors do not react to any

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 35/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

other data like steps and speed.


When I run the other sketch DualMotorSpeedDemo
I get the following result. Motors do not run.
I would appreciate any help.
Thanks a lot.

I have checked the components with different


sketched. LM393 works perfect. All components
function properly.
Reply

Iresh  3 years ago

Thank you!
Any modification, if motors speed is different.
Reply

gerson  3 years ago

The RobotCarSpeedSensorDemo.ino sketch does


nothing right ?! Because there is no motor drive,
correct?
Reply

PJO  3 years ago

I recently learned about the DronebotWorkshop.


This lesson is quite old. How do I information about
it? For example, where do I purchase a wheel disc?
Reply

Ian Steward  3 years ago

How do I purchase the kit for this project


Reply

egers  3 years ago

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 36/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

Hi Bill, Thank you for all the effort you put into The
Workshop. Can you do an article on ultrosonic with
the car?
Thank you.
Reply

peter  3 years ago

why cant the sketch upload on my arduino


Reply

UsmanRiaz  2 years ago

Very Good Sir I Like U


Reply

JT High  2 years ago

Amazing detail to this video and the notes/code


here. Thank you for your extensive efforts to
educate and entertain your audience. Do you host
the code out on GitHub also (or other public repos)?
Reply

James Craig  2 years ago

You said not to take the completed robot apart


because there were to be follows. Did you ever
provide them.
Reply

yinka Abiodun  2 years ago

Great work, I am working on similar project. how so


I determine the position of the robot ?
Reply

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 37/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

Robert  2 years ago

Control 2 Motor HM-GM37 with disk encoder HALL.


The motor have 6 wire, the Power +, -, the Encoder
Phase A, B,
and the encoder power +,-.
I try to find a layout or example in many site and
project how
to connect, and synchronize both Motor.
I thing your site is the one, talk a lot about motor,
but not
talking about Hall encoder dual phase, basic signal
11PPR.

I have no idea about 11PPR, Thanks a lot if you find


something to help me.
Reply

Nandhu  2 years ago

Code to move robot for 100 centimeters forward


exactly?
Reply

Daniel Thackeray  2 years ago

This code ignores one of the most important points


about using interrupts: that if you reference a
variable that can be altered by an interrupt always
create a local copy, and use that local copy. In this
case the movement functions use counter_A and
counter_B in several different comparison
statements. These variables can be altered by the
interrupt service routine, therefore they could be
different between one usage and the next, even if
the statements are only a few lines apart. This sort
of error is very difficult to debug, because it can be
intermittent. That’s why the rule is:… Read more »
Reply

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 38/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

Jan Atle Ramsli  2 years ago

First of all, than you for being the best, most


instructive and comprehensive robot dude out
there. One of the problems I have with these two-
wheeled rovers, is that they categorically refuse to
drive straight forward. Every motor has its own idea
about how fast it should go when given a PWM
input. So, I set one interrupt vector to each wheel,
counting how much the left/wight wheels had spun.
IF the left wheel counter > right wheel counter
THEN increase right wheel speed END IF 123 IF the
left wheel counter > right wheel counterTHEN
increase right wheel speedEND… Read more »

 Last edited 2 years ago by Jan Atle Ramsli

Reply

Randy Reed  2 years ago

Thank you. I have an electronics background and


have been looking for more info on Arduino, esp32,
etc.
The way you organise and present your info is
excellent. I especially like the way your website goes
through the videos and code. I’ve always been a
little intimidated by interrupts, but you’ve made
them very easy to understand.
Please don’t stop. You have a lot of material and I’ve
gone through several. I’m looking forward to seeing
more.
Reply

Spackstor  2 years ago

 Reply to Randy Reed

While I have worked with encoders, I am a


hack. Nice to see how someone with a
knack does it. Thank you for this.

Silly aside, 25.4 cm is 10 inches.

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 39/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

Reply

Fred  2 years ago

Not looking for a reply, just saying you do a good job


and keep it up
Reply

Denis Bujoreanu  2 years ago

Hi there, I found this article quite useful, I applied its


approach on a kit robot I put together using the
same parts and for the code part I think that in
order to ensure both wheels turn with almost the
same number of ‘steps’ so that the robot moves in a
straight line you should change the while loop so
that it stops when BOTH counters have reached the
number of required steps. The change I propose is
from: while (steps > counter_A && steps >
counter_B) 1 while (steps > counter_A && steps >
counter_B) to this: while… Read more »
Reply

Chuck Knox  2 years ago

I use your site and your youtube tutorials as an


authoritative knowledge base. If I need to know
about something related to Arduino, this is go to
place. Thank you!
Reply

Talha  2 years ago

Hello I want to know how can we stop the timer in


case of esp32?
Reply

Tom Duong  2 years ago

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 40/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

I like your instruction, very clear and easy to follow.


I built a robot car using esp-12, L293D , servo motor
and ultrasonic distance sensor.
I want the robot to measure the distance while it
moves close to the wall. If the distance <10cm then
the car stop, the servo motor then turn the distance
sensor to find the longest distance, turn the car
toward that way and then keep moving on. If you
already have a project similar to this, please kindly
share. Thanks
Reply

ahdamad  1 year ago

Can I use the same code without timerone library


Reply

niboon  1 year ago

float circumference = (wheeldiameter * 3.14) / 10; //


Calculate wheel circumference in cm

Why u / 10 ?? When wheel is mm . I think * 10 right ?


Reply

Arda Uyar  1 year ago

I can’t thank enough for this incredible workshop.


Despite having zero knowledge, i feel like i can now
control robots! haha… ok jokes aside, i would really
like to know how we would run this car for seconds.
I also looked up for other workshops but so for no
success… These codes make the car move for
desired cm’s or steps, what if it moved for a period
of time? I would be so glad if you could add some
codes for this. Thank you so much!
Reply

jason  1 year ago

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 41/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

I wish I can do a job like that.Thanks


Reply

kamal  10 months ago

1 thank you for this wonderful tutorial

Reply

theodor  9 months ago

The code was not working on my project until i


added this two lines as the arduino documentation
says . I posted it in case someone has the same
problem .

Timer1.initialize(1000000); // set timer for 1sec


pinMode(MOTOR1, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt (MOTOR1),
ISR_count1, RISING); // Increase counter 1 when
speed sensor pin goes High
pinMode(MOTOR2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt (MOTOR2),
ISR_count2, RISING); // Increase counter 2 when
speed sensor pin goes High
Timer1.attachInterrupt( ISR_timerone ); // Enable
the timer
Reply

ssadik  9 months ago

i am from morroco and a love robots electronic stufs


;
thak you for all your efort and god job . precer
Reply

Sid  8 months ago

Thanks for all your great work, it has been very


helpfull, Ive go the ESP32 working with the

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 42/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

TB6612fng for a robot. I’m almost 70 so it does take


A WHILE.
Reply

Muhammad Hamza  7 months ago

Can you please upload a video on how to make a


robot that simultaneously avoids obstacles and
moves to a specific distance in a specified direction?
Reply

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 43/44
07/05/2023, 15:35 Build a Robot Car with Speed Sensors using Arduino Interrupts

Categories What's New? What’s Popular

6-Wheel Rover Project Welcome to the Workshop!

Arduino Arduino GIGA R1 WiFi Sound with ESP32 - I2S Protocol

Build a REAL Robot Use a PS3 Controller with an ESP32 Getting started with ESP32

Electronics Using ChatGPT to Write Code for Getting started with the ESP32-CAM
Arduino and ESP32
ESP32 Controlling DC Motors with the
Using the ESP32 DAC (and making a
L298N Dual H-Bridge and an Arduino
Featured Fruity Instrument)
Using Servo Motors with ESP32
General Mecanum Wheel Robot Car with
ESP-NOW Remote Control ESP32 WiFiManager - Easy WiFi
IoT
Provisioning
Driving DC Motors with
Project
Microcontrollers Stepper Motors with Arduino -
QuadCopter Getting Started with Stepper Motors
Measure Air Quality with
Raspberry Pi Microcontrollers ESP NOW - Peer to Peer ESP32
Communications
Raspberry Pi 10 Projects Pico W with the Arduino IDE
Measure DC Voltage and Current
Robots Building a Dual-Boot Workstation
with an Arduino
Software Designing and Building Linear DC
Power Supplies
Tutorial
Using GC9A01 Round LCD Modules
Vendors
Sound with ESP32 – I2S Protocol

Using Arduino Interrupts – © 2023 DroneBot Workshop


Hardware, Pin Change and Timer

Using the ESP32-CAM MicroSD Card

LED Displays with Arduino

Home Arduino Raspberry Pi Drones Robots Electronics IoT Reviews Tutorials About Us Contact Us
Privacy and Cookies

https://dronebotworkshop.com/robot-car-with-speed-sensors/ 44/44

You might also like