Go Down

Topic: DC motor speed control. (Read 855 times) previous topic - next topic

matteodd90

Hello,

I am a newbie and this is the first post here. I am trying to do a speed controller on a dc motor trough a photo interrupter(KTIR0611s). Something like what the guy in this video does:
http://www.youtube.com/watch?v=PWoQQwbTJMw
but I want to calculate the number of revolution per second my motor does.
I control the PWM of the motor trough a potentiometer.

Here is my code:

int Val_PWM = 0; //PWM value (0-255)
int Val_Slider = 0;  //Slider value
const int SLIDER = A5;    //Slider 5 kohm
const int MOTOR =  10; 
float speed;
unsigned long time;
unsigned long prev_time = 0;
unsigned long delta;
const int photo_interrupter = 2;



void setup(){
 
  attachInterrupt(0, speed_control , FALLING);
  Serial.begin(9600);
  pinMode(photo_interrupter, INPUT);
  pinMode(SLIDER, INPUT); 
  pinMode(MOTOR, OUTPUT);
 
}


void loop() {


   Val_Slider = analogRead(SLIDER); 
   Val_PWM = Val_Slider;
   analogWrite(MOTOR, Val_PWM/4); 

}


void speed_control(){
     
    time = millis();
    delta = time - prev_time;
    speed = 500.0/(float)delta;  //the motor has two blades so it does half revolution per interrupt
    Serial.print ("revolutions per second:");       
    Serial.println (speed);
    prev_time = time;
     
}


The problem is that when the PWM is different from 0 it happens that many interrupts occur also when nothing pass trough the photo interrupter even if the wire on the digital pin 2 is detached, while when I don't supply the motor the photo interrupter works normally.

What the cause might be?

Sorry if my English is not correct.

AWOL

#1
Dec 20, 2012, 12:37 pm Last Edit: Dec 20, 2012, 12:42 pm by AWOL Reason: 1
Don't do serial I/O in an interrupt.
Please use code tags when posting code.

Quote
What the cause might be?

Noise? Do you have very long wires to the photo-interrupter?

Please post a schematic
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PeterH

Does it make any difference whether the motor is actually connected? I mean, if you connect it's power supply to the driver board/shield and switch it on in software, but don't connect the cables to the motor itself.
I only provide help via the forum - please do not contact me for private consultancy.

matteodd90

Quote
Noise? Do you have very long wires to the photo-interrupter?


I have really short wires (maximum 8 centimeters)

Quote
Please post a schematic


I attached a circuit I draw with Fritzing, I hope it is understandable (it is the first time using it)

Quote
Does it make any difference whether the motor is actually connected? I mean, if you connect it's power supply to the driver board/shield and switch it on in software, but don't connect the cables to the motor itself.


If I connect it's power supply and switch it on in software but i don't connect the cables to the motor the circuit works. I mean the interrupts occur only in the right moment printing on the serial acceptable results.

HazardsMind

My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Go Up