Introduction: Brightness Control, Arduino (with Animations)
Over the last few years I have built two pinball machines ( pinballdesign.com) and two robot heads (grahamasker.com) each controlled by Arduinos. Having had a career as a mechanical engineer I am fine with the design of the mechanisms, however I struggle with the programming. I decided to create animations to illustrate some of the basic concepts of the Arduino. I thought it would help me and others to understand them. A picture is worth a thousand words and an animation can be a thousand pictures!
So here is an animated explanation on the subject of Brightness Control. The animation above shows a schematic of a potentiometer connected to an Arduino. It shows how adjusting the position of the potentiometer can alter the brightness of a led. I am going to explain all of the elements of this process. For anyone not familiar with potentiometers and leds, I will start with those. I will then explain why the led has to be connected to a PWM enabled Arduino pin and how the MAP function is used within an Arduino sketch to convert the input from the potentiometer to an output which is suitable for controlling an led.
If you are familiar with leds and potentiometers then you can skip sections 1 and 2.
Step 1: ABOUT LEDs
The left illustration above shows the circuit symbol for an led and the polarity of the led legs. Current will only flow through an LED in one direction so polarity is important. The longer leg is the positive. Also there is a flat side to the flange, this is the negative side.
VOLTAGE and CURRENT
The voltage required by an LED ranges from about 2.2v to 3.2 volts depending upon its colour. Their current rating is typically 20mA. In order to restrict the current and prevent the LED from overheating, it is necessary to use a resistor in series with each LED. I recommend about 300 ohms.
The illustration on the right above shows a way of soldering a resistor to a leg of an led and insulating it with heat shrink sleeving.
Step 2: POTENTIOMETER
In Arduino terms a potentiometer is a sensor. “Sensor” refers to any any external device which when connected to input pins can be sensed by the Arduino. We are going to use a potentiometer connected to the Arduino, to control the brightness of an LED. A potentiometer is sometimes called a voltage divider, which I think is a better description. The diagram on the left above indicates the principal of a voltage divider. In this example, a resistor is connected to ground at one end and held, by some power source to 5v at the other end. If a slider is moved along the resistor it will be at a voltage of 0v at the left hand end, 5v at the right hand end. In any other position it will be at a value between 0v and 5v. At half way, for example it will be at 2.5V. If we reshape the arrangement as shown on the right above, then this represents the action of a rotating potentiometer.
Step 3: THE CIRCUIT
The illustration above shows how we need to connect the potentiometer and the led to an Arduino.
The Ardunio needs to sense the voltage being fed to it by the potentiometer. The voltage alters smoothly as the potentiometer is turned, it is thus an analogue signal and hence needs to be connected to an analogue input pin on the Arduino. The voltage on this pin will be read by the Arduino each time the program requests it via function “analogRead”.
The Arduino only has digital output pins. However those pins with a tilde ( ~ ) beside them simulate an analogue output that is suitable to control the brightness of a Led. This process is called Pulse Width Modulation (PWM) and is explained via the next animation, Step 4.
Step 4: PWM
PWM, Pulse Width Modulation
As previously mentioned, the pins with a tilda, “~“ beside them are PWM pins. Because the pins are digital they can only be at 0v or 5v, however with PWM they can be used to dim an LED or control the speed of a motor. They do so by supplying 5v to an LED but pulsing it between 0v and 5v at 500 Hz (500 times per second) and stretching or shrinking the duration of each 0v and 5v element of the pulse. As the LED sees a longer 5v pulse than a 0v pulse then it becomes brighter. In our program, we use the function analogueWrite() to output a PWM “square wave”. It has 256 increments, Zero giving a 0% duty cycle and 255 giving 100% “duty cycle” i.e. continuous 5 volts. Thus 127 would gives a 50% duty cycle, half the time at 0v and half the time at 5v. The animation above shows how as this duty cycle is stretched towards 100% then the led becomes brighter.
Step 5: THE PROGRAM (ARDUINO SKETCH)
The above video steps through a a program (sketch) that can be used to control the brightness of a led by using a potentiometer. The circuit is the same as shown at step 3.
If you find this video to fast (or slow) to read comfortably then you can adjust its speed At the right hand end of the lower control bar is a symbol shaped like a gear wheel (some times with a red 'HD' label on it.) If clicked it will bring up a menu which includes "playback speed".
It would, of course, be better if you could could click a button to step through each line of the program at your own speed, however unfortunately it is not possible to provide that interactive method here. If you would prefer to use that method on this subject and many other Arduino topics then there is free preview version of an interactive/animated ebook available at animatedarduino.com
There is one feature in the program that I think needs more explanation: on line14 the "map" function is used. There is an explanation on its purpose next, in step 6
Step 6: MAP
We have the potentiometer connected to an analogue pin. The potentiometer voltage varies between 0v and 5v. This range is registered in the processor in 1024 increments. When the value input is used to create an output via a PWM enabled digital pin this range has to be mapped to the output range of a digital pin. This has 255 increments. The map function is used for this purpose and provides an output which is proportional to the input.
The video above illustrates this.
Step 7: Animated Arduino
The images in this Instructable have been taken from my ebook Animated Arduino which is available at www.animatedarduino.com in which I aim to provide a better understanding of some of the concepts encountered whilst learning to program the Arduino.
There is a free preview copy of the ebook available on the website which allows you to experience the interactive nature of the book. It is a basically a collection of sample pages and thus omits a lot of the explanation. It includes sample pages that allow you to click buttons that step you through each line of a pro-gram and view related comments. Other pages have video animations and audio content that you can control. A contents page is included in order that you can see what the complete edition contains.