craig richardson
Published

Automatic Stair Lights

LED lights on the stairs that light the way to go.

IntermediateShowcase (no instructions)6,205
Automatic Stair Lights

Things used in this project

Hardware components

Power led
×11
NPN Transistor
×11
Crimp housing
×22
Stripboard
×1
DC power socket
×1
100 ohm resistor
×11
Light dependant resistor
×1
Capacitor 4.7uF
×1
Black pre crimped wires
×12
Blue pre crimped wires
×12
2200uF capacitor
×11
Carbon pot (optional)
×1
Power supply
×1
12 way crimp housing
×2
Double sided header
×1
Solderless header (optional)
×1
Raspberry Pi zero
×1
PIR sensor
×2
Quadrant moulding
×2
Red LED
×1
330 ohm resistor
×1

Story

Read more

Code

stairled.py

Python
The code for monitoring the stairs and activating the LEDs
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# GPIO pin defines
LightDetector 	= 2
infoLed		= 4
irDetBot	= 14
irDetTop	= 15
stairLeds	= [18,23,24,25,8,7,1,12,16,20,21]
ledActives	= [0,0,0,0,0,0,0,0,0,0,0]

# global variables
topTriggered	= 0
botTriggered	= 0

# set the led that tells us the system is active
GPIO.setup(infoLed, GPIO.OUT)
GPIO.output(infoLed, GPIO.LOW)

# set the ir detector pins to inputs
GPIO.setup(irDetBot, GPIO.IN)
GPIO.setup(irDetTop, GPIO.IN)

# set stair led pins to output and clear them 
for i in range(0, 11):
	GPIO.setup(stairLeds[i], GPIO.OUT)
	GPIO.output(stairLeds[i], GPIO.LOW)

# check the light detector readings
def rc_time (LightDetector):
	count = 0

	GPIO.setup(LightDetector, GPIO.OUT)
	GPIO.output(LightDetector, GPIO.LOW)
	time.sleep(0.1)

	GPIO.setup(LightDetector, GPIO.IN)
	
	while(GPIO.input(LightDetector) == GPIO.LOW):
		count += 1

	GPIO.setup(LightDetector, GPIO.OUT)

	return count

# do the check for anyone going up or down the stairs
def checkStairs():
	global topTriggered
	global botTriggered
	global ledActives

	if GPIO.input( irDetTop ) and topTriggered == 0:
		topTriggered = 1

	if GPIO.input( irDetBot ) and botTriggered == 0:
		botTriggered = 1

	checkChange = 0
	if botTriggered or topTriggered:
		checkChange = 1
	# if someone is at the top then start incrementing the top var this goes from 10 to 0
	if topTriggered:
		if topTriggered < 12:
			ledActives[11-topTriggered] += 1
		else:
			# topTriggered starts off here at 12 so 22 - 12 = 10 as this also goes 10 to 0

			if ledActives[22-topTriggered]:
				ledActives[22-topTriggered] -= 1
		topTriggered = topTriggered + 1
		# once 24 is reached we are done so go bak to zero
		if topTriggered > 22:
			topTriggered = 0
	# if someone at the bottom start incrementing the bottom var this goes 0 to 10
	if botTriggered:
		if botTriggered < 12:
			ledActives[botTriggered-1] += 1
		else:
			if ledActives[botTriggered-12]:
				ledActives[botTriggered-12] -= 1
		botTriggered = botTriggered + 1
		if botTriggered > 22:
			botTriggered = 0

	if checkChange:
		activeCount = 0
		for i in range(0,11):
			if ledActives[i] > 0:
				GPIO.output(stairLeds[i], GPIO.HIGH)
				activeCount += 1
			else:
				GPIO.output(stairLeds[i], GPIO.LOW)

		if activeCount == 11:
			if botTriggered and topTriggered:
				for k in range(0,11):
					ledActives[k] = 1
				topTriggered = 12
				botTriggered = 12
				time.sleep(15)
			else:
				time.sleep(15)
	return

def testLeds():
	count = 0
	while count < 11:
		GPIO.output(stairLeds[count], GPIO.HIGH)
		time.sleep(0.6)
		count = count + 1

	count = 0
	while count < 11:
		GPIO.output(stairLeds[count], GPIO.LOW)
		time.sleep(0.6)
		count = count + 1

	return

lightVal = [0,0,0,0,0]
active = 0
average = 0
count = 0
while True:
	if botTriggered == 0 and topTriggered == 0:
		lightVal[count] = rc_time(LightDetector)
		print(count)

		count = count + 1
		if count >= 5:
			count = 0

		for i in range(0,5):
			average = average + lightVal[i]
		average = average / 5
		if average > 550:
			GPIO.output(infoLed, GPIO.HIGH)
			active = 1
		else:
			GPIO.output(infoLed, GPIO.LOW)
			active = 0

#	print(average)
	if active:
		checkStairs()

	if botTriggered == 0 and topTriggered ==0:
		time.sleep(1)
	else:
		time.sleep(0.25)
#	testLeds()

GPIO.cleanup()

Credits

craig richardson

craig richardson

3 projects • 2 followers

Comments