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);
}```
would I require a mosfet or transistor despite using a potentiometer?
... what do you think that a potentiometer does?