Lesson 8 Flowing LED Lights

Share for us
Share on facebook
Share on twitter
Share on pinterest
Share on whatsapp

Introduction

In this lesson, we will conduct a simple yet interesting experiment – using LEDs to create flowing LED lights. As the name suggests, these eight LEDs in a row successively light up and dim one after another, just like flowing water.

Components

– 1 * SunFounder Uno board

– 1 * Breadboard

– 8 * LED

– 8 * Resistor (220Ω)

– 1 * USB cable

– Jumper wires

Principle

The schematic diagram

PrincipleThe principle of this experiment is simply to turn on eight LEDs in turn. The eight LEDs are connected to pin 2-pin 9 respectively. Set them as High level and the corresponding LED at the pins will light up. Control the time of each LED brightening and you will see flowing LED lights.

Experimental Procedures

Step 1: Build the circuit

Step 2: Open the code file

Step 3: Select correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Now, you should see eight LEDs brighten one by one from the LED connected to pin 2 to that to pin 9, and then dim in turn from the LED at pin 9 to the one at pin 2. After that, the LEDs will light up from the LED at pin 9 to that at pin 2 and dim from the LED at pin 2 to that at pin 9. This whole process will repeat until the circuit is power off.

Code

//Flowing LED Lights /* Eight LEDs will light up one by one from left to right, and then go out one by one from right to left. After that, the LEDs will light up one by one from right to left, and then go out one by one from left to right. This process will repeat indefinitely.*/ //Email:support@sunfounder.com //Website:www.sunfounder.com //2015.5.7 /**************************************/ void setup() { //set pins 2 through 9 as output for(int i = 2;i <= 9;i++) { pinMode(i,OUTPUT); //initialize a as an output } } /****************************************/ void loop() { //iterate over the pins //turn the led on from lowest to the highest for(int a = 2;a <= 9;a++) { digitalWrite(a,HIGH);//turn this led on delay(100);//wait for 100 ms } //turn the led off from pin9 to pin2 for(int a = 9;a>=2;a–) { digitalWrite(a,LOW);//turn this led off delay(100);//wait for 100 ms } //turn the led off from pin2 to pin9 for(int a = 9;a>=2;a–) { digitalWrite(a,HIGH);//turn this led on delay(100);//wait for 100 ms } for(int a = 2;a <= 9;a++) { digitalWrite(a,LOW);//turn this led off delay(100);//wait for 100 ms } }