Arduino Nano 33 IoT - LED RGB

This guide shows you how to use the Arduino Nano 33 IoT to make an RGB LED glow in any color.

Arduino Nano 33 IoT RGB LED

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×RGB LED
1×Alternatively, RGB LED Module
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of RGB LED

The RGB LED can show many different colors by mixing red, green, and blue. It is made of three separate LEDs, one for each color, that are all in one case so it looks like a single LED.

RGB LED Pinout

An RGB LED has four pins.

  • R (red) pin: This pin controls the red light.
  • G (green) pin: This pin controls the green light.
  • B (blue) pin: This pin controls the blue light.
  • Common (Cathode-) pin: Connect this pin to the ground (0V).
RGB LED Pinout

To connect an RGB LED to the Arduino Nano 33 IoT, you need to add resistors to limit the current. This step can make the wiring more difficult. Fortunately, you can use an RGB LED module that already has the resitors built in.

The RGB LED Module comes with four small connectors.

  • Common (Cathode) pin: Connect this pin to ground (0 volts).
  • R (red) pin: Use this pin to control the red light.
  • G (green) pin: Use this pin to control the green light.
  • B (blue) pin: Use this pin to control the blue light.
RGB LED Module Pinout

※ NOTE THAT:

There are two kinds of LED based on the shared pin: one is called common anode and the other is common cathode. This lesson uses a common cathode LED.

How RGB LED works

In physics, a color is made up of three main parts: red, green, and blue. Each part can have a value from 0 to 255. When you mix these values, you can create 256 x 256 x 256 different colors.

If we send PWM signals to the R, G, and B pins, the RGB LED shows a color based on the PWM duty cycle values. By changing the duty cycle of these PWM signals (from 0 to 255), the RGB LED can display any color. The color values of Red (R), Green (G), and Blue (B) match the PWM duty cycles on their respective R, G, and B pins.

Wiring Diagram between RGB LED and Arduino Nano 33 IoT

  • Simple wiring diagram to connect the Arduino Nano 33 IoT with an RGB LED.
The wiring diagram between Arduino Nano and 33 IoT RGB LED

This image is created using Fritzing. Click to enlarge image

  • Simple wiring plan for connecting the Arduino Nano 33 IoT to an RGB LED module.
The wiring diagram between Arduino Nano and 33 IoT RGB LED module

This image is created using Fritzing. Click to enlarge image

How To Control RGB LED

Imagine you want your RGB LED to show the color #00979D. You can do it using this step:

  • Find a color code. Here are some hints:
    • Use this color picker to choose any color you like: https://www.w3schools.com/colors/colors_picker.asp
    • If you want to get a color from a picture, use this tool: https://html-color-codes.info/colors-from-image/
  • Next, change the color code into R, G, B numbers with this tool: https://www.w3schools.com/colors/colors_hexadecimal.asp. For example, you might get R = 0, G = 151, B = 157.
RGB LED color picker
  • Choose which Arduino Nano 33 IoT pins will connect to the red, green, and blue pins. For example:
#define PIN_RED D11 // Using digital pin D11 on the Arduino Nano 33 IoT for the red LED channel #define PIN_GREEN D10 // Using digital pin D10 on the Arduino Nano 33 IoT for the green LED channel #define PIN_BLUE D9 // Using digital pin D9 on the Arduino Nano 33 IoT for the blue LED channel
  • Set these Arduino Nano 33 IoT pins to work as outputs.
pinMode(PIN_RED, OUTPUT); pinMode(PIN_GREEN, OUTPUT); pinMode(PIN_BLUE, OUTPUT);
  • Make the LED glow with this color: #00979D (Red: 0, Green: 151, Blue: 157).
analogWrite(PIN_RED, 0); analogWrite(PIN_GREEN, 151); analogWrite(PIN_BLUE, 157);

Arduino Nano 33 IoT - RGB LED Example Code

This code changes the LED color by cycling through these colors in order:

  • Color Code #00C9CC – This color is made with red: 0, green: 201, blue: 204.
  • Color Code #F7788A – This color is made with red: 247, green: 120, blue: 138.
  • Color Code #34A853 – This color is made with red: 52, green: 168, blue: 83.
/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-led-rgb */ #define PIN_RED D11 // Arduino Nano 33 IoT D11 used for the red LED channel #define PIN_GREEN D10 // Arduino Nano 33 IoT D10 used for the green LED channel #define PIN_BLUE D9 // Arduino Nano 33 IoT D9 used for the blue LED channel void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_GREEN, OUTPUT); pinMode(PIN_BLUE, OUTPUT); } void loop() { // Set LED to hex color #00C9CC (Red = 0, Green = 201, Blue = 204) analogWrite(PIN_RED, 0); analogWrite(PIN_GREEN, 201); analogWrite(PIN_BLUE, 204); delay(1000); // Hold the color for 1 second // Set LED to hex color #F7788A (Red = 247, Green = 120, Blue = 138) analogWrite(PIN_RED, 247); analogWrite(PIN_GREEN, 120); analogWrite(PIN_BLUE, 138); delay(1000); // Hold the color for 1 second // Set LED to hex color #34A853 (Red = 52, Green = 168, Blue = 83) analogWrite(PIN_RED, 52); analogWrite(PIN_GREEN, 168); analogWrite(PIN_BLUE, 83); delay(1000); // Hold the color for 1 second }

When using many colors, we could shorten the code by creating a function:

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-led-rgb */ #define PIN_RED D11 // Define the Arduino Nano 33 IoT digital pin driving the red channel #define PIN_GREEN D10 // Define the Arduino Nano 33 IoT digital pin driving the green channel #define PIN_BLUE D9 // Define the Arduino Nano 33 IoT digital pin driving the blue channel void setup() { pinMode(PIN_RED, OUTPUT); // Set the red channel pin as an output pinMode(PIN_GREEN, OUTPUT); // Set the green channel pin as an output pinMode(PIN_BLUE, OUTPUT); // Set the blue channel pin as an output } void loop() { // Activate color with hex code #00C9CC (Red = 0, Green = 201, Blue = 204) setColor(0, 201, 204); delay(1000); // Maintain this color for 1 second // Activate color with hex code #F7788A (Red = 247, Green = 120, Blue = 138) setColor(247, 120, 138); delay(1000); // Maintain this color for 1 second // Activate color with hex code #34A853 (Red = 52, Green = 168, Blue = 83) setColor(52, 168, 83); delay(1000); // Maintain this color for 1 second } void setColor(int R, int G, int B) { analogWrite(PIN_RED, R); // Write the red intensity value to the red channel pin analogWrite(PIN_GREEN, G); // Write the green intensity value to the green channel pin analogWrite(PIN_BLUE, B); // Write the blue intensity value to the blue channel pin }

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!