Controlling a DC Brushless Motor Speed

Hi guys! I’m currently doing a project regarding the use of a brushless DC fan and so far so good. However I am having a few issues when it comes to controlling the speed of the fan. Here is the code I have so far

//Fan Control Deluxe by Ethan Keating

#include <Servo.h>
int pinButton = 2; //Pin the button is connected too
int pinButtonL = 3;//Low speed button
int pinButtonM = 4;//Medium speed button
int pinButtonH = 5;//High speed button
int fanSpeed;
int press1 = 0;
int potpin =0;
int fan = 11;
int state = HIGH;
int val;
int reading;
int previous = LOW;
long time = 0;
long debounce = 200;

Servo myservo;

void setup()
{
  pinMode(pinButton, INPUT);
  pinMode(pinButtonL, INPUT);
  pinMode(pinButtonM, INPUT);
  pinMode(pinButtonH, INPUT);
  pinMode(fan, OUTPUT);
  myservo.attach(9);
  digitalWrite(2,HIGH); //When button is pressed it will turn on the fan
  digitalWrite(3,HIGH); //When button is pressed it will set fan on low speed
  digitalWrite(4,HIGH); //When button is pressed it will set fan on med speed
  digitalWrite(5,HIGH); //When button is pressed it will set fan on high speed

}

void loop()
{
 val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);                  // sets the servo position according to the scaled value 
  delay(15);                           // waits for servo to get there
  reading = digitalRead(pinButton);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(fan, state);

  previous = reading;
}

Basically I have four buttons. Button one which is connected to digital PWM 2 is my on/off button. Where as the pinButtonL/M/H are the buttons I want to use to control the speed with L being Low, M being Medium and H being High. I have been told I could probably just use one button and do multiple rotations until it’s at the speed where I want it. Any help would be much appreciated. Any further questions, please feel free to ask.

Thanks :slight_smile:

  pinMode(pinButton, INPUT);
  pinMode(pinButtonL, INPUT);
  pinMode(pinButtonM, INPUT);
  pinMode(pinButtonH, INPUT);
  pinMode(fan, OUTPUT);
  myservo.attach(9);
  digitalWrite(2,HIGH); //When button is pressed it will turn on the fan
  digitalWrite(3,HIGH); //When button is pressed it will set fan on low speed
  digitalWrite(4,HIGH); //When button is pressed it will set fan on med speed
  digitalWrite(5,HIGH); //When button is pressed it will set fan on high speed

Give the pins nice names, then refer to them as "Hey, you". Why?

    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

or

   state = !state;

whichever is easier.

Basically I have four buttons.

Only one of which you actually read.

You currently seem to be setting the speed of the motor based on the value of a potentiometer, although there is nothing in the comments or your text to link what the code is doing to the hardware you have.

What, exactly, is the problem?

You could use one switch to control the speed. Count the number of presses, instead of just toggling state. When the number of presses exceeds 3 (off = 0, low, medium, high = 3), reset it to 0. Then, make your decision on what to do (using a switch statement with 4 cases) based on the number of times the switch has been pressed.

Hi, I am puzzled, you have a servo program, yet you want to control a Brushless fan HOW ? ? ?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf? Can you please post a picture of your project.

WHY??? You have the project infront of you, we don't. You know how it is wired, we don't. You know how you are powering the motor and arduino, we don't.

So please supply us with as much info as possible so we can give you accurate solutions to your problems.

Tom..... :)

you have a servo program, yet you want to control a Brushless fan HOW ? ? ?

Presumably (but not stated) via an ESC (Electronic Speed Control). Widely used by RC (Radio Control) systems to convert the PWM output of an RC receiver to the waveforms required to drive a 3 wire brushless motor, but an ESC can be driven directly from an Arduino PWM output if required.

an ESC can be driven directly from an Arduino PWM output if required.

On the other hand, they are most commonly driven as though they were servos, using the Servo library. After the correct arming sequence, of course.

Sorry for the confusion, I forgot to add that I am actually also controlling a servo with a pot. I’ll attach my fritizing sketch so you can see what i’ve done so far, however I am not completely finished. I will also post what I am trying to fulfil so you have more understanding on what I am trying to create here.

Fan Control Deluxe:

  • Use an H-Bridge to control the speed (and optionally the direction) of a fan motor.
  • The motor is mounted on a servo to control direction of fan.
  • Two pushbuttons (+/-) or one potentiometer control the speed of the fan.
  • Two pushbuttons (left/right) or one potentiometer control the servo and with it the direction of the fan.

Options:

  • One pushbutton enable/disables left/right fan oscillation.
  • Two thermistors mounted at some distance of the fan measure the temperature and automaticallyt direct the fan to the hottest of these two spots.
  • Three buttons for direct setting of off/medium/full speed.
  • A Processing program displays a GUI to remotely control the fan operation.

There are a few things I obviously haven’t done yet (For example added in my H-bridge) but this is just because I am trying to get everything to work before I move onto that step. I am still relatively new to Arduino.

Thanks guys :slight_smile:

Hi, (after recovering from 5minutes in fetal position from looking at fritz.)

Forget trying to power the servo from the arduino, it cannot supply enough current. Forget about powering the motor from the arduino for the same reason.

I suggest you start from scratch and just program to get the motor control working. Then start from scratch get the servo working. Then put it together. Now please a circuit diagram, full one just the motor , forget the servo for now. Do it in anything except Fritz, hand draw and photograph if you like. What is the DC Brushless Fan, because of the way they work, I doubt you will be able to reliably control its speed by controlling its power supply.

Tom...... :)