• Skip to main content
  • Skip to primary sidebar

Homemade Circuit Projects

Get free circuit help 24/7

Circuits for Beginners | Basic Circuits | LED Driver | Hobby Circuits | Transistor Circuits

New-Projects | Privacy Policy | About us | Contact | Disclaimer | Copyright | Videos

You are here: Home / Arduino Engineering Projects / Fading an LED ON/OFF – Arduino Basics

Fading an LED ON/OFF – Arduino Basics

Last Updated on June 20, 2019 by Swagatam

ask questions through comments

The post discusses a basic Arduino function where we learn the process of fading an LED ON/OFF through some basic code implementations.

Creating a Fading Effect

We see how to use an analogwrite() function in order to implement a fading ON/OFF of an LED. The function incorporates PWM pulses across a pinout for accomplishing the suggested fading action over the connected LED.

Hardware Needed

Along with your Arduino, other materials such as a breadboard, an LED and a 220 ohm, 1/4 wat resistor would be required for the experiment.

The Circuit

The steps involved for the proposed LED ON/OFF fading with an Arduino are as follows:

1. Attach the longer terminal of the LED to the digital output pin#9 via the 220 ohm resistor in series, while the cathode or the shorter terminal of the LED with the ground directly or the negative supply rail.

The Code

Once pin#9 of the board is integrated as the LED positive pin, the setup() function could be simply left alone and would'nt need any further operations.

The main loop code component in the form of analogwrite() needs a couple of acknowledgments: The first being addressing the function regarding which pin to be used for writing, and the second being the value of the PWM to be determined.

For initiating a fading ON/OFF effect on the connected LED, the PWM could be consistently varied from zero to maximum or to 255 and vice versa, completing the entire single cycle of operation.

The code below shows the PWM magnitude being determined through a variable named as brightness. Also the it increments by variable fadeAmount in the loop.

In a situation where brightness is at the extreme values (either 0 or 255), prompts fade

amount to become negative.

Meaning if suppose the fadeAmount is 5, it changes to -5, and 5 in case it's set a 55. In the later periods in the loop these changes results brightness to vary course of the action as well.

The function analoguewrite() causes quick alterations in the PWM values, such that the delay at the conclusion of the sketch controls the fading speed.
You may experiment with the delay values to investigate the changes produced in the program.
/*
Fade

This example shows how to fade an LED on pin 9
using the analogWrite() function.

This example code is in the public domain.
*/

int led = 9;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup()  {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop()  {
// set the brightness of pin 9:
analogWrite(led, brightness);

// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}

get free help for circuit diagrams

You'll also like:

  • 1.  Converting Analogue to Digital (Analogue Read Serial) – Arduino Basics
  • 2.  Password Security Lock Circuit Using 4×4 Keypad and Arduino
  • 3.  How to Make LED Air Pollution Meter Circuit with Arduino
  • 4.  Joystick Controlled 2.4 GHz RC Car Using Arduino
  • 5.  Wireless Servo Motor Control Using 2.4 GHz communication link
  • 6.  Simple Digital Water Flow Meter Circuit using Arduino

About Swagatam

I am an electronic engineer (dipIETE ), hobbyist, inventor, schematic/PCB designer, manufacturer. I am also the founder of the website: https://www.homemade-circuits.com/, where I love sharing my innovative circuit ideas and tutorials.
If you have any circuit related query, you may interact through comments, I'll be most happy to help!

Subscribe for the Latest Posts


 

Reader Interactions

Comments

  1. Blitz Tear says

    August 20, 2014 at 4:45 pm

    Good Day Mr. Majumdar. We were hoping if you could give us tips on how to build a 8×8 dot matrix with RGB leds. For our project. We have MPLABX IDE v1.85 for C language, PICkit2 as emulator. Looking to use PIC12Fxxx or PIC16Fxxx IC's. We wanna use only Red-Green-Blue colors to display desired characters. We really have nothing to start with. I mean, what IC do we use? what other components do we need? Hoping for your reply. Thank You in advance.

    • Swagatam says

      August 21, 2014 at 5:11 am

      Good day Blitz, frankly speaking, I am not expert with MCU circuits and stuffs, so I am as blank as you are right now, hopefully soon I'll get this field too in my area of expertise.

Primary Sidebar



Categories

  • 3-Phase Power (15)
  • 324 IC Circuits (19)
  • 4017 IC Circuits (52)
  • 4060 IC Circuits (25)
  • 555 IC Circuits (98)
  • 741 IC Circuits (19)
  • Amplifiers (59)
  • Arduino Engineering Projects (83)
  • Audio Projects (94)
  • Battery Chargers (83)
  • Car and Motorcycle (94)
  • Datasheets (46)
  • Decorative Lighting (Diwali, Christmas) (32)
  • DIY LED Projects (89)
  • Electronic Components (97)
  • Electronic Devices and Circuit Theory (35)
  • Electronics Tutorial (109)
  • Fish Aquarium (5)
  • Free Energy (34)
  • Fun Projects (12)
  • GSM Projects (9)
  • Health Related (19)
  • Heater Controllers (28)
  • Home Electrical Circuits (100)
  • How to Articles (20)
  • Incubator Related (6)
  • Industrial Electronics (28)
  • Infrared (IR) (40)
  • Inverter Circuits (98)
  • Laser Projects (12)
  • LM317/LM338 (21)
  • LM3915 IC (25)
  • Meters and Testers (64)
  • Mini Projects (156)
  • Motor Controller (66)
  • MPPT (7)
  • Oscillator Circuits (24)
  • PIR (Passive Infrared) (8)
  • Power Electronics (33)
  • Power Supply Circuits (74)
  • Radio Circuits (9)
  • Remote Control (47)
  • Security and Alarm (61)
  • Sensors and Detectors (118)
  • SG3525 IC (5)
  • Simple Circuits (74)
  • SMPS (29)
  • Solar Controllers (60)
  • Timer and Delay Relay (53)
  • TL494 IC (5)
  • Transformerless Power Supply (8)
  • Transmitter Circuits (40)
  • Ultrasonic Projects (14)
  • Water Level Controller (45)


Circuit Calculators

  • AWG to Millimeter Converter
  • Battery Back up Time Calculator
  • Capacitance Reactance Calculator
  • IC 555 Astable Calculator
  • IC 555 Monostable Calculator
  • Inductance Calculator
  • LC Resonance Calculator
  • LM317, LM338, LM396 Calculator
  • Ohm’s Law Calculator
  • Phase Angle Phase Shift Calculator
  • Power Factor (PF) Calculator
  • Reactance Calculator
  • Small Signal Transistor(BJT) and Diode Quick Datasheet
  • Transistor Astable Calculator
  • Transistor base Resistor Calculator
  • Voltage Divider Calculator
  • Wire Current Calculator
  • Zener Diode Calculator


You can also Chat with me here:

Facebook
Twitter
YouTube
Instagram
My Facebook-Page
Quora



© 2022 · Swagatam Innovations

We use cookies on our website to give you the best experience.
Cookie settingsAccept All
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Please visit the Privacy Policy Page for more info.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT