Gather Workshops Logo

Binary Blink

Light on, light off.

Rainbow Dash turning light on and off

What We Are Making

Turn a light on and off by pushing a button.

Bits We Need

  • Red LED
    LED
    A visual output for our circuit
  • Button
    Button
    An input, used to turn our light on and off
  • 10k Ohm Resistor
    10k Resistor
    To regulate the voltage readings from the button
  • Wire
    Wires
    To connect everything together!

LED Overview

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

Physical Components
of the LED Circuit

Output wiring diagram

Set up your LED as in the diagram.

Logical Components
of the LED Circuit

  • Test Button
    Test Button
    On-screen button, sends message
    that light should turn on.
  • Arduino Out
    Light Output
    Converts test button message
    into an Arduino signal.

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 on and stay on.

Button Overview

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

Physical Components
of the Button Circuit

Output wiring diagram

Set up your button as in the diagram.

Logical Components
of the Button Circuit

  • Arduino Out
    Button Input
    Converts the Arduino signal
    into a JavaScript message.
  • Test Button
    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 and configure it.

Connect the nodes

Join Button Nodes

Click and drag the small square on the arduino node,
and attach it to the 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 Button Input

Press your button down and up again.

You should see the debug message in your browser.

Logical Components
of the whole circuit

  • Arduino In
    Button Input
    Receives an Arduino signal
    as a JavaScript message.
  • Function
    Conversion Function
    Converts the button signal
    into the correct message
    for the LED.
  • Arduino Out
    LED Output
    Sends the JavaScript message
    as an Arduino signal.

Circuit Arduino Nodes

Full Circuit with Arduino Nodes

Link the Arduino input and output directly.

The LED should be on when the button is pressed down.

Conversion Function

Full Circuit with Function Node

Link a function between the in node and the out node,
so we can manipulate the value being passed along.

Function Code

var buttonPressed = msg.payload;
var lightOn = context.lightOn;

if(lightOn === undefined) {
    lightOn = false;
}

if(buttonPressed === true) {
    if(lightOn === true) {
      lightOn = false;
    } else {
      lightOn = true;
    }
}

context.lightOn = lightOn;
msg.payload = lightOn;

return msg;

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

Shorter Function Code

var buttonPressed = msg.payload;
var lightOn = context.lightOn || false;

if(buttonPressed) {
    lightOn = !lightOn;
}

context.lightOn = lightOn;
msg.payload = lightOn;

return msg;

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.

Test your circuit

Your button should now act like an on/off switch

Binary Traffic Light

Challenge: Stop and Go

Add a green LED to your circuit to make a stop/go light.

Thumbs Up!

Cool, now let’s try something a little more complex…

Take me to the next chapter!

Loading...