Raspberry PI 3+ python blinking LED
I've recently been getting into the raspberry PI world. I have a model 3+ that one of my first projects was to get an LED to blink. The following below will make an LED blink for a random amount of time for a random amount of times.
import RPi.GPIO as GPIO
import time
import random
from random import *
# set the output mode of the GPIO pins
GPIO.setmode(GPIO.BCM)
# don't show warnings in the console
GPIO.setwarnings(False)
# this sets the output to pin 18 (positive)
GPIO.setup(18,GPIO.OUT)
# we randomly pick some numbers
numberOfBlinks = randint(10,30)
blinkLength = randint(1,5)
print(numberOfBlinks,blinkLength)
print("LEDs on")
counter = 0
# loop through and turn the LED on and off
while counter <= numberOfBlinks:
# set the output to high (ON)
GPIO.output(18,GPIO.HIGH)
# wait for X seconds
time.sleep(blinkLength)
# set the output to low (OFF)
GPIO.output(18,GPIO.LOW)
counter = counter + 1
print(counter)
print("LEDs off")
Comments