9. Chapter Potentiometer & RGBLED

In this chapter, we will use 3 potentiometers to control the brightness of 3 LEDs of RGBLED to create multiple colors.

9.1. Project Colorful Light

In this project, 3 potentiometers are used to control the RGB LED and in principle it is the same as with the Soft Light. project. Namely, read the voltage value of the potentiometer and then convert it to PWM used to control LED brightness. Difference is that the previous soft light project needed only one LED while this one required (3) RGB LEDs.

9.1.1. Component List

  1. Raspberry Pi (with 40 GPIO) x1

  2. GPIO Extension Board & Ribbon Cable x1

  3. Breadboard x1

220Ω x3

res-220R

Rotary potentiometer x3

Rotary-potentiometer

Resistor 10kΩ x2

Resistor-10kΩ

ADC module x1 (Only one)

ADC-module-1 or ADC-module-2

RGB LED x1

RGB-LED-real

Jumper Wire M/M x17

jumper-wire

9.1.2. Circuit with ADS7830

Schematic diagram

ADS7830-Schematic-3

Hardware connection. If you need any support,please feel free to contact us via:

support@freenove.com

ADS7830-fritizing-3

If circuit above doesn’t work, please try following wiring.

ADS7830-fritizing-4

9.1.3. Circuit with PCF8591

Schematic diagram

PCF8591-Schematic-3

Hardware connection.

PCF8591-fritizing-3

Note

Youtube video: https://youtu.be/CmLLNsBMN2U

9.1.4. Code

9.1.4.1. C Code Colorful Softlight

If you did not configure I2C, please refer to Chapter 7. If you did, please continue.

First, observe the project result, and then learn about the code in detail.

Hint

If you have any concerns, please contact us via: support@freenove.com

  1. Use cd command to enter 09.1.1_ColorfulSoftlight directory of C code.

$ cd ~/Freenove_Kit/Code/C_Code/09.1.1_ColorfulSoftlight
  1. Use following command to compile ColorfulSoftlight.cpp and generate executable file ColorfulSoftlight.

$ g++ ColorfulSoftlight.cpp -o ColorfulSoftlight -lwiringPi -lADCDevice
  1. Then run the generated file ColorfulSoftlight.

$ sudo ./ColorfulSoftlight

After the program is executed, rotate one of the potentiometers, then the color of RGB LED will change. The Terminal window will display the ADC value of each potentiometer.

../../../_images/ADC-value.png

The following is the program code:

 1/**********************************************************************
 2* Filename    : Softlight.cpp
 3* Description : Use potentiometer to control LED
 4* Author      : www.freenove.com
 5* modification: 2020/03/07
 6**********************************************************************/
 7#include <wiringPi.h>
 8#include <stdio.h>
 9#include <softPwm.h>
10#include <ADCDevice.hpp>
11
12#define ledRedPin 3         //define 3 pins for RGBLED
13#define ledGreenPin 2
14#define ledBluePin 0
15
16ADCDevice *adc;  // Define an ADC Device class object
17
18int main(void){
19    adc = new ADCDevice();
20    printf("Program is starting ... \n");
21
22    if(adc->detectI2C(0x48)){    // Detect the pcf8591.
23        delete adc;                // Free previously pointed memory
24        adc = new PCF8591();    // If detected, create an instance of PCF8591.
25    }
26    else if(adc->detectI2C(0x4b)){// Detect the ads7830
27        delete adc;               // Free previously pointed memory
28        adc = new ADS7830();      // If detected, create an instance of ADS7830.
29    }
30    else{
31        printf("No correct I2C address found, \n"
32        "Please use command 'i2cdetect -y 1' to check the I2C address! \n"
33        "Program Exit. \n");
34        return -1;
35    }
36    wiringPiSetup();
37    softPwmCreate(ledRedPin,0,100);     //creat 3 PMW output pins for RGBLED
38    softPwmCreate(ledGreenPin,0,100);
39    softPwmCreate(ledBluePin,0,100);
40    while(1){
41        int val_Red = adc->analogRead(0);  //read analog value of 3 potentiometers
42        int val_Green = adc->analogRead(1);
43        int val_Blue = adc->analogRead(2);
44        softPwmWrite(ledRedPin,val_Red*100/255);    //map the read value of potentiometers into PWM value and output it
45        softPwmWrite(ledGreenPin,val_Green*100/255);
46        softPwmWrite(ledBluePin,val_Blue*100/255);
47        //print out the read ADC value
48        printf("ADC value val_Red: %d  ,\tval_Green: %d  ,\tval_Blue: %d \n",val_Red,val_Green,val_Blue);
49        delay(100);
50    }
51    return 0;
52}

In the code you can read the ADC values of the 3 potentiometers and map it into a PWM duty cycle to control the 3 LED elements to vary the color of their respective RGB LED.