Change the speed of 4 motors with 1 button

Hello !

Sorry if I express myself badly, I’m French. :grinning: I start from a few days to learn how to use Arduino. I would like to now if it’s possible to direct the speed of 4 motors thanks to a button. I would like that the 4 motors run with 5V when the button is not pressed. Then, when pressing on the button, 2 motors began to run faster and two other start to slow down. I would like if it is possible that you give me lines of work.

Thank you in advance for your answers :slight_smile:

PS: These engines are computer fans.

What you want to do is perfectly possible.

Do you know how to read the state of a switch or button and react to the change from off to on for instance ? Do you know how to control the speed of a motor by outputting a value from the Arduino ?

The Arduino cannot output more than 5V so without external hardware the motors will have to start slower than you want. In any case the Arduino cannot output a varying voltage but it can output 5V that is being turned off and on rapidly with a varying on/off ratio that will fool most devices.

How are the motors connected to the Arduino ? How are the motors powered ?

Motors should NOT be powered from the Arduino. They should have their own power supply.

...R

Thank you for your answers ! :)

In terms of the button, yes I know how to retrieve a value. (DigitalRead () which gives a HIGH or LOW value) Then in terms of control of the speed I confess that I have trouble understanding. I know we need to vary a value between 0 and 255 but it is very unclear to me. Then to connect the motors I thought using two stacked shields (motor shiel I think, I do not have it with me). Then I would have connected my fans, to the connections B and A. And I thought allimenter motors with Arduino card but I did not que'elle that could deliver 5V.

How do I process for the code? What connections should I do so that my fans can rotate with a power of 15V?

thank you again

That shouldn't be a problem.

I would like that the 4 motors run with 5V when the button is not pressed. Then, when pressing on the button, 2 motors began to run faster and two other start to slow down.

The two most important concepts in programming are conditional execution, such as [u]If-Statements[/u] (if-this, do that) and [u]loops[/u] (doing things over-and-over, usually until some condition is met).

