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 value;

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

void loop() {
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

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.

• Stanley Brown

In this video on hooking up the arduino potentiometer, it is mentioned that you don’t care which side is hooked up to 5v and gnd. Is that a correct statement?

• Finos

Can i use instead of a pot a thermistor ;

• chopy61

hello what is the diode on the fan for

Dear Sir

I failed to understand Arduino block which you have shown in the sketch is it a component itself or that block has Nos of components in it and that complete block is known as arduino will you please explain.

• Popescu Marian

That rectangular shape is the Arduino Uno Board, and those (5V, A0, GND, 9) are the Arduino’s pins where you need to connect the external components: potentiometer, resistor, transistor.

Looking for the latest from TI?