How to change the speed of a DC motor (Max/MSP)

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

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

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

  • Brian

I'm not sure what you mean by:

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 Diodes 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!

;D

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

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:

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;
}

}

Any reason for not using the PWM outputs, instead of the DIY PWM you've got?

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):

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

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

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

??

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