If you have a Raspberry Pi, a photoresistor, and a common cathode RGB LED, and some wires, and a 330 ohm resistor, and a potentiometer and you want to make a cool electronics project, take a look at this.
Note: This project only identifies red, green, blue, purple, yellow (green and red (in light)), and turquoise because the Raspberry Pi can't vary how bright the LED is (not very well, at least).
The circuit is:
Hook up common pin of RGB LED (through 330 ohm resistor) to ground.
Hook up red pin of RGB LED to GPIO 27; hook up blue to GPIO 22, and green to GPIO 4.
Hook up photoresistor (squiggly part) like this:
Hook up common pin of RGB LED (through 330 ohm resistor) to ground.
Hook up red pin of RGB LED to GPIO 27; hook up blue to GPIO 22, and green to GPIO 4.
Hook up photoresistor (squiggly part) like this:
Code:
import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
red = 27
blue = 22
green = 4
sensor = 23
gpio.setup(red, gpio.OUT)
gpio.setup(green, gpio.OUT)
gpio.setup(blue, gpio.OUT)
gpio.setup(sensor, gpio.IN)
h = True
import time
def input2(pin):
w = gpio.input(pin)
if w == True:
return 0
else:
return 1
while h == True:
print("Keep object on until you are told to take it off.")
gpio.output(blue, 1)
time.sleep(0.5)
bluesense = input2(sensor)
gpio.output(blue, 0)
time.sleep(0.5)
gpio.output(green, 1)
time.sleep(0.5)
greensense = input2(sensor)
gpio.output(green, 0)
time.sleep(0.5)
gpio.output(red, 1)
time.sleep(0.5)
redsense = input2(sensor)
gpio.output(red, 0)
print("You can take object off now.")
if bluesense == 0:
print("Object is blue")
h = False
if greensense == 0:
print("Object is green")
h = False
if redsense == 0:
print("Object is red")
h = False
import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
red = 27
blue = 22
green = 4
sensor = 23
gpio.setup(red, gpio.OUT)
gpio.setup(green, gpio.OUT)
gpio.setup(blue, gpio.OUT)
gpio.setup(sensor, gpio.IN)
h = True
import time
def input2(pin):
w = gpio.input(pin)
if w == True:
return 0
else:
return 1
while h == True:
print("Keep object on until you are told to take it off.")
gpio.output(blue, 1)
time.sleep(0.5)
bluesense = input2(sensor)
gpio.output(blue, 0)
time.sleep(0.5)
gpio.output(green, 1)
time.sleep(0.5)
greensense = input2(sensor)
gpio.output(green, 0)
time.sleep(0.5)
gpio.output(red, 1)
time.sleep(0.5)
redsense = input2(sensor)
gpio.output(red, 0)
print("You can take object off now.")
if bluesense == 0:
print("Object is blue")
h = False
if greensense == 0:
print("Object is green")
h = False
if redsense == 0:
print("Object is red")
h = False
How it works:
In the code, the Pi reads which color reflects most, and then that is the color. (Colors reflect only themselves, so blue reflects only blue.)
Cool fact:
Every color (in light) is everything except itself. Like, red is everything except red (green and blue because green, blue, and red are the primary colors of light.
In the code, the Pi reads which color reflects most, and then that is the color. (Colors reflect only themselves, so blue reflects only blue.)
Cool fact:
Every color (in light) is everything except itself. Like, red is everything except red (green and blue because green, blue, and red are the primary colors of light.
Note: If you type in each line of code in IDLE or IDLE3 (Python), it will automatically fix the indentation.