Skip to content

Instantly share code, notes, and snippets.

@spuder
Last active April 8, 2023 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spuder/06122b59148af579ae1f1ea644ac7595 to your computer and use it in GitHub Desktop.
Save spuder/06122b59148af579ae1f1ea644ac7595 to your computer and use it in GitHub Desktop.
Arudino PWM control AnkerMake M5
// https://www.facebook.com/page/107323940927534/search/?q=fan
const int fan_control_pin = 9; //Blue Wire on Fan (about 25kHz PWM)
int count = 0;
unsigned long start_time;
int rpm;
void setup() {
TCCR1A = 0; // undo the configuration done by...
TCCR1B = 0; // ...the Arduino core library
TCNT1 = 0; // reset timer
TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11); //Undo default timers
TCCR1B = _BV(WGM13) | _BV(CS10); //for pins 9 & 10
ICR1 = 320; // PWM will be 0 to 320 instead of 0 to 255
pinMode( fan_control_pin, OUTPUT);
OCR1A = 0; //set pwm to 0 out of 320 on pin 9
OCR1B = 0; //set pwm to 0 out of 320 on pin 10
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(2), counter, RISING); //Yello Wire with 5V pullup
}
void loop() {
for(int pwm = 0; pwm <= 320; pwm += 64){
OCR1A = pwm;
delay(5000);
start_time = millis();
count = 0;
while((millis() - start_time) < 1000){
}
rpm = count * 30; //60/2
Serial.print("PWM = ");
Serial.print(map(pwm, 0, 320, 0, 100));
Serial.print("% , Speed = ");
Serial.print(rpm);
Serial.println(" rpm");
}
}
void counter() {
count++;
}
// Standard PWM
// 490hz
int fanPin = 9; // digital pin 19 (analog pin 5) connected to the fan
int speed = 0; // variable to store the desired fan speed
unsigned long previousMillis = 0; // variable to store the previous time
const long interval = 5000; // interval in milliseconds for changing the fan speed
int ledPin = 13; // built-in LED pin
void setup() {
pinMode(fanPin, OUTPUT);
analogWrite(fanPin, 0); // set the initial fan speed to 0%
pinMode(ledPin, OUTPUT); // set the LED pin as an output
}
void loop() {
unsigned long currentMillis = millis();
// check if the interval has elapsed
if (currentMillis - previousMillis >= interval) {
// update the previous time
previousMillis = currentMillis;
// switch the fan speed between 0%, 30%, 60%, and 100%
if (speed == 0) {
speed = 76; // 30% duty cycle
blinkLED(1); // blink the LED once
} else if (speed == 76) {
speed = 153; // 60% duty cycle
blinkLED(2); // blink the LED twice
} else if (speed == 153) {
speed = 255; // 100% duty cycle
blinkLED(3); // blink the LED three times
} else {
speed = 0; // 0% duty cycle
blinkLED(4); // blink the LED four times
}
}
// set the fan speed using the PWM signal
analogWrite(fanPin, speed);
delay(100); // add a small delay to avoid rapid changes in fan speed
}
void blinkLED(int numBlinks) {
for (int i = 0; i < numBlinks; i++) {
digitalWrite(ledPin, HIGH);
delay(250);
digitalWrite(ledPin, LOW);
delay(250);
}
}
@spuder
Copy link
Author

spuder commented Mar 25, 2023

Screen Shot 2023-03-25 at 4 22 52 PM

@spuder
Copy link
Author

spuder commented Mar 25, 2023

Screen Shot 2023-03-25 at 4 27 17 PM

@spuder
Copy link
Author

spuder commented Mar 25, 2023

Photo credit Cisien

Screen Shot 2023-03-25 at 4 44 22 PM

Speculated that P is pulse, S is sense

@spuder
Copy link
Author

spuder commented Mar 30, 2023

The fan runs at a 20kHz frequency.
It starts at 87% duty cycle then slows to 50% duty cycle at idle)

@spuder
Copy link
Author

spuder commented Apr 8, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment