Hello,
This is a project of obtaining rpm and PID values from DC motor
I’m told by my professor that if I put my finger on the spinning DC motor, and the value of rpm changes then I should have the microcontroller (Arduino Uno) to apply PID to readjust the rpm back to its normal regular speed…
For example…
Like let’s say the rpm of a motor goes to 12000 rpm and then I add my finger to the dc motor and it reads 8000 rpm and so my professor told me to have PID applied so that even if I put my finger into the dc motor, the PID will take care and have the dc motor go back to original 12000 rpm.
how would I make this to be?
I wrote a pseudocode below to show what I mean…
if interruption on spinning dc motor occurs
then calculate PID on rpm
how would I make this happen?
thanks.
Btw, I attempted the code below
So this is a code was attempted to try to get the PID values, calculation from according to what I described above (as objective)
and I only get rpm values as result.
#include <Wire.h>
#include <PID_v1.h>
/////////////////
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);
////////////////
int potPin = 0; // Analog in 0 connected to the potentiometer
const int transistorPin = 9; // connected to the base of the transistor
int potValue = 0; // value returned from the potentiometer
int ledPin = 13; // IR LED connected to digital pin 13
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;
void rpm_fun()
{
//Each rotation, this interrupt function is run twice, so take that into consideration for
//calculating RPM
//Update count
rpmcount++;
}
void setup() {
///////////////////
//initialize the variables we're linked to
Input = analogRead(0);
Setpoint = 100;
//turn the PID on
myPID.SetMode(AUTOMATIC);
///////////////////////
// set the transistor pin as output:
pinMode(transistorPin, OUTPUT);
// initialize the serial communication:
Serial.begin(9600);
attachInterrupt(0, rpm_fun, FALLING);
//Turn on IR LED
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
rpmcount = 0;
rpm = 0;
timeold = 0;
}
void loop() {
//////////////
Input = analogRead(0);
myPID.Compute();
analogWrite(3,Output);
//Serial.println();
/////////////////////
// read the potentiometer:
int sensorValue = analogRead(A0);
// map the sensor value to a range from 0 - 255:
int outputValue = map(sensorValue, 0, 1023, 0, 255);
// use that to control the transistor:
// read the potentiometer, convert it to 0 - 255:
potValue = analogRead(potPin) / 4;
// use that to control the transistor:
analogWrite(9, potValue);
// send the value of analog input 0:
//Serial.println(analogRead(A0));
// wait a bit for the analog-to-digital converter
// to stabilize after the last reading:
//delay(10);
delay(1000);
//Don't process interrupts during calculations
detachInterrupt(0);
//Note that this would be 60*1000/(millis() - timeold)*rpmcount if the interrupt
//happened once per revolution instead of twice. Other multiples could be used
//for multi-bladed propellers or fans
rpm = 30*1000/(millis() - timeold)*rpmcount;
timeold = millis();
rpmcount = 0;
//Print out result to lcd
//Serial.clear();
Serial.println("RPM=");
Serial.println(rpm);
//Restart the interrupt processing
attachInterrupt(0, rpm_fun, FALLING);
}