user87034: 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);
}```


user19409: 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.


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.

87034: <p>I am trying to vary the speed of a 2-wire fan by using a Arduino Uno and only a potentiometer.</p>
<p>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.</p>
<p>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.</p>
<p>Additionally, would I require a mosfet or transistor despite using a potentiometer? This could have also be the problem regarding my second attempt.</p>
<p><strong>First Code Attempt:</strong></p>
<pre><code>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);
}
</code></pre>
<p><strong>Second Code:</strong></p>
<pre><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);
}```
</code></pre>
 19409: <p>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.</p>
<p>Luckily, there is a tutorial on the Arduino website for driving motors with PWM through a FET.<br />
<a href="https://docs.arduino.cc/learn/electronics/transistor-motor-control" rel="nofollow noreferrer">https://docs.arduino.cc/learn/electronics/transistor-motor-control</a></p>
<p>The following program is adapted from the <a href="https://docs.arduino.cc/built-in-examples/analog/AnalogInOutSerial" rel="nofollow noreferrer">&quot;Analog In, Out Serial&quot; built-in example</a> and should be sufficient to control the speed of the motor.</p>

<pre class="lang-C++ prettyprint-override"><code>// 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);
}
</code></pre>
<p>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.</p>
<p>Make sure that your power supply has enough power to control the fan motor as well.</p>
<p>Here are some wiring diagrams from the tutorial webpage.</p>
<p><a href="https://i.stack.imgur.com/tve31.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tve31.png" alt="Transistor Motor Control Diagram" /></a>
<a href="https://i.stack.imgur.com/3WuXI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3WuXI.png" alt="Transistor Motor Schematic" /></a></p>
<p>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.</p>