First, I suggest you read-through the Programming Language Reference and look at some of the examples so you can get an idea of what you can do and what the programs ('sketches") look like. You won't understand it all or remember it all, but it won't take you that long to read-through the language reference, and it should give you some ideas.

Then, I suggest you get a switch or (push-button switch) and a couple of LEDs (and the required "current limiting" resistors for the LEDs).

You can actually do some things with the pin-13 LED already on the board, such as turning it on & off or dimming it when you push a button.

LED are dimmed using PWM, and DC motors are controlled the same way. So, you can do a lot of relevant experimentation with LEDs before connecting motors. PWM simply means switching on & off rapidly with a variable duty cycle... If the LED is on 10% of the time and switching rapidly, it will appear dim, or the motor will run slow. PWM is built-into the Arduino (as you'll see when you read the language reference).

The trick to project development (especially software) is to take it one step at a time! Add one or two lines of code at a time (or whatever minimum amount of code that makes sense and compiles), and then compile & test that code before moving on. As you gain experience, you can write more than one or two lines at a time, but NOBODY just sits-down and writes a whole program... Programs are "developed" one-step, or one-part at a time.

The Arduino can't directly put-out enough current to power a motor. And, most computer-fans are 12VDC. There are examples of how to "boost" the voltage and/or current with a transistor or MOSFET. PWM will work fine through a transistor or MOSFET

Some (3-wire) computer fans have speed-feedback so you can precisely control the speed. This can be VERY helpful because if you set your PWM fan-speed to "25" (that would be about 10% of the 8-bit maximum of 255) the fan might not get enough power to start moving. With feedback, your software would know to give it more "juice" and then it would start regulating speed. But, speed-feedback makes your software more complicated, so that's something you can add later, if you have 3-wire fans, and if you feel you need it.


I suggest you start with the basic Blink LED example. Then, modify the sketch to make the LED dim... Just try to understand how the output-side of your software can control the brightness of an LED, or the speed of a motor. There are LED dimming examples, but modify the code to do something you want to do!

Next, add a push-button switch. This is the input-side of your hardware & software. Modify your sketch so the switch does something to the LED. Maybe make it dim when you push the button once, then bright when you push it again... Whatever you want to do. Then back to dim when you push the button again it again (remember loops!). The idea is to understand how the to make your sketch do something you want it to do when you push the button.

Now, add a 2nd LED that works the opposite way... One LED dims while the other get's brighter, etc.

Now, that you know how to control the brightness of an LED with software, you know how to control motor speed. It's time to connect the motors and motor-driver circuit. I'd leave the LEDs connected to help with troubleshooting. When the LED is off, the motor should be off. When the LED is dim, the motor should be running slowly, etc. The motors are unlikely to be "linear", so half-brightness may not be half-speed. You can experiment and tweak your code as necessary.

Once you can control motor speed by reading the push-button, you can refine your code and add the finishing touches, etc.

hen in terms of control of the speed I confess that I have trouble understanding. I know we need to vary a value between 0 and 255 but it is very unclear to me.

[u]PWM[/u]

Thank you a lot !

You have succeeded in show me a path to follow and it delighted me! I'm really happy! I'll test it all and I'll let you know if I have a problem. Thank you again and again.

What do you think about this ? Can it work ? I have test him with proteus Isis. I noticed the voltage across the LEDs but I have a value of between 2.57V and 0009 V for the red LED and a constant 0.09V value for the green light. Could you help me ?


//variables int RedLed = 2; int GreenLed = 3; int Button = 8; byte Push = 0; byte i = 1;

void setup () { pinMode (RedLed, OUTPUT); pinMode (GreenLed, OUTPUT); }

void loop () { while (digitalRead (Button) == HIGH) //Button is pressed { Push++ ; //I add 1 to the variable "Push" delay(1000) ;

if (int (Push) == 1) { digitalWrite (RedLed , 100); //Here i want to change the intensity of the leds digitalWrite (GreenLed , 0); } else if(int (Push) == 2) { digitalWrite (RedLed , 150); //Here i want to change the intensity of the leds digitalWrite (GreenLed , 255); } else if(int (Push) == 3) { digitalWrite (RedLed , 200);//Here i want to change the intensity of the leds digitalWrite (GreenLed , 200); } else if(int (Push) == 4) { digitalWrite (RedLed , 255); //Here i want to change the intensity of the leds digitalWrite (GreenLed , 150); } else if(int (Push) == 5) { digitalWrite (RedLed , 0); //Here i want to change the intensity of the leds digitalWrite (GreenLed , 100); } else { Push = i; //The variable Push go back to 0 } } }


      digitalWrite (RedLed , 100); //Here i want to change the intensity of the leds

Lines like these are not changing the intensity they are trying to write a number to a digital pin. A digital pin can only be on or off. There is no possibility of a value between the two. If you want to change the intensity of the LED you must use one of the PWM enabled pins on the board, denoted by the ~ character alongside their pin number and use

     analogWrite (RedLed , someNumber); //Here i want to change the intensity of the leds

Vary the value of someNumber between 0 and 255 and the brightness of the LED will change. So, read the input from the switch, change the value of someNumber and use it with the analogWrite()

if (int (Push) == 1)

There is no need to change the variable type of Push, it is already an int.

As a side note, you are currently detecting whether a switch IS closed but it would be better to detect when a switch BECOMES closed to change the value sent to the LED. That way you do not need the delay() in the code. Look at the StateChangeDetection example in the IDE to see how to do it.

See: http://www.thebox.myzen.co.uk/Tutorial/LEDs.html You will damage your Arduino if you don't use a LED series resistor. You cannot effectively measure the PWM waveform without using an oscilloscope.

Thank you!

Your answer really helps me move forward by changing my mistakes. I thank you and escuse me to ask you questions that are completely stupid for you! I think. : D

The only stupid question is the one that isn't asked.

Ok ! This code work perfectly ! ... I think ! Some mistakes or advises ?

//variables const int RedLed = 2; const int GreenLed = 3; const int Button = 8; byte Push = 0; byte buttonState = 0; byte lastButtonState = 0;

void setup () { pinMode (RedLed, OUTPUT); pinMode (GreenLed, OUTPUT); }

void loop () { buttonState = digitalRead(Button);

if (buttonState != lastButtonState) { if (buttonState == HIGH) { Push++ ; } } lastButtonState = buttonState;

if (Push == 1) { analogWrite (RedLed , 0);//Here i want to change the intensity of the leds analogWrite (GreenLed , 255); } else if(Push == 2) { analogWrite (RedLed , 100);//Here i want to change the intensity of the leds analogWrite (GreenLed , 200); } else if(Push == 3) { analogWrite (RedLed , 150);//Here i want to change the intensity of the leds analogWrite (GreenLed , 150); } else if(Push == 4) { analogWrite (RedLed , 200);//Here i want to change the intensity of the leds analogWrite (GreenLed , 100); } else if(Push == 5) { analogWrite (RedLed , 255);//Here i want to change the intensity of the leds analogWrite (GreenLed , 0); } else { Push = 0;//The variable Push go back to 0 } }


Now I will test with motors ! I scared !

You are using pin 2 for analogWrite() but it is not a PWM pin, at least not on a Uno.

You can tidy up the program using arrays like this (not tested)

//variables
const byte RedLed = 5;  // ******************** CHANGED TO A PWM PIN *********************
const byte GreenLed = 3;
const byte Button = 8;
byte Push = 0;
byte buttonState = 0;
byte lastButtonState = 0;

const byte redValues[] = {0, 100, 150, 200, 255};
const byte greenValues[] = {255, 200, 150, 100, 0};

void setup ()
{
  pinMode (RedLed, OUTPUT);
  pinMode (GreenLed, OUTPUT);
}

void loop ()
{
  buttonState = digitalRead(Button);

  if (buttonState != lastButtonState)
  {
    if (buttonState == HIGH)
    {
      Push++ ;
    }
    if (Push == 5)
    {
      Push = 0;
    }
  }
  lastButtonState = buttonState;

  if (Push != 0)
  {
    analogWrite (RedLed , redValues[Push - 1]);
    analogWrite (GreenLed , greenValues[Push - 1]);
  }
}

My idea was to use two stacked shields. So I would have obtained 4 inputs, for my motors. Is it possible you to send a higher energy to 5V in this situation? Otherwise how to send 5V sometimes, sometimes 10 and 15 V. Is there a mathematical convertion to know the voltage, according to a given PWM? Or they have nothing in common. In the code, what are the steps to follow in order to achieve the desired result?

Really thank you for your help!

PS: I tested my fans and well run on 15 V.

If you use a PWM pin on the Arduino it can only output 0V or 5V, nothing in between or above 5V. What PWM does is to pulse the pin with 5V. The ratio of the length of the on pulse to that of the off pulse is what determines what can be regarded as the average voltage for most purposes but in reality is still either 0V or 5V

What a motor shield will usually let you do is to feed a PWM signal to one its motor control pins so that the speed of the motor is controlled by the on/off ratio of the PWM signal. Power for the motor comes from an external source other than the Arduino, which cannot supply enough current anyway, and the external voltage can be higher than 5V if required.

Ok thank you ! I think I can finish the code now ! I will show you the code latter :)

Ok ! I have finish the code ! I have take a lot of time because I didn't have a lot of time ! Could it work ?


//variables const int motor1 = 3; const int motor2 = 11; const int Button = 8; byte Push = 0; byte buttonState = 0; byte lastButtonState = 0;

void setup () { pinMode (motor2, OUTPUT); pinMode (motor1, OUTPUT); pinMode (Button, INPUT); }

void loop () { buttonState = digitalRead(Button);

if (buttonState != lastButtonState) { if (buttonState == HIGH) { Push++ ; } } lastButtonState = buttonState;

if (Push == 1) { analogWrite (motor1 , 175);//Here i want to change the intensity of the motors analogWrite (motor2 , 175); } else if(Push == 2) { analogWrite (motor1 , 50);//Here i want to change the intensity of the motors analogWrite (motor2 , 255); }

else { Push = 0;//The variable Push go back to 0 } }


Thank you in advance ! :)

Could it work ?

Have you tried it and does it work ?

How is the switch wired ? How are the motors powered ?

I will test it on Tuesday with my professor of electronic :) !