Lesson 19 – RGB LED


Tutorial

1. Introduction
We have controlled the color change of RGB light by controlling the raspberry pi. To achieve RGB LED controlling, it needs 3-channel PWM, while raspberry pi only has one way hardware PWM output (GPIO1). In the experiment, we use the wiringPi library to configure GPIO0, GPIO1, GPIO2 as soft PWM output.

2. Hardware Required

  • RGB – LED *1
  • 220Ω Resistor *3
  • RPI GPIO Shield *1
  • 40Pin Colorful Ribbon Cable *1
  • USB Cable*1
  • Breadboard*1
  • Raspberry Motherboard*1
  • Jumper Wire*Several

3. Connection Diagram

4. Programming

  • a. Use winSCP to put the lesson18_RGB_LED into the lesson folder of raspberry system.
    b. In the terminal input cd lesson18_RGB_LED to enter the folder.
    c. In the terminal execute make to generate an RGB_LED executable file inside the folder.
    d. Wiring as the above diagram, then enter the lesson18_RGB_LED folder, execute sudo ./RGB_LED at the terminal.
    RGB light will display different colors.
    e. Ctrl + c can exit the processing program.

5. Sample Code

The first four lines below are –

  • #include <stdio.h>
  • #include <softPwm.h>
  • #include <stdint.h>
  • #include <wiringPi.h>
 

#include 
#include   
#include   
#include   
  
#define LedPinRed    0  
#define LedPinGreen  1  
#define LedPinBlue   2  
  
int colors[] = {0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0x00FFFF, 0xFF00FF, 0xFFFFFF, 0x9400D3};  
  
/****************************************************************************************** 
*A number is linear mapped from a range to another one, for example, a number from 0 to 100 is mapped to 0 ~ 255. 
******************************************************************************************/  
int map(int x, int in_min, int in_max, int out_min, int out_max)     
{  
    return (x -in_min) * (out_max - out_min) / (in_max - in_min) + out_min;  
}  
  
void ledInit(void)  
{  
    softPwmCreate(LedPinRed,  0, 100);  //create a soft pwm, original duty cycle is 0Hz, range is 0~100   
    softPwmCreate(LedPinGreen,0, 100);  
    softPwmCreate(LedPinBlue, 0, 100);  
}  
  
void ledColorSet(int color)        //set color, for example: 0xde3f47  
{  
    int r_val, g_val, b_val;  
  
    r_val = (color & 0xFF0000) >> 16;  //get red value  
    g_val = (color & 0x00FF00) >> 8;   //get green value  
    b_val = (color & 0x0000FF) >> 0;   //get blue value  
  
    r_val = map(r_val, 0, 255, 0, 100);   //change a num(0~255) to 0~100  
    g_val = map(g_val, 0, 255, 0, 100);  
    b_val = map(b_val, 0, 255, 0, 100);  
      
    softPwmWrite(LedPinRed,   100 - r_val);  //change duty cycle  
    softPwmWrite(LedPinGreen, 100 - g_val);  
    softPwmWrite(LedPinBlue,  100 - b_val);  
}  
  
int main(void)  
{  
    int i;  
  
    if(wiringPiSetup() == -1){      //when initialize wiringPi failed, print message to screen  
        printf("setup wiringPi failed !\n");  
        return 1;   
    }  
  
    ledInit();  
  
    while(1){  
        for(i = 0; i < sizeof(colors)/sizeof(int); i++){  
            ledColorSet(colors[i]);  
            delay(500);  
        }  
    }  
  
    return 0;  
}

 

Prerequisites

  1. This development track is based on the Rasbperry Pi and the Ultimate Starter Kit for the Raspberry Pi.
  2. You will need access to both the Raspberry Pi SBC and the electronics components part of the Ultimate Starter Kit for the Raspberry Pi kit to be able to work on these tutorials.
  3. If you haven’t purchased the Raspberry Pi 4 B yet please head over to our store and purchase one now. You can pick up the Ultimate Starter Kit for the Raspberry Pi from OztoyLib.
  4. Depending on where you live you might also be able to pick up the Raspberry Pi and Ultimate Starter Kit for the Raspberry Pi at your local electronics hobby store.

About the Ultimate Starter Kit for the Raspberry Pi

The Ultimate Starter Kit for the Raspberry Pi comes packed with ~37 different electronic bits (Sensors, LEDs, switches, LCD, servo, etc.) including tutorials to get started. You will learn how to connect up the ~37 different electronic bits (Sensors, LEDs, switches, LCD, servo, etc.), create circuits using the Raspberry Pi including learning to write code in C. As you progress through the different tutorials you will explore the different capabilities of the Raspberry Pi including the intricacies of integrating the Raspberry Pi with the different electronic bits i.e. sensors, LED’s, switches, servos, etc. included in the starter kit.

The Ultimate Starter Kit for the Raspberry Pi comes along with detailed tutorials including code samples. Ultimate Starter Kit for the Raspberry Pi walks you through the basics of using the Raspberry Pi in a hands-on way. You’ll learn the fundamentals of electronics, programming in C and gain hands on experience through working on the Raspberry Pi building creative projects. The kit includes a selection of the most common and useful electronic components with an ebook of 37 projects. Starting the basics of electronics, to more complex projects, the kit will get you interacting with the physical world using sensor and actuators. Along with the kit you get access to detailed tutorials and wiring diagrams.


About the Raspberry Pi

The Raspberry Pi is a series of small single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and in developing countries. It is a capable little computer which can be used in electronics projects, and for many of the things that your desktop PC does, like spreadsheets, word processing, browsing the internet, and playing games. The original model became far more popular than anticipated, selling outside its target market for uses such as robotics.

The Raspberry Pi does not include peripherals (such as keyboards, mice and cases). However, some accessories have been included in several official and unofficial bundles. According to the Raspberry Pi Foundation, over 5 million Raspberry Pis were sold by February 2015, making it the best-selling British computer. By November 2016 they had sold 11 million units, and 12.5m by March 2017, making it the third best-selling “general purpose computer”. In July 2017, sales reached nearly 15 million.In March 2018, sales reached 19 million. Most Pis are made in a Sony factory in Pencoed, Wales; some are made in China or Japan.

You can read more about the Raspberry Pi here – RaspberryPi.org.

 

Questions