Analog Output through PWM | How to control brightness of a LED?

Introduction

PWM or Pulse Width Modulation is a technique for getting an analog output using digital signals.

A digital signal, in general, can have either of the two values: HIGH/ON or LOW/OFF. If we switch the signal between these two values at an extremely fast rate, say, 500 times in 1 second, the signal at the output will appear to be continuous; it will seem as if it is an analog signal. 

The duration of on-time, i.e. the time during which the signal is HIGH is called the pulse width. To get varying analog values, you can modulate, i.e. change that pulse width. If you repeat this on-off pattern fast enough, the result is a steady voltage between 0 and 5V.

For example, if you need to have these types of analog voltage output, then these will be the graphs of output signal:

  • 0% Duty Cycle | analogWrite(0)
  • 25% Duty Cycle | analogWrite(64)
  • 50% Duty Cycle | analogWrite(127)
  • 75% Duty Cycle | analogWrite(191)
  • 100% Duty Cycle | analogWrite(255)

evive and Arduino generate analog output in PWM form. Pin 13 is internally connected to the pin 13 LED which a PWM Pin.

analogWrite()

Generally, Arduino’s PWM frequency is about 500Hz. In Arduino IDE, we use PWM concept through analogWrite() function. We give a value ranging on a scale of 0 – 255, such that analogWrite(255) requests a 100% duty cycle (always ON), and analogWrite(127) is a 50% duty cycle (ON for one half the time).

Syntax:

analogWrite(pin, value)

where the pin is the PWM pin and value is the duty cycle between 0% (always OFF or 0) and 100% (always ON or 255)

Example: LED Circuit

In the previous topic, you have learned about LED and its property. Also, you have programmed the inbuilt LED connected to digital pin 13. Now with the concept of analog write, we will control the LED brightness connected to digital pin 3 (We chose this pin as it is a PWM pin).

Let’s get started. 

Components Required

You will require a few extra components for this activity which are:

  • A resistor with a value of 220 Ohm to 10 kOhm.
    Resistors
  • A LED of any color
    LED Red White Yellow Green
  • A breadboard for making the LED Circuit.
    Breadboard 170 Pins ATL

    evive Notes Icon
    Note: If you are using evive, you can use the inbuilt breadboard

Circuit for the LED – Arduino

LED Circuit Arduino

 

Circuit for the LED – evive

evive LED Circuit (2)

 

What we will do now?

In this example, we will control the brightness of the LED. First, the LED will get brighter until it reaches the maximum brightness and then it will get dimmer until it is OFF. We will repeat this process.  If we control the voltage supply to the LED, we can control the current flowing through it, and as a result its brightness.

Below is the Arduino sketch:

Arduino LED Fading