Pages: [1]   Go Down
Author Topic: DC motor speed control.  (Read 776 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25732
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: December 20, 2012, 06:42:11 am by AWOL » Logged

"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.

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12534
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


* circuit.jpg (592.14 KB, 4410x2469 - viewed 55 times.)
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3413
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You should use a darington pair driver, it is more stable and will last longer. If you want to go in both directions, get an H-bridge.



http://www.google.com/imgres?start=207&um=1&hl=en&safe=off&tbo=d&biw=1440&bih=754&tbm=isch&tbnid=c6snM7yNRGutfM:&imgrefurl=http://www.electronics-tutorials.ws/io/io_7.html&docid=lX9Zbtbrh_KcCM&imgurl=http://www.electronics-tutorials.ws/io/io32.gif&w=295&h=270&ei=B13UUIGvB87V0gGl-4C4Cg&zoom=1&iact=hc&vpx=473&vpy=186&dur=2879&hovh=215&hovw=235&tx=108&ty=155&sig=106574055291386674452&page=7&tbnh=142&tbnw=155&ndsp=39&ved=1t:429,r:18,s:200,i:58
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Pages: [1]   Go Up
Jump to: