0% found this document useful (0 votes)
38 views6 pages

Circuit 1C: RGB: Parts Needed

This circuit uses an RGB LED and a photoresistor to create a night light. The photoresistor detects the light level and turns on the RGB LED when it is dark. A potentiometer is used to cycle through different colors on the RGB LED. The Arduino code uses analogWrite() to pulse the RGB LED pins at different values, blending the LED colors to produce different hues.

Uploaded by

Darwin Vargas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views6 pages

Circuit 1C: RGB: Parts Needed

This circuit uses an RGB LED and a photoresistor to create a night light. The photoresistor detects the light level and turns on the RGB LED when it is dark. A potentiometer is used to cycle through different colors on the RGB LED. The Arduino code uses analogWrite() to pulse the RGB LED pins at different values, blending the LED colors to produce different hues.

Uploaded by

Darwin Vargas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Circuit 1C: RGB

In circuit 1B, you got to use a potentiometer, which varies resistance based on the twisting of a knob.
In this circuit, you’ll be using a photoresistor, which changes resistance based on how much light the
Page | 1
sensor receives. Using this sensor you can make a simple night-light that turns on when the room
gets dark and turns off when it is bright.

Parts Needed
Grab the following quantities of each part listed to build this circuit:

NEW COMPONENTS

RGB LED: An RGB LED is actually three small LEDs — one red, one green and one blue — inside a
normal LED housing. This RGB LED has all the internal LEDs share the same ground wire, so there are
four legs in total. To turn on one color, ensure ground is connected, then power one of the legs just as
you would a regular LED. Don’t forget the current-limiting resistors. If you turn on more than one color at a
time, you will see the colors start to blend together to form a new color.

NEW CONCEPTS
ANALOG OUTPUT (PULSE-WIDTH MODULATION): The digitalWrite() command can turn pins on (5V)
or off (0V), but what if you want to output 2.5V? The analogWrite() command can output 2.5 volts by
quickly switching a pin on and off so that it is only on 50 percent of the time (50% of 5V is 2.5V). By doing
this, any voltage between 0 and 5V can be produced. This is what is known as Pulse-Width Modulation
(PWM). It can create many different colors on the RGB LED.

NEW IDEAS

PWM PINS: Only a few of the pins on the


MICROCONTROLLER have the circuitry
needed to turn on and off fast enough for PWM.
These are pins 3, 5, 6, 9, 10 and 11. Each
PWM pin is marked with a ~ on the board. Remember, you can only use analogWrite() on these pins.
Hookup Guide

Page | 2
Page | 3

SOURCE CODE:

int photoresistor = A0; //variable for storing the photoresistor value


int potentiometer = A1; //this variable will hold a value based on the position of the knob
int threshold = 700; //if the photoresistor reading is lower than this value the light will turn on

//LEDs are connected to these pins


int RedPin = 9;
int GreenPin = 10;
int BluePin = 11;

void setup() {
Serial.begin(9600); //start a serial connection with the computer

//set the LED pins to output


pinMode(RedPin, OUTPUT);
pinMode(GreenPin, OUTPUT);
pinMode(BluePin, OUTPUT);
}

void loop() {

Page | 4
photoresistor = analogRead(A0); //read the value of the photoresistor
potentiometer = analogRead(A1);

Serial.print("Photoresistor value:");
Serial.print(photoresistor); //print the photoresistor value to the serial monitor
Serial.print(" Potentiometer value:");
Serial.println(potentiometer); //print the potentiometer value to the serial monitor

if (photoresistor < threshold) {


//if it's dark (the photoresistor value is below the threshold) turn the LED on
//These nested if statements check for a variety of ranges and
//call different functions based on the current potentiometer value.
//Those functions are found at the bottom of the sketch.
if (potentiometer > 0 && potentiometer <= 150)
red();
if (potentiometer > 150 && potentiometer <= 300)
orange();
if (potentiometer > 300 && potentiometer <= 450)
yellow();
if (potentiometer > 450 && potentiometer <= 600)
green();
if (potentiometer > 600 && potentiometer <= 750)
cyan();
if (potentiometer > 750 && potentiometer <= 900)
blue();
if (potentiometer > 900)
magenta();
}
else { //if it isn't dark turn the LED off

turnOff(); //call the turn off function

Page | 5
}

delay(100); //short delay so that the printout is easier to read


}

void red () {

//set the LED pins to values that make red


analogWrite(RedPin, 100);
analogWrite(GreenPin, 0);
analogWrite(BluePin, 0);
}
void orange () {

//set the LED pins to values that make orange


analogWrite(RedPin, 100);
analogWrite(GreenPin, 50);
analogWrite(BluePin, 0);
}
void yellow () {

//set the LED pins to values that make yellow


analogWrite(RedPin, 100);
analogWrite(GreenPin, 100);
analogWrite(BluePin, 0);
}
void green () {
//set the LED pins to values that make green
analogWrite(RedPin, 0);
analogWrite(GreenPin, 100);
analogWrite(BluePin, 0);
Page | 6
}
void cyan () {

//set the LED pins to values that make cyan


analogWrite(RedPin, 0);
analogWrite(GreenPin, 100);
analogWrite(BluePin, 100);
}
void blue () {

//set the LED pins to values that make blue


analogWrite(RedPin, 0);
analogWrite(GreenPin, 0);
analogWrite(BluePin, 100);
}
void magenta () {

//set the LED pins to values that make magenta


analogWrite(RedPin, 100);
analogWrite(GreenPin, 0);
analogWrite(BluePin, 100);
}
void turnOff () {

//set all three LED pins to 0 or OFF


analogWrite(RedPin, 0);
analogWrite(GreenPin, 0);
analogWrite(BluePin, 0);}

You might also like