NodeMCU RGB LED using MicroPython

RGB Oct 06, 2019

This tutorial explains how to use RGB LED on NodeMCU using MicroPython.

Introduction :

An RGB LED is a combination of 3 LEDs RED, Green and Blue. These three colors Red, green and blue can make any color. By varying supplied voltage to RGB LEDs different colors are formed. In the Node MCU different voltage are supplied using analog output function.

An RGB LED has 4 pin interfaces. 3 Pins are for Red, Blue and Green.

An RGB LED can be two types-

  1. Common Anode:-Anode (+) pin is common.
  2. Common Cathode:- (Cathode-/GND) pin is common.

Hardware Required:

  1. NodeMCU - 1
  2. RGB LED - 1
  3. Resistor 430Ω - 1
  4. Jumper Female To Female - 4

Pin Diagram of NodeMCU

gp

Circuit Diagram

Untitled-design--5--1

Procedure:

  1. Connect Negative pin of RGB led to GND pin of NodeMCU through a 430Ω resistor.
  2. Connect Red pin of LED to D5 pin, Green pin to D6 and Blue pin to D7 pin of NodeMCU using Female to Female cables.
  3. To supply power to NodeMCU you can either use a power supply or simply plug it into your laptop through the USB port.
  4. Now, our cirucit is complete. We have to load the program for RGB Led colour variations to NodeMcu using Ampy.
  5. Put the following code in test.py file

import machine
import time

blue = machine.Pin(14, machine.Pin.OUT)
red = machine.Pin(12, machine.Pin.OUT)
green = machine.Pin(13, machine.Pin.OUT)

while True:
        green.on()
        time.sleep_ms(500)
        green.off()
        blue.on()
        time.sleep_ms(500)
        blue.off()
        red.on()
        time.sleep_ms(500)
        red.off()
        
  1. To run this command we have to upload it into NodeMCU using ampy
    We do this by executing the command:
ampy --port /dev/tty.SLAB_USBtoUART run test.py

Output:

After successful uploading of the code, the RGB LED generates three different colors in a time interval of 500 millisecond. Try different set of RGB to get different colors.

Note :

Please do let us know if we are missing any topic or you want us to elaborate any topic, comment below and subscribe to be updated.