Advertisement
arduino potentiometer controller schematic

The LED Brightness or Fan Speed with Arduino – Tutorial #7

This is an easy tutorial showing how you can control the brightness of an LED or adjust the speed of a DC fan using Arduino Board, a small BC547 transistor and a potentiometer. In my example the whole circuitry is powered from a 9V battery and I use a small 12V fan, but you should use a 9V or 6V.

If you want to use higher currents then replace the BC547 (100mA maximum collector current) with a BD139 or other according to you requirements. Dot not forget that voltage ratings for the Arduino Board are between 7V and 12V.

Code example

const byte POTENTIOMETER = 0;
const byte CONTROL = 9;
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 explanation

We set the potentiometer pin as 0 (A0 on the Arduino Uno board), the CONTROL variable is the voltage that is used to cotrol the led brightness of the speed of the fan. In the setup() function the CONTROL pin 9 is set as OUTPUT. In the loop() we are getting the analog reading value of the voltage that is applied on pin A0 or 0.

Then use the map() function to translate the value 1024 to 255 and store it in the value variable. After that Arduino writes this value to the CONTROL pin, resulting in a variable voltage that is dependent on the pontetiometer value (its voltage on the adjustable pin, in this case it is connected as a voltage divider *see the schematic).

The Schematic

arduino potentiometer controller schematic

Do not power the circuit and the Arduino Board from the USB cable because the USB port cannot output more than 100mA, that is why you need an auxiliary power supply with voltage between 7 and 12V.

5 Comments

Join the conversation!

Error! Please fill all fields.
Looking for the latest from TI?