Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
cstram
Published © GPL3+

Drive an Old PC Fan with Arduino

Do you want to drive an old PC fan for your Arduino projects? Follow this small tutorial and you will know how!

BeginnerFull instructions provided2,177
Drive an Old PC Fan with Arduino

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
12V Generic Power Supply
×1
Recycled PC FAN
×1
Rotary Encoder with Push-Button
Rotary Encoder with Push-Button
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
IC2 OLED Display
×1

Story

Read more

Schematics

Arduino PC FAN control

It's a Fritzing breadboard schematic

Code

Arduino PC FAN Control

Arduino
Software that allows to control the speed of an old PC FAN with a Rotary Encoder.
#include <RotaryEncoder.h>
#include <U8glib.h>

// ----- Rotary settings here ----
#define ROTARYSTEPS 1
#define ROTARYMIN 80
#define ROTARYMAX 255

#define FANPIN 3

// Setup a RoraryEncoder for pins A2 and A3:
RotaryEncoder encoder(A2, A3);

U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE);  // I2C / TWI

int pos=0;
char Potenza[5];

 
void setup() {
  // put your setup code here, to run once:
  
  Serial.begin(115200);
  Serial.println("Fan test with Encoder:");

  encoder.setPosition(0 / ROTARYSTEPS); // start with the value of 0
  analogWrite(FANPIN, 0);
  u8g.setColorIndex(1);         // pixel on for Display 


}

void loop() {
  // put your main code here, to run repeatedly:

 encoder.tick();

  int newPos = encoder.getPosition();
  if (pos != newPos) {

    if (newPos < ROTARYMIN) {
    encoder.setPosition(ROTARYMIN / ROTARYSTEPS);
    newPos = ROTARYMIN;
    } else if (newPos > ROTARYMAX) {
      encoder.setPosition(ROTARYMAX / ROTARYSTEPS);
      newPos = ROTARYMAX;
    }
    sprintf(Potenza, "%d", newPos);
    u8g.firstPage();  
    do {
      draw("Power:", 0, 12);
      draw(Potenza, 90, 12);
    } while( u8g.nextPage() );
    
   analogWrite(FANPIN, newPos);
   Serial.println(newPos);
   pos=newPos;  
 }
}


void draw(char* parola, int posx, int posy) {
  // graphic commands to redraw the complete screen should be placed here  
  u8g.setFont(u8g_font_unifont);
  //u8g.setFont(u8g_font_osb21);
  u8g.drawStr( posx, posy, parola);
}

Credits

cstram
16 projects • 21 followers
Passionate about IT, Electronics and DIY. Strong believer in Raspberry and Arduino devices. Experience in digital television and security.

Comments