1

I am trying to vary the speed of a 2-wire fan by using a Arduino Uno and only a potentiometer.

Initially, I assumed that I could go about doing so by using a code that I used for dimming an LED. When I have run the code and varied the potentiometer, the LED on the power supply changed but, the fan did not come on. I assume that this was due to an error in the first code.

In the second code, the fan also did not come on. I am unsure if it was my wiring. I have an voltage supply connected to a breadboard to power the fan.

Additionally, would I require a mosfet or transistor despite using a potentiometer? This could have also be the problem regarding my second attempt.

First Code Attempt:

int potPin=A2;
int gPin=9;
int potVal;
float LEDVal;
int dt=250;
void setup() {
  // put your setup code here, to run once:
pinMode(potPin, INPUT);
pinMode(gPin, OUTPUT);
Serial.begin(9600);
}

void loop() { // put your main code here, to run repeatedly: potVal=analogRead(potPin); LEDVal=(255./1023.)*potVal; analogWrite(gPin,LEDVal); Serial.println(LEDVal); delay(dt); }

Second Code:

int reading;
int value;

void setup() { pinMode(CONTROL, OUTPUT); }

void loop() { reading = analogRead(POTENTIOMETER); value = map(reading, 0, 1024, 0, 255);
analogWrite(CONTROL, value); }```

AI09
  • 13
  • 1
  • 3

1 Answers1

1

Like people have already mentioned in the comments, you likely need to add a FET to drive the fan. The Arduino simply can't put provide the current required to drive the fan. According to the Arduino documentation, the microcontroller can only provide 20mA per output pin, which is enough to drive LEDs but not fan motors.

Luckily, there is a tutorial on the Arduino website for driving motors with PWM through a FET.
https://docs.arduino.cc/learn/electronics/transistor-motor-control

The following program is adapted from the "Analog In, Out Serial" built-in example and should be sufficient to control the speed of the motor.

// These constants won't change. They're used to give names to the pins used:

const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to

const int analogOutPin = 9; // Analog output pin that the FET is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {

// declare the ledPin as an OUTPUT:
pinMode(analogOutPin, OUTPUT);
}

void loop() {

// read the analog in value:
sensorValue = analogRead(analogInPin);

// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);

// change the analog out value:
analogWrite(analogOutPin, outputValue);

// wait 2 milliseconds before the next loop for the analog-to-digital
// converter to settle after the last reading:
delay(2);
}

This program has not been tested and is intended as an example. Make sure that the output for the FET is wired to a pin that can output PWM. Then make sure that your program is referencing the correct pin.

Make sure that your power supply has enough power to control the fan motor as well.

Here are some wiring diagrams from the tutorial webpage.

Transistor Motor Control Diagram Transistor Motor Schematic

Note: The particular fan that you are using might not be able to be driven by PWM and you should seek out and read the relevant documentation for the fan.

sa_leinad
  • 3,218
  • 2
  • 23
  • 51