Table of Contents
Introduction
In this project, we're going to use the light sensor module included with the TINKERkit to control the brightness of the eight channel RGB LED strip using two different approaches:
- Set the number of ON LEDs as the measured light changes
- Set the brightness of all the LEDs as the measured light changes
Required Parts
The following parts are required for this project:
- A TINKERplate attached to a preprogrammed Raspberry Pi
- From a TINKERkit
- Three male to female jumper wires
- the light sensor module
- the mini breadboard
- the 74HCT125 IC chip
- the 8 channel RGB LED module
- seven male to male jumper wires
Steps
Assembly
Using the parts called out above, assemble the night light electronics as shown below. Use the three male to female wires to connect the light sensor to the Analog Input block on the left, and the seven male to male wires for the remaining connections to the IC and REG LED strip on the bottom part of the diagram.
Theory of Operation
The light sensor module consists of a light sensitive resistor in series with fixed resistor. When connected to the TINKERplate +5V and ground pins, the circuit looks like:
In the schematic above, R1 and FTR1 form a simple resistive divider with the voltage at the SIG output being equal to +5V*FTRI/(FTR1+R1). As the light intensity increases on the photoresistor, its resistance decreases which will result in a lower voltage reaching the SIG pin and the analog input.
Code 1
This code measures the voltage from the light sensor ten times per second, compares it to a high and low limit, then converts it to a value in the range of 0 to 1. This value is then used to scale the brightness of all of the RGB LEDs equally.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import piplates.TINKERplate as TINK import time string0 = [0xFF,0xFF,0xFF] #Create a single full white LED pattern TINK.setDEFAULTS(0) #set all Digital I/O ports to their default states TINK.setMODE(0,1,'rgbled') #set the mode of Digital I/O port 1 to RGB LED while(True): #start repeating loop ain=TINK.getADC(0,3) #read the voltage on Analog Input channel 3 k=(ain-0.3)/3.7 #subtract a 0.3V offset and scale result if(k<0): k=0 #clip lower limit to 0 if(k>1): k=1 #clip upper limit to 1 #print(ain) #debug statement - uncomment to use string2=[(x * k) // 1 for x in string0] #scale the values in string0 from 0 through 255 #print (string2) #debug statement - uncomment to use #create the full, 8 channel RGB LED string: string1=string2+string2+string2+string2+string2+string2+string2+string2 TINK.setRGBSTRING(0,1,string1) #send eight LED values to port 1 time.sleep(0.1) #sleep 100msec before repeating |
As you cover the sensor with your hand, the LED strip will gradually go from off to full white. You may also notice that when the LED strip gets very dim, only the green LEDs turn on - this is not a bug but the result of some round-off errors in the TINKERplate firmware. Note that the sensor and the LED strip should be optically isolated from each other - otherwise you'll create a feedback situtaion which will cause the LEDs to flash off and on.
Code 2
This version of the code controls the brighness of the LED strip by increasing the number of ON LEDs as the room gets darker.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import piplates.TINKERplate as TINK import time stringW = [0xFF,0xFF,0xFF] #Create a single full white LED pattern stringB = [0x0,0x0,0x0] #Create a single full off LED pattern TINK.setDEFAULTS(0) #set all Digital I/O ports to their default states TINK.setMODE(0,1,'rgbled') #set the mode of Digital I/O port 1 to RGB LED while(True): #start repeating loop ain=TINK.getADC(0,3) #read the voltage on Analog Input channel 3 k=(ain-0.3)/3.7 #subtract a 0.3V offset and scale result if(k<0): k=0 #clip lower limit to 0 if(k>1): k=1 #clip upper limit to 1 j=int(k*8.0+0.5) nlString=[] #Create empty list for i in range(j): nlString=nlString+stringW #populate 1st part of list with white LEDs for i in range(8-j): nlString=nlString+stringB #populate rest of list with OFF LEDs #print(j,nlString) #debug statement - uncomment to use #print(ain) #debug statement - uncomment to use TINK.setRGBSTRING(0,1,nlString) #send eight LED values to port 1 time.sleep(0.1) #sleep 100msec before repeating |
The above programs can be downloaded from github by typing the following instructions on the command line:
- git clone https://github.com/pi-plates/TINKERplate-Projects.git
- cd TINKERplate-Projects
- python3 NIGHTlight1.py or python3 NIGHTlight2.py