Lesson 2 LED Flowing Lights

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

Introduction

In this lesson, we will learn how to make eight LEDs blink in various effects as you want based on Raspberry Pi.

Components

– 1*Raspberry Pi

– 1*Breadboard

– 1*Network cable (or USB wireless network adapter)

– 8*LED

– 8*Resistor (220Ω)

– Jumper wires

Experimental Principle

Set GPIO0 to GPIO7 to low in turn by programming, then LED0 to LED7 will light up in turn. You can make eight LEDs blink in different effects by controlling their delay time and the order of lighting up.

Experimental Procedures

Step 1Connect the circuit as shown in the following diagram

Step 2Edit and save the code with vim (see path/Rpi_BasicKit /02_8led/8Led.c)

Step 3Compile the code

gcc  8Led.c  -lwiringPi


Step 4
Run the program

./a.out

Now, when you press Enter, eight LEDs will light up circularly and render different effects.

sz

Further Exploration

We can write the blinking effects of LEDs in an array. If you want to use one of these effects, you can call it in the main() function directly.

C Code

#include <wiringPi.h>
#include <stdio.h>#define LedPin 0int main(void)
{
if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen
printf(“setup wiringPi failed !”);
return 1;
}
printf(“linker LedPin : GPIO %d(wiringPi pin)\n”,LedPin); //when initialize wiring successfully,print message to screen

pinMode(LedPin, OUTPUT);while(1){
digitalWrite(LedPin, LOW); //led on
delay(500);
digitalWrite(LedPin, HIGH); //led off
delay(500);
}return 0;
}


Python Code

#!/usr/bin/env python import RPi.GPIO as GPIO import time pins = [11, 12, 13, 15, 16, 18, 22, 7] def setup(): GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location for pin in pins: GPIO.setup(pin, GPIO.OUT) # Set all pins’ mode is output GPIO.output(pin, GPIO.HIGH) # Set all pins to high(+3.3V) to off led def loop(): while True: for pin in pins: GPIO.output(pin, GPIO.LOW) time.sleep(0.5) GPIO.output(pin, GPIO.HIGH) def destroy(): for pin in pins: GPIO.output(pin, GPIO.HIGH) # turn off all leds GPIO.cleanup() # Release resource if __name__ == ‘__main__’: # Program start from here setup() try: loop() except KeyboardInterrupt: # When ‘Ctrl+C’ is pressed, the child program destroy() will be executed. destroy()