Pages: [1]   Go Down
Author Topic: How to change the speed of a DC motor (Max/MSP)  (Read 9144 times)
0 Members and 1 Guest are viewing this topic.
Cambridge (UK)
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
fura ura
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello Arduino forum

I would like to ask for some help. I could not find my answer on the forum, I am new to the Arduino and to the code as well.

I would like to control the speed of a simple DC motor. It has two wires (grand and the 5V). Is it possible to control its spin rate with a message received from the digital pins (Max/MSP)?

Any help appreciated.
Thank you
Krisztian
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 197
Posts: 12744
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

For controlling the speed of the motor, this should get you started...

http://www.arduino.cc/playground/Main/DCMotorControl

- Brian
Logged

Califonia, US
Offline Offline
Newbie
*
Karma: 0
Posts: 44
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not sure what you mean by:

Quote
control its spin rate with a message received from the digital pins (Max/MSP)

I'm guessing you mean via a Serial connection over the USB maybe?

Either way if all you want to control is speed that can be easily done using one of the PWM pins on the Arduino.  However, because of the back EMF created by the motor you should switch it on by using a transistor or MOSFET with a protection diode.  See the section on protection diodes for relays here http://www.kpsec.freeuk.com/components/diode.htm to get an idea.  You'll probably also want to put a small ceramic capacitor (0.1 - 1.0 uF) across the terminals of the motor to smooth the switching noise.

If you want to control speed and direction of the motor you'll need an H-bridge.

By the way, since you say the motor is 5V I would be really careful about sharing the power line between your Arduino and motor since the motor will add a lot of noise to your circuit.  If you must use the same power bus, put a big old capacitor from +5V to GND (100uF at least, the more the merrier).  

Good luck!
Logged

Cheers!

Cambridge (UK)
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
fura ura
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 ;D
Logged

Cambridge (UK)
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
fura ura
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello Both,

thank you for the quick reply. I do not knot how I could miss the playground tutorial. I will start studying this, I really thought it is easier.

Thank you again, I will post my progress.
Krisztian
Logged

Cambridge (UK)
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
fura ura
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello

I tried to mix two codes, as I would like to control the fan's speed with an H-Bride through the serial port (Max/MSP).

If anyone could help me with the delay message, it would be appreciated.

The fan spins, I just could not control the seed of it. Now I am using a 5V fan with the arduino's power.

I am not quite sure what I actually have to send between the HIGH and a LOW to control its speed.

the code:
Code:
int motor1Pin = 10;    // H-bridge leg 1 (pin 2, 1A)
int motor2Pin = 11;    // H-bridge leg 2 (pin 7, 2A)
int enablePin = 9;    // H-bridge enable pin
 
unsigned char speed;
unsigned int low = 1;
unsigned int high = 10;
unsigned int baseTime = 10;
  
void setup() {
  Serial.begin(9600);
  // set the switch as an input:


  // set all the other pins you're using as outputs:
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);

  // set enablePin high so that motor can turn on:
  digitalWrite(enablePin, HIGH);
 
}

void loop() {

    digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
   delayMicroseconds(low*10);
    digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
   delayMicroseconds(high*10);


if (Serial.available())
{
speed = Serial.read();
low = speed;
high = baseTime - speed;
}

}
Logged

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

Any reason for not using the PWM outputs, instead of the DIY PWM you've got?
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.

Cambridge (UK)
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
fura ura
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello,
the answer is: I do not know, I put them back to pin 3 and 4 and still the same. The speed does not change with the message I receive from Max/MSP (0-255).

On the H-Bridge the connections are like on here:
http://itp.nyu.edu/physcomp/Labs/DCMotorControl

except the switch pin (deleted from code as well)

1, Funny thing: If I unplug Motor Logic Pin1 (which is digPin 4 (LOW) ) the FAN still spins with the same speed. Does it just need HIGH to work?

2, Could anyone tell me what messages a PWM FAN needs in HIGH and LOW as minimum (lowest speed) and maximum (fastest)? Do I have to change both or just one to change the speed? 5V and 12V fans I am using.

The resent code (does not change speed with the Max/MSP message):
Code:
int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
int enablePin = 9;    // H-bridge enable pin
int val = 0;
int speed = 0;
 

int high = 10;

  
void setup() {
  Serial.begin(9600);

  // set all the other pins you're using as outputs:
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);

  // set enablePin high so that motor can turn on:
  digitalWrite(enablePin, HIGH);
 
}

void loop() {
  
  
  // check if data has been sent from the computer
  if (Serial.available()) {
    // read the most recent byte (which will be from 0 to 255)
    val = Serial.read();
    speed = map(val, 0, 255, 0, 1000);
 
    high = speed;
  
    Serial.println(val);    
  };
    
   digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
   delayMicroseconds(100);
   digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
   delayMicroseconds(high);
  }

Thank you,
K
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, on the playground page for "Using an Arduino to control the speed and direction of a DC Motor" it says that the schematics for that project are wrong, does anyone know where to get the correct schematics?

thank you

DT
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I couldn't find any info on the correct schematics.
Looking at the parts, it seems that the relay is to actuate the motor from the external power supply (not 5V from Arduino).
If that is the case, how can you write PWM to a relay and control the speed of the motor?
In my understanding, a relay is either ON or OFF...

??
Logged

Washington
Offline Offline
God Member
*****
Karma: 38
Posts: 789
Firefox & Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Relays can PWM but the contacts will soon fuse together if you run significant current through them.
It is also VERY noisy.
Logged

Avoid throwing electronics out as you or someone else might need them for parts or use.
Solid state rectifiers are the only REAL rectifiers.
Resistors for LEDS!

Pages: [1]   Go Up
Jump to: