# Finding speed using PWM

Hi everyone! I am currently doing my final year project. I would like to find the speed of the robot. I am using PWM to control the speed of the robot now. Is it possible to get the speed of the robot in m/s using PWM? Thank You! The codes are as follows:

``````#include <SoftPWM.h>

// motor 1
int in2 = 2;
int in3 = 3;
// motor 2
int in4 = 4;
int in5 = 5;
// motor 3
int in9 = 9;
int in10 = 10;
// motor 4
int in11 = 11;
int in12 = 12;

void setup() {
// arduino default serial
Serial.begin(9600);

// enable softPWM
SoftPWMBegin();
SoftPWMSet(2,0);
SoftPWMSet(3,0);
SoftPWMSet(4,0);
SoftPWMSet(5,0);
SoftPWMSet(9,0);
SoftPWMSet(10,0);
SoftPWMSet(11,0);
SoftPWMSet(12,0);
}

void loop() {
forward(100);
}

void forward(int speed){
SoftPWMSetPercent(in2, speed);
SoftPWMSetPercent(in4, speed);
SoftPWMSetPercent(in9, speed);
SoftPWMSetPercent(in11, speed);
delay(1000);
}
``````

The relationship between PWM duty cycle and speed is project dependent: do some measurements of the speeds you get for different duty cycles and you have a rough idea on the relation between the two for your project under test conditions. Different temperature, a slope, or whatever will affect speed even when the duty cycle remains the same.

The proper solution: a speedometer of sorts.

This robot appears to be a car with four wheels driven by one motor each is that correct?
How do you steer?

As @wvmarle says the PWM is not going to tell you the actual speed (even in a straight line).
You could fit encoders to tell you how fast the wheels are turning but that would depend on the wheels not slipping.
If you used an accelerometer you might be able to calculate your velocity.

A lot probably depends on how accurate you need to know your speed and direction, maybe a little wheel slip does not matter.

Thanks for all the replies. It really helps me a lot with the understanding of how PWM works and how I can find the speed of the robot.

ngjy:
Hi everyone! I am currently doing my final year project. I would like to find the speed of the robot. I am using PWM to control the speed of the robot now. Is it possible to get the speed of the robot in m/s using PWM?

Controlling the velocity of a robot moving in a particular direction requires a control method. In many cases, some motor speed measurements need to be done in order to determine or pre-determine motor speed or wheel speed, from which you can then estimate robot velocity. The measurements can involve rotary encoders. If you pre-measure robot speed versus PWM level under particular ideal conditions, then the robot might move (in a straight line) at more or less the speed that corresponds to a particular PWM level. Here, you depend fully on assuming that the pre-measured speed is what the robot will achieve for a particular PWM level (once everything has stabilised). This is for the case where no speed measurements are actually done during normal robot operation.

Otherwise, to be more certain about robot speed, you will learn that speed measurements of one kind, or even various kinds need to be made while the robot is operating. The speed measurements can be processed in order to apply particular levels of PWM at particular times in order to achieve and/or maintain a certain speed.

If your robot is able to use some kinds of position sensing system, like GPS or some indoor position measurement system, then these are other ways for getting speed information in order to make the robot travel at a certain speed.