Pulse Count To Speed Control

Hi All,

I am currently working on a project But really struggling.
The project is to control the speed of a motor based on the RPM/pulses of a wheel. I have two separate proxy sensors - one for the wheel and one for the motor.

I have a 17 tooth gear connected to a wheel and i have a proxy sensor counting each time a tooth of the gear goes past. I have the same setup for the motor. I need the motor to run at half the speed that the wheel is spinning Using some kind of (Probably PID Loop).

I will put my current code bellow but right now it isnt a very gradual speed increase and decrease and it is also like a second delayed.

Thanks in Advance :slight_smile:

#include <arduino-timer.h>
auto timer = timer_create_default();

bool docounting1 = true;
int pulses1 = 0;
int pwmvalue1;
bool docounting2 = true;
int pulses2 = 0;
int pwmvalue2;
int val1;
int val2;
int motorPin = 10;

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT);
  pinMode(4, INPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  timer.every(500, countPulses);
}

void loop() {
  timer.tick();
  digitalWrite(6, HIGH);
  digitalWrite(7, LOW);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  
  val1 = digitalRead(2);
  val2 = digitalRead(4);
  
  if (val1 == LOW && docounting1 == true) {
    docounting1 = false;
    pulses1 = pulses1 + 1;
  } else if (val1 == HIGH) {
    docounting1 = true;
  }
  
  if (val2 == LOW && docounting2 == true) {
    docounting2 = false;
    pulses2 = pulses2 + 1;
  } else if (val2 == HIGH) {
    docounting2 = true;
  }

  analogWrite(motorPin, pwmvalue1);
}

void countPulses() {
  Serial.print("Pulses in half a second (Pulse 1): ");
  Serial.println(pulses1);
  pwmvalue1 = map(pulses1, 0, 10, 0, 255);
  if (pulses1 >= 10) {
    pwmvalue1 = 255;
  }
  
  Serial.print("Pulses in half a second (Pulse 2): ");
  Serial.println(pulses2);
  pwmvalue2 = map(pulses2, 0, 10, 0, 255);
  if (pulses2 >= 10) {
    pwmvalue2 = 255;
  }
  
  pulses1 = 0;
  pulses2 = 0;
}

like i said, the code kind of works but it is pretty Janky.
all help is greatly appreciated!!
Thanks

Your speed control is based on the pulse rate, so step 1 is to be sure that you are measuring the pulse rate accurately.
Write a simple version of this code that only reads the sensor and prints out the pulse rate once every 5 seconds. Get that working first so you can be sure it's dependable.

The next step would be to verify that you can control the motor smoothly, so slowly ramp it up from stopped to full speed and back down. If that works, then it's time to combine the pulse measurement and the motor control using feedback. Tuning it to work smoothly is where the real fun begins!

Thanks For the advice!
i have got the pulses working and converting into rpm but i dont think the way i have it set up in that code with the way the timer works. i feel as though a PID loop would be the best bet, so if you know how to implement that or even know a better way please feel free to let me know

Thanks

What are the rotation speeds you are working with (RPM)?

What is the number of pulses per second you are looking at?

The Wheel pulses can go from anywhere from 0-about 20 per half a second and same for the motor

If you feel confident with what you have now, then take a look at the PID libraries available for arduino. I've used the one by Brett Beauregard a few times now.

Yeah thanks, ill have a look.
do you have any example code on how i could do this with the setup i have?
is using the timer to count the pulses every 500ms a bad setup?
I also dont have very much experience with PID loops so any help would be very helpful
Thanks

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.