Arduino Lesson 3. RGB LEDs

Similar documents
Lesson 4 RGB LED. Overview. Component Required:

Module 4: Traffic Signal Design Lesson 1: Traffic Signal (Arduino) Control System Laboratory Exercise Grade 6-8

R,G,B LEDs. Three PWM outputs and three primary colors. Just screams to be made, doesn t it? 220 (red,red,brown) or 330 (orange,orange,brown)

Ginger Bread House

Character LCDs. Created by lady ada. Last updated on :47:43 AM UTC

Preface. About SunFounder. About Super Kit. Free Support

Computer Architecture and Organization. Electronic Keyboard

Experiment 0: Hello, micro:bit!

Bill of Materials: 7-Segment LED Die with Arduino PART NO

ENGR 40M Project 3b: Programming the LED cube

1. Arduino Board and Breadboard set up from Project 2 (8 LED lights) 2. Piezo Speaker

The Micropython Microcontroller

Preface. If you have any TECHNICAL questions, add a topic under FORUM section on our website and we'll reply as soon as possible.

EP486 Microcontroller Applications

Arduino LED Matrix Control. Controlling lots of LEDs

9/23/2014. Andrew Costin, Tom Syster, Ryan Cramer Advisor: Professor Hack Instructor: Professor Lin May 5 th, 2014

The Infinity Portal Craig A. Lindley 03/16/2011

CT-DMX-300 LED Controller

Model Railway Animation: Part 1, LEDs - Expanded By David King

Smart Interface Components. Sketching in Hardware 2 24 June 2007 Tod E. Kurt

Introduction. The Clock Hardware. A Unique LED Clock Article by Craig A. Lindley

BLINKIN LED DRIVER USER'S MANUAL. REV UM-0 Copyright 2018 REV Robotics, LLC 1

successive approximation register (SAR) Q digital estimate

Lecture (04) Arduino Microcontroller Programming and interfacing. By: Dr. Ahmed ElShafee

Circuit Playground Express (& other ATSAMD21 Boards) DAC Hacks

MAKEVMP502 BASIC LEARNING KIT FOR RASPBERRY PI USER MANUAL

Touch Panel RGB LED Controller Part No. touch-panel-rgb

MAKE AN RGB CONTROL KNOB.

8 PIN PIC PROGRAMMABLE BOARD (DEVELOPMENT BOARD & PROJECT BOARD)

Circuit Playground Hot Potato

Digital 1 Final Project Sequential Digital System - Slot Machine

Theory and Practice of Tangible User Interfaces. Thursday Week 3: Analog Input. week. Sensor 1: Potentiometers. Analog input

Lesson Sequence: S4A (Scratch for Arduino)

Dynamic Animation Cube Group 1 Joseph Clark Michael Alberts Isaiah Walker Arnold Li

Alice EduPad Board. User s Guide Version /11/2017

Rfid Based Attendance System

Light Emitting Diodes (LEDs)

R20AD 10-bit Universal Decoder R-series Card Module User Manual

Measure the value of water flow using water flow sensor and DC water pump 12 V interfacing with Arduino uno

Assignment #3: Piezo Cake

Using Sliders with the 4201-B Lighting Controller Application Note June 26, 2008

Part No./ 型号 : RGB-Controller-101

Fig. 1 Analog pins of Arduino Mega

StickIt! VGA Manual. How to install and use your new StickIt! VGA module

Lab #10: Building Output Ports with the 6811

LEVEL CROSSING MODULE FOR LED SIGNALS LCS2

LT-300 LED Controller

Introduction 1. Digital inputs D6 and D7. Battery connects here (red wire to +V, black wire to 0V )

Design and Implementation of Timer, GPIO, and 7-segment Peripherals

Experiment (6) 2- to 4 Decoder. Figure 8.1 Block Diagram of 2-to-4 Decoder 0 X X

SparkFun Inventor's Kit for micro:bit Experiment Guide

Objectives: Learn how LED displays work Be able to output your name on the display

Introduction 1. Green status LED, controlled by output signal ST. Sounder, controlled by output signal Q6. Push switch on input D6

This module senses temperature and humidity. Output: Temperature and humidity display on serial monitor.

Design and Implementation of an AHB VGA Peripheral

DMC550 Technical Reference

PHYS 3322 Modern Laboratory Methods I Digital Devices

Basic LabVIEW Programming Amit J Nimunkar, Sara Karle, Michele Lorenz, Emily Maslonkowski

Giant Digital Letterforms: LEDs and Pixelated Type

LED Array Tutorial. This guide explains how to set up and operate the LED arrays that can be used for your. Internal Structure of LED Array

Microcontrollers. Outline. Class 4: Timer/Counters. March 28, Timer/Counter Introduction. Timers as a Timebase.

COLORtube EQ Controller

TV Synchronism Generation with PIC Microcontroller

Studio DMX Decoders. Part numbers: DMX , DMX , DMX

Chapter 4: One-Shots, Counters, and Clocks

Digital Circuits 4: Sequential Circuits

SWITCH: Microcontroller Touch-switch Design & Test (Part 2)

DOGM GRAPHIC SERIES. 128x64, 3.3V available in low quantity! flat: 5.6mm with LED b./l. mounted TECHNICAL DATA ORDERING CODE ACCESSORIES

Introduction 1. Green status LED, controlled by output signal ST

DOGM GRAPHIC SERIES 132x32 DOTS

ECE Lab 5. MSI Circuits - Four-Bit Adder/Subtractor with Decimal Output

16 Universe LED Matrix Panels Instructions

WaveMaker III Gartech Enterprises Inc. 12/17/2012

Data Acquisition Using LabVIEW

Informatics Enlightened Station 1 Sunflower

TV Character Generator

Design and Implementation of SOC VGA Controller Using Spartan-3E FPGA

Edge Connector Light Level Detector

Laboratory 11. Required Components: Objectives. Introduction. Digital Displays and Logic (modified from lab text by Alciatore)

LED PAR 36 USER MANUAL

WiFi Time Provider v1 for Arduino Nixie Clock Operating Instructions & Construction Manual

DOGM GRAPHIC SERIES 128x64 DOTS

LT A LED RGB Controller. User s Manual

10 Zone RGB-W LED Controller

EA DOGL128x-6 EA LED68X51-RGB

Device: LDP This document Version: 1.1. Date: July Description: 64x16 1R1G LED Display Panel

Product Specification

Solutions to Embedded System Design Challenges Part II

Lab #6: Combinational Circuits Design

7 SEGMENT LED DISPLAY KIT

Electronic Panel Meters DIGEM Preference Program Process control, automation & laboratory uses Class 0.01 to 1 Current, Voltage, Frequency,

Product Specifications

W0EB/W2CTX DSP Audio Filter Operating Manual V1.12

ELECTRONIC GAME KIT TEACHING RESOURCES. Version 2.0 BUILD YOUR OWN MEMORY & REACTIONS

Lecture 23 Design for Testability (DFT): Full-Scan

C2 +5V. (14) Vdd (+5 Vdc) (13) OSC1/A7 1.2K (12) 1.2K (11) 1.2K (10) U1 16F628 16F628A 1.2K (1) A2 1.2K (8) 1.2K (7) 1.2K. (Gnd) Vss (5) (6) 1.

EE 367 Lab Part 1: Sequential Logic

Published in A R DIGITECH

Lab Using The Multimeter And The Trainer

Self-Playing Xylophone

Transcription:

Arduino Lesson 3. RGB LEDs Created by Simon Monk Last updated on 2013-06-22 06:45:59 PM EDT

Guide Contents Guide Contents Overview Parts Part Qty Breadboard Layout Colors Arduino Sketch Using Internet Colors Theory (PWM) Other Things to Do 2 3 4 4 4 6 8 9 12 13 14 Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds Page 2 of 14

Overview In this lesson, you will learn how to use a RGB (Red Green Blue) LED with an Arduino. You will use the analogwrite function of Arduino to control the color of the LED. At first glance, RGB (Red, Green, Blue) LEDs look just like regular LEDs, however, inside the usual LED package, there are actually three LEDs, one red, one green and yes, one blue. By controlling the brightness of each of the individual LEDs you can mix pretty much any color you want. We mix colors just like you would mix audio with a 'mixing board' or paint on a palette - by adjusting the brightness of each of the three LEDs. The hard way to do this would be to use different value resistors (or variable resistors) as we played with in lesson 2. That's a lot of work! Fortunately for us, the Arduino has an analogwrite function that you can use with pins marked with a ~ to output a variable amount of power to the appropriate LEDs. Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds Page 3 of 14

Parts To build the project described in this lesson, you will need the following parts. Part Qty Diffuse RGB LED 10mm 1 270 Ω Resistors (red, purple, brown stripes) - you can use up to 1K ohm although it will be a little dimmer 3 Half-size Breadboard 1 Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds Page 4 of 14

Arduino Uno R3 1 Jumper wire pack 1 Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds Page 5 of 14

Breadboard Layout The RGB LED has four leads. There is one lead going to the positive connection of each of the single LEDs within the package and a single lead that is connected to all three negative sides of the LEDs. The common negative connection of the LED package is the second pin from the flat side of the LED package. It is also the longest of the four leads. This lead will be connected to ground. Each LED inside the package requires its own 270Ω resistor to prevent too much current flowing through it. The three positive leads of the LEDs (one red, one green and one blue) are connected to Arduino output pins using these resistors. If you are using a common ANODE LED instead of common CATHODE, connect the Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds Page 6 of 14

If you are using a common ANODE LED instead of common CATHODE, connect the long pin to +5 instead of ground Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds Page 7 of 14

Colors The reason that you can mix any color you like by varying the quantities of red, green and blue light is that your eye has three types of light receptor in it (red, green and blue). Your eye and brain process the amounts of red, green and blue and convert it into a color of the spectrum. In a way, by using the three LEDs we are playing a trick on the eye. This same idea is used in TVs, where the LCD has red, green and blue color dots next to each other making up each pixel. If we set the brightness of all three LEDs to be the same, then the overall color of the light will be white. If we turn off the blue LED, so that just the red and green LEDs are the same brightness, then the light will appear yellow. We can control the brightness of each of the red, green and blue parts of the LED separately, making it possible to mix any color we like. Black is not so much a color as an absense of light. So the closest we can come to black with our LED is to turn off all three colors. Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds Page 8 of 14

Arduino Sketch The following test sketch will cycle through the colors red, green, blue, yellow, purple, and aqua. These colors being some of the standard Internet colors. /* Adafruit Arduino - Lesson 3. RGB LED */ int redpin = 11; int greenpin = 10; int bluepin = 9; void setup() { pinmode(redpin, OUTPUT); pinmode(greenpin, OUTPUT); pinmode(bluepin, OUTPUT); } void loop() { setcolor(255, 0, 0); // red setcolor(0, 255, 0); // green setcolor(0, 0, 255); // blue setcolor(255, 255, 0); // yellow setcolor(80, 0, 80); // purple setcolor(0, 255, 255); // aqua } void setcolor(int red, int green, int blue) { analogwrite(redpin, red); analogwrite(greenpin, green); analogwrite(bluepin, blue); } Try the sketch out and then we will dissect it in some detail... The sketch starts by specifying which pins are going to be used for each of the colors: int redpin = 11; int greenpin = 10; int bluepin = 9; Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds Page 9 of 14

The next step is to write the 'setup' function. As we have learnt in earlier lessons, the setup function runs just once after the Arduino has reset. In this case, all it has to do is define the three pins we are using as being outputs. void setup() { pinmode(redpin, OUTPUT); pinmode(greenpin, OUTPUT); pinmode(bluepin, OUTPUT); } Before we take a look at the 'loop' function, lets look at the last function in the sketch. void setcolor(int red, int green, int blue) { analogwrite(redpin, red); analogwrite(greenpin, green); analogwrite(bluepin, blue); } This function takes three arguments, one for the brightness of the red, green and blue LEDs. In each case the number will be in the range 0 to 255, where 0 means off and 255 means maximum brightness. The function then calls 'analogwrite' to set the brightness of each LED. If you look at the 'loop' function you can see that we are setting the amount of red, green and blue light that we want to display and then pausing for a second before moving on to the next color. void loop() { setcolor(255, 0, 0); // red setcolor(0, 255, 0); // green setcolor(0, 0, 255); // blue setcolor(255, 255, 0);// yellow setcolor(80, 0, 80); // purple setcolor(0, 255, 255);// aqua } Try adding a few colors of your own to the sketch and watch the effect on your LED. If you are using a Common Anode RGB LED, then you will need to change the analog write values so that the color is subtracted from 255, like this: analogwrite(redpin, 255-red); for each of the three colors. Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds Page 10 of 14

Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds Page 11 of 14

Using Internet Colors If you have done any Internet programming, you will probably be aware that colors are often represented as a 'hex' number. For example the color red has the number #FF0000. You can find the numbers associated with a particular color using tables like these: http://www.devguru.com/features/colors (http://adafru.it/az6) The six digits of the number are actually three pairs of numbers; the first pair being the red component of the color, the next two digits the green part and the final pair the blue part. Red is #FF0000, because its maximum red (FF is 255 in hex) and it has no green or blue part. It would be pretty useful to be able to dial up one of these color numbers so that it is displayed on the RGB LED. Let's try and make the color indigo (#4B0082). The red, green and blue parts of indigo are (in hex) 4B, 00 and 82 respectively. We can plug those into the 'setcolor' function like this: setcolor(0x4b, 0x0, 0x82); // indigo We have used hex numbers for the three parts of the color by putting '0x' in front of them. Try adding a few colors of your own to the 'loop' function. Don't forget to add a delay after each one. Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds Page 12 of 14

Theory (PWM) Pulse Width Modulation (or PWM) is a technique for controlling power. We also use it here to control the brightness of each of the LEDs. The diagram below shows the signal from one of the PWM pins on the Arduino. Roughly every 1/500 of a second, the PWM output will produce a pulse. The length of this pulse is controlled by the 'analogwrite' function. So 'analogwrite(0)' will not produce any pulse at all and 'analogwrite(255)' will produce a pulse that lasts all the way until the next pulse is due, so that the output is actually on all the time. If we specify a value in the analogwrite that is somewhere in between 0 and 255 then we will produce a pulse. If the output pulse is only high for 5% of the time then whatever we are driving will only receive 5% of full power. If however the output is at 5V for 90% of the time then the load will get 90% of the power delivered to it. We cannot see the LEDs turning on and off at that speed, so to us, it just looks like the brightness is changing. Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds Page 13 of 14

Other Things to Do Try putting a ping-pong ball over the LED Try changing the delays to speed up or slow down the color changing There are lots of things you can do with RGB LEDs. Check out some of the projects on the Internet using RGB LEDs and you will find multi-color persistence of vision devices, as well as all sorts of lighting effects. About the Author Simon Monk is author of a number of books relating to Open Source Hardware. The following books written by Simon are available from Adafruit: Programming Arduino (http://adafru.it/1019), 30 Arduino Projects for the Evil Genius (http://adafru.it/868) and Programming the Raspberry Pi (http://adafru.it/am5). Adafruit Industries Last Updated: 2013-06-22 06:46:01 PM EDT Page 14 of 14