Gather Workshops Logo

Spectrum Spinner

All the colours of the rainbow.

RGB LED

What We Are Making

Change the colour of a light by turning a dial.

Bits We Need

  • RGB LED
    RGB LED
    A visual output for our circuit
  • Button
    Potentiometer
    A dial which outputs a range of values
  • 10k Ohm Resistor
    3x 330Ω Resistor
    To regulate the voltage to the LED
  • Wire
    Wires
    To connect everything together!

LED Overview

Setting up our LED will once again consist of two parts:
the physical components, and the logical components

RGB LED Diagram

RBG LEDs

This LED can display a range of colours
by mixing together levels of red, green and blue.

It has four pins: Red, Earth, Green, and Blue.

It requires 330 ohm resistors for each colour pin,
to keep values sent to the pins from interfering
with each other.

Physical Components
of the LED Circuit

Output wiring diagram

Set up your LED as in the diagram.

Logical Components
of the LED Circuit

  • Test Buttons
    Test Buttons
    On-screen buttons, one for each
    LED colour component.
  • Arduino Outputs
    Light Output
    One Arduino pin output for each
    LED colour component.

Pass a Message Out to the Arduino

LED Node Configuration

Drag an arduino out node into your workspace and configure it.

Inject a Message

Inject Node Configuration

Drag an inject node into your workspace and configure it.

Connect the nodes

Inject Node Configuration

Click and drag the small square on the inject node,
and attach it to the arduino out node.

Deploy Your Code

Deploying Node Red code to the Arduino

Click the “Deploy” button in Node Red
to link your logic flow with the Arduino.

Test the LED Output

Click the square trigger button on the inject node.

Your LED should turn red.

Test Green and Blue

Multiple Inject Nodes

Add two more flows to test the green and blue.

The test buttons should all work correctly.

Potentiometer Overview

Setting up our potentiometer will also consist of two parts:
the physical components, and the logical components

Physical Components
of the Potentiometer Circuit

Potentiometer wiring diagram

Set up your potentiometer as in the diagram.

Logical Components
of the Potentiometer Circuit

  • Arduino In
    Potentiometer Input
    Converts the Arduino signal
    into a JavaScript message.
  • Debug Node
    Debug Logger
    Displays the JS message on
    the screen when received.

Receive a Message from the Arduino

Button Input Node Configuration

Drag an arduino in node into your workspace and configure it.

Debug the Incoming Data

Button Debug Node Configuration

Drag a debug node into your workspace. The default configuration is fine.

Connect the nodes

Join potentiometer Nodes

Join your arduino node to your debug node.

Deploy Your Code

Deploying Node Red code to the Arduino

Click the “Deploy” button in Node Red
to link your logic flow with the Arduino.

Test the potentiometer input

Turn the dial on your potentiometer.

You should see value being logged in your debug panel.

Logical Components
of the whole circuit

  • Arduino In
    Potentiometer Input
    Receives an Arduino signal
    as a JavaScript message.
  • Function
    Conversion Function
    Converts the potentiometer signal into the correct message for the LED.
  • Arduino Outputs
    LED Outputs
    Sends the JavaScript message
    as an Arduino signal.

Circuit Arduino Nodes

Full Circuit with all nodes

Link your arduino input to the three arduino output nodes
via a function, like we did last time.

This time your function requires three outputs instead of one.

Function Code

var dialValue = msg.payload;
var redOn = false;
var greenOn = false;
var blueOn = false;

if(dialValue < 300){
    redOn = true;
} else if(dialValue > 900) {
    blueOn = true;
} else {
    greenOn = true;
}

var redMessage = { payload: redOn };
var greenMessage = { payload: greenOn };
var blueMessage = { payload: blueOn };

return [redMessage, greenMessage, blueMessage];

Paste this code into your function config popup,
in the code editor section.

Shorter Function Code

var dialPosition = msg.payload;

var messages = [
    {payload: (dialPosition <= 300)}, // red
    {payload: (dialPosition > 300 && dialPosition < 900)}, // green
    {payload: (dialPosition >= 900)} //blue
]

return messages;

For experienced coders:
This code does exactly the same thing,
but is a bit shorter.

Deploy Your Code

Deploying Node Red code to the Arduino

Click the “Deploy” button in Node Red
to link your logic flow with the Arduino.

Full Circuit Test

Turn the dial to change the colour of your LED.

Your potentiometer should now act like a colour picker.

Spectrum Mood Light

Challenge: Mood Lighting

Modify the conversion function to output 5 or more colours.

Bonus points: Fade the LED through a continuous spectrum.

Thumbs Up!

Spectrum Spinner: Complete!

Yay, onwards to the next adventure…

Take me to the next chapter!

Loading...