Controlling a dc motor via pid and rpm sensor.

zhomeslice:
In the folder where your sketches are stored in a sub folder called libraries create a new folder called PID_v2 and copy the PID_v2.h ad .cpp into that folder
Restart the arduino IDE
Open the .ino and Save it wherever you would like and it should now compile.

Arduino: 1.6.12 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\Naeem\Documents\Arduino\libraries\PID_v2\PID_v2.cpp:80:69: error: expected primary-expression before ')' token

Using library PID_v2 in folder: C:\Users\Naeem\Documents\Arduino\libraries\PID_v2 (legacy)
exit status 1
Error compiling for board Arduino/Genuino Uno.

it showing this error

naeemawan36:
Arduino: 1.6.12 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\Naeem\Documents\Arduino\libraries\PID_v2\PID_v2.cpp:80:69: error: expected primary-expression before ')' token
...
it showing this error

Fixed :slight_smile: just updated it from using constrain() which caused other issues and I didn't test it Here you go

PID_v2.cpp (9.88 KB)

zhomeslice:
Fixed :slight_smile: just updated it from using constrain() which caused other issues and I didn't test it Here you go

thanks that work
i m working on 31khz frequency
would you suggest can i used this
TCCR1B = TCCR1B & B11111000 | B00000001;
for pin 9 which give directly 31khz and second if you can guide i read that "direct mode" in pid work when Input is less than Setpoint and "reverse mode" work vice versa which mean i have to used 2 PID but in the code you upload didn't have "reverse mode"

naeemawan36:
thanks that work
i m working on 31khz frequency
would you suggest can i used this
TCCR1B = TCCR1B & B11111000 | B00000001;
for pin 9 which give directly 31khz and second if you can guide i read that "direct mode" in pid work when Input is less than Setpoint and "reverse mode" work vice versa which mean i have to used 2 PID but in the code you upload didn't have "reverse mode"

change this line
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, 0.04, 0.1, 0.0, DIRECT);
to

PID myPID(&Input, &Output, &Setpoint, 0.04, 0.1, 0.0, REVERSE);

or
myPID.SetControllerDirection(REVERSE); // at any time to reverse the PID
Should reverse the direction of the code
Pin 9 should work just as good fine
change
#define PWMpin 3
to:
#define PWMpin 9

now I'm not familiar with working timers so you will need to give me an example of how you do it and I can assist in linking it in if it doesn't use analogWrite()

zhomeslice:
change this line
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, 0.04, 0.1, 0.0, DIRECT);
to

PID myPID(&Input, &Output, &Setpoint, 0.04, 0.1, 0.0, REVERSE);

or
myPID.SetControllerDirection(REVERSE); // at any time to reverse the PID
Should reverse the direction of the code
Pin 9 should work just as good fine
change
#define PWMpin 3
to:
#define PWMpin 9
it completely work with

TCCR1B = TCCR1B & B11111000 | B00000001;;// if this is place in setup

analogWrite(9,OUTPUT);/// this will produced the pwm of 31khz

now I'm not familiar with working timers so you will need to give me an example of how you do it and I can assist in linking it in if it doesn't use analogWrite()

This looks good then :slight_smile:
Let me know how it goes.

ok i will test on hardware tomorrow morning and will let you know how its work and please just give a rough sketch of code how codes is going i will be very grateful

i ran this code on hardware but not running good
i hav update the code and kindly review and tell me if code is good to go or not

Tachometer_Final_with_Self_Test_ONE_PPR.ino (6.58 KB)

naeemawan36:
i ran this code on hardware but not running good
i hav update the code and kindly review and tell me if code is good to go or not

Can you post some of the debug output so I can get an idea as to what you are struggling with.
Also how many pulses per revolution does your proximity sensor have?
I'll renew you code as soon as I have an idea what you mean by not running good.
At least it's running that is a great sign :slight_smile:

naeemawan36:
i ran this code on hardware but not running good
i hav update the code and kindly review and tell me if code is good to go or not

Ok I reviewed your code.
You have it setup to use an encoder/tachometer/proximity sensor with the resolution of 400 Pulses per revolution the exact same as I have on my encoder.
you haven't followed my instructions to match your system with mine Please read Post #58 below

zhomeslice:
Ok I reviewed your code.
You have it setup to use an encoder/tachometer/proximity sensor with the resolution of 400 Pulses per revolution the exact same as I have on my encoder.
you haven't followed my instructions to match your system with mine Please read Post #58 below

Sir i again review that post and update my code then i will let u know

Sir this is not producing pid 2 erroe are comming 1st is that it i think it didnt enter in loop and 2nd if set setpoint to 400 it automatically chnges the setpoint 4100 then to 7500 am confused why it changing setpoint
Setpoint = (Setpoint == 4100) ? 7100 : 4100; this syntax i didnt understand
and can u explain that is printing in blue circle

here is what i m saying set point is 4100 but in serial printer it changes

please sir help my input is in point value and completely confused Sir help me
am using proximity sensor and motor has 3 blades of fan is this is okie ?

Thanks alot for ur time i completed tacho metre and pid
really thankful to u for ur help and kind responce :slight_smile: :slight_smile: :slight_smile: