Code blocks for RGB LED interfacing with Arduino in Tinkercad

CODE BLOCKS FOR RGB LED INTERFACING WITH ARDUINO IN TINKERCAD

In the previous topic, you learned Code Blocks for how to blink an LED, in this section I will show you, code blocks for RGB LED interfacing with Arduino in tinkercad. In simulation, you can see the output of the LED, the colour of LED getting changed every second from red to blue and blue to green repeatedly.

REQUIRED HARDWARE OR COMPONENT

S.N. COMPONENTS QUANTITY
1. Arduino 1
2. RGB LED (Common Cathode CC) 1
3. Resistor (280 ohms) 3
4. Connecting Wires Few

CIRCUIT DIAGRAM

Circuit Diagram RGB LED in tinkercad

CONNECTION TABLE FOR RGB LED INTERFACING WITH ARDUINO IN TINKERCAD

  S.N. Arduino Pin RGB LED
  1. 11   RED
  2. GND   Cathode (–)
  3. 09   BLUE
  4. 06   GREEN

CIRCUIT EXPLANATION

The RGB LED has four terminals (three anodes and one cathode) that are Red (+), Cathode (–), Blue (+) & Green (+) and it is a common cathode type LED. 

RGB LED PINOUT Common anode and cathode

Here its Red, Cathode, Blue & Green terminals are connected to pin 11, GND, pin 9 and pin 8 of Arduino respectively.

Here we have a resistor connected between the LED anode (+) pin and Arduino output pins 11, 9 and 6.

The resistor is used here because it helps us to limit the current and prevent the RGB LED from burning. If we do not connect the resistor, you get a message in software during simulation that the “LED broke because of: LED is driven by 64.1 mA while maximum current is 20.0 mA”.

The usable lifetime of the LED may reduce. That’s why there is a need for a resistor to reduce the current. Therefore a resistor is required to reduce the current.

ARRANGEMENTS OF CODE BLOCKS FOR RGB LED INTERFACING WITH ARDUINO IN TINKERCAD AND TEXT CODE

Code Blocks for RGB LED

RGB LED block code in tinkercad
Click to Enlarge

 

Text Code for RGB LED

RGB LED text code in tinkercad
Click to Enlarge

CODE BLOCKS FOR RGB LED INTERFACING WITH ARDUINO IN TINKERCAD AND TEXT CODE EXPLANATION

CODE BLOCK EXPLANATION

Step 1:  First click on the output block, then drag and drop the “set RGB LED in pins” command from it to the workspace. 

setpins for RGB LED in tinkercad

By default, it is set to 3, 3, 3 and colour to red. Change number 3 to 11 from the dropdown menu (where the red pin of the RGB LED is connected to the circuit), then next 3 to to 9 from the next dropdown menu (where the blue pin of the RGB LED is connected to the circuit) and at last 3 to 6 from the dropdown menu (where the green pin of the RGB LED is connected to the circuit).

rgb led after colour set tinkercad

Now click on the color option here, the color box will appear and from this, we will select the red color.

set colour for rgb led tinkercad

Step 2: After this, go to the control block option, then drag and drop the “wait” command block from it to the work area, below the first block and set wait time to 1 sec or more as per your choice.

add wait 1 section

Step 3: Click on the output block, then drag and drop the “set RGB LED in pins” command from it to the work space, below the wait command block and select pins in the dropdown menu the same as we selected in step 1 (11, 9, 6).

Now click on the color option this time select blue colour from the colour box.

change rgb led colour to blue

Step 4: After this, go to the control block option, then drag and drop the “wait” command block from it to the work area, below the third block and set wait time to 1 sec or more as per your choice.

add wait again

Step 5: Click on the output block, then drag and drop the “set RGB LED in pins” command from it to the work space, below the wait command block and select pins in the dropdown menu the same as we selected in step 1 (11, 9, 6).

Now click on the color option, this time select green colour from the colour box.

change rgb led colour to green

Step 6: After this, go to the control block option, then drag and drop the “wait” command block from it to the work area, below the fifth block and set wait time to 1 sec or more as per your choice.

add final wait

TEXT CODE EXPLANATION

In setup() function, it automatically generates lines of code as soon as you select input or output blocks.

The Function “pinMode( )” has been defined here in the pin working mode as an input/output.

Function pinMode() ” is used to define the pin working mode as an input/output.

In this function we have to pass two argument –

  1. First one is pin number and

Second is its mode, pinMode(pinNumber, mode).

void setup() {
  pinMode(11, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(6, OUTPUT);
}

In the loop() function, one of the inbuilt function that is “analogWrite(x, y)” is used to create a PWM signal, where x is the digital pin and y is a value for the “duty cycle”, between “0 and 255”.

Where 0 indicates 0% duty cycle and 255 indicates a 100 % duty cycle, the value of ‘y’ will decide the brightness of the LED (how much voltage passing in scale from 0 to 5 V within the range from 0 to 255). For example

analogWrite(PinNumber, value);
analogWrite(11, 153);

PinNumber should be a PWM supported pin, the tilde sign on the Arduino board shows PWM supported pins. 

Another function used is “delay()” , which is used to pause the program for the given interval of time in milliseconds (1 second = 1000 millisecond) i.e.

  delay(1000);
void loop() {
  analogWrite(11, 153);
  analogWrite(9, 0);
  analogWrite(6, 0);
  delay(1000);

  analogWrite(11, 0);
  analogWrite(9, 153);
  analogWrite(6, 0);
  delay(1000);

  analogWrite(11, 0);
  analogWrite(9, 0);
  analogWrite(6, 153);
  delay(1000);
}

START SIMULATION OF RGB LED INTERFACING WITH ARDUINO

Click on the start simulation button to perform the RGB LED program and to test the circuit.

rgb led output simulation tinkercad
Click to Enlarge

 

[pad]


NEXT POST
CODE BLOCKS FOR SWITCH AND LED INTERFACING WITH ARDUINO IN TINKERCAD


PREVIOUS POST
LED BLINKING USING ARDUINO IN TINKERCAD