Thursday, September 23, 2010

Ducted Fan and Arduino

So I was looking for more of a breeze...




...seems like I've got some good breeze now with the 56mm ducted fan. Be careful, these ducted fans will slice your finger off!


Below is the sketch that works with the Great Planes ESC (adapted from other people's ESC sketches). It arms the ESC, then sweeps the speed up, stop, back to full speed, and sweeps back down to off.

#include <Servo.h>

Servo myservo;

void arm(){
// arm the speed controller, modify as necessary for your ESC
Serial.println("arming");
setSpeed(30);
delay(2000);
setSpeed(90);
delay(2000);
Serial.println("armed");
setSpeed(30);
delay(2000);
}

void setSpeed(int speed){
// speed is from 0 to 100 where 0 is off and 100 is maximum speed
//the following maps speed values of 0-100 to angles from 0-180,
int angle = map(speed, 0, 100, 0, 180);
myservo.write(angle);
}

void setup()
{
Serial.begin(115200);
myservo.attach(9);
arm();
}

void loop()
{
int speed;

Serial.println("Sweeping up");
for(speed = 37; speed <= 90; speed += 1) {
setSpeed(speed);
Serial.println(speed);
delay(100);
}
setSpeed(30);
delay(1000);
Serial.println("Sweeping down");
for(speed = 90; speed > 37; speed -= 1) {
setSpeed(speed);
Serial.println(speed);
delay(100);
}
Serial.println("30 halting...");
setSpeed(30);
delay(5000);
}

10 comments:

mohankumar said...

the code posted here makes the speed high and low.Can u please tell me how to modify this code to get a brushless motor running at constant speed?

t11s said...

mohankumar, simply call my "setSpeed();" command with a speed (valid ones are from 37 to 90 for this particlar fan), then don't call setSpeed again, and the speed should not change.

Bilal said...

Hi dear it is very useful .Thank you so much for share.Can we control Esc by serial port ? any idea?

t11s said...

Bilal, you could always use an Arduino or other microcontroller to convert a serial command to timed digital output.

Alternatively, you might be able to use RTS/CTS and DTR/DSR and direct digital outputs from an RS-232 port, but they may need to be level shifted to not blow out the ESC.

Here is an example:

http://www.the-control-freak.com/COMDIO/COMDIO.htm

jon phillips said...

Hi There,
Can you tell me how to modify the code to read the state of a switch to turn the fan on full speed?
Thanks

jon phillips said...

Hi there, Can you tell me how to modify the sketch to read the state of a switch to turn the fan on full speed?
Thanks

t11s said...

Jon, I'm not sure how your switch is set up. You can read here about interfacing to a switch:

http://www.ladyada.net/learn/arduino/lesson5.html

The important thing is to first call my arm() procedure to arm the motor controller. Then call my procedure setSpeed(90) for full speed, or setSpeed(30) for low speed. Depending on your motor controller and fan, you may need to move these numbers up or down a bit.

Igor Lollato said...

Please, im using a Arduino with ESC and a brushless with 2800KV, the motor beep but i can´t make it work with the arduino just with the radio control, there is something wrong with the code? ??
My e-mail is igor_machado1@hotmail.com
Please give me a hand xD.

Anonymous said...

can someone help me ????
everything is working but sometimes i write speed 30 and it goes like 70, and then i write 0 and goes like 50.
My e-mail is
igor_machado1@hotmail.com

Thanks a lot.

t11s said...

Igor, it sounds like your ESC is looking for a PWM speed that is below the duty cycle of the "myservo" class. You may need to find the myservo class and re-write it to have a shorter duty cycle when you send it a "0".