RPi Node-Red: PIR(motion) Sensor Tutorial

Goal:

Learn how to wire a passive infrared sensor and read it’s state in Node-Red.

What You Will Learn:

  • Basic Circuit Prototyping
  • Basic Node-Red Programming

What You Need To Know:

Parts List:

  • One PIR
  • Breadboard
  • Some Jumper Wires
  • One 100 Ohm Resistor

What is a PIR?

A passive infrared sensor or PIR is able to detect movement by measuring the change of infrared radiation.  This PIR comes in a 3 pin package, if you remove the diffusion hood you can see that the pins are labeled “VCC”, “OUT”, and “GND”.  VCC means “voltage at the common collector” and normally refers to a power rail within a circuit, this sensor requires 5 volts. OUT is the pin we will read information from.  GND is the ground connection.

Getting Started:

Setting up the Hardware

I encourage gently taking off the diffusion cap on the PIR to view the pins labels.  GND goes to ground, VCC goes to 5V, and OUT is connected to a 100 ohm resistor and then to GPIO18.  

Setting up Node-Red

Start Node-Red and navigate to 127.0.0.1:1880.  Drag a Raspberry Pi input node and a Debug node into the flow area.

Double click on the Raspberry Pi input node to open its configuration menu.  Set the Pin to GPIO18.  Name the node “PIR”.

The debug node can be left with it’s default settings, wire the button node to the debug node and deploy the flow.

If everything has been put together correctly, the state under the PIR node should change from a 0 to a 1 if movement has been detected and then back to 0 after a period of time, inspect the debug tab to view a log of these state transitions.  You may have noticed the two components under the PIR with a plus pattern on them. These are adjustable potentiometers, they control the sensitivity of the PIR and the duration of the activated state.

Whats Next?

RPi Node-Red: Push Button + LED or Buzzer

Goal:

Combine the use of Raspberry Pi input and output nodes in Node-Red to control an LED or buzzer with a push button.

What You Will Learn:

  • Basic Circuit Prototyping
  • Basic Node-Red Programming

What You Need to Know:

Parts List:

  • At Least 1 LED
  • At Least 1 Resistor
  • Piezoelectric Buzzer
  • Some Jumper Wires
  • Breadboard

Optional

  • Needle Nose Pliers

Getting Started:

Setting up the Hardware

This wiring diagram is simply the combination of the individual LED, buzzer, and push button circuits.  Make sure that the LED has a resistor on it. Wire a pin on the buzzer to GPIO18, wire the positive leg of the LED to GPIO23, and wire one of the pins on the button to GPIO24.  You can wire all the grounds to a rail and then to the GPIO GND or the connections can be made individually.

If it is difficult to put the components into the breadboard without the legs bending try and use needle nose pliers to help push the pins in.

Setting up Node-Red

Start Node-Red and navigate to 127.0.0.1:1880.  Drag one Raspberry Pi input node and two Raspberry Pi output nodes into the flow area.

Double click the Raspberry Pi input node to open its configuration menu.  Set the Pin to GPIO24.  Set Resistor? to pulldown.  I set the Name to “button”.

Double click one of the Raspberry Pi output nodes to open its configuration menu.  This will be the LED so set the Pin to GPIO23.  Leave Type set to Digital output, Initialise the pin state to low.  Name the node “LED”

Double click the other Raspberry Pi output node to open it’s configuration menu.  Set Pin to GPIO24.  Set Type to PWM output.  Set Frequency to 100.  Name the node “buzzer”.

Wire each output node to the single input node and deploy the flow.

If everything works properly activating the button should turn on the LED and activate the buzzer.  To stop either output node from activating just remove its connection to the button node and redeploy the flow.

Whats Next?

  • What other combinations can you make with the items in your kit?

RPi Node-Red: Piezoelectric Buzzer Tutorial

Goal:

Turn a piezoelectric buzzer on and off using buttons in Node-Red.

What You Will Learn:

  • Basic Circuit Prototyping
  • Basic Node-Red Programming

What you Need to Know:

Optional

Parts List:

  • Piezoelectric Buzzer
  • Some Jumper Wires

What is a Piezoelectric Buzzer?

A piezoelectric buzzer is a buzzer that is activated with an oscillating electric signal.  This means to produce a tone the buzzer must be turned on and off many times per second. The faster the the buzzer is turned on and off the higher the pitch is produced.  We can achieve this using a pulse width modulated signal (PWM). With a PWM signal we can specify the frequency in hertz (cycles per second).

Getting Started:

Setting up the Hardware

Using  jumper wires connect one pin of the buzzer to GPIO18 and the other to GND (ground).

Setting up Node-Red

Start Node-Red and navigate to 127.0.0.1:1880.  Drag two inject nodes and a Raspberry Pi output node into the flow area.

Double click on the Raspberry Pi output node to open its configuration menu.  Set the Pin to GPIO18.  Set Type to PWM output.  Set Frequency to 100 Hz.  You can Name the node anything, I chose “buzzer”.

Double click one of the inject nodes.  Use the dropdown menu to change the Payload data type number, type a 1 in the textbox.  Name the inject node “on”.

Double click the unmodified inject node.  Use the drop down menu to change the Payload data type to number, except type a 0 in the text box.  Name this node “off”.

1 and 0 correspond for to values of a binary digit(bit) true and false, high and low, on and off respectively.  These are called Boolean values and are a fundamental data type in Computer Science.

Connect the inject nodes to the output node and then deploy the flow.

If everything has worked correctly pressing the “on” button should cause a sound to come from the buzzer and the “off” button should stop that sound.

Whats Next?

RPi Node-Red: Push Button Tutorial

Goal:

Learn how to wire a push button and read it’s current state in Node-Red

What You Will Learn:

  • Basic Circuit Prototyping
  • Basic Node-Red Programming

What You Need to Know:

Optional

Parts List:

  • 1 Push Button
  • Breadboard
  • Some Male-Female Jumper Wires

Optional

  • Multi Meter
  • Needle Nose Pliers

What is a Push Button?

A push button is a momentarily activated button.  When the button mechanism is fully depressed an internal electrical connection is made and this newly made connection can be read as the activation of the button.

What is a Breadboard?

A breadboard is a prototyping platform that allows the easy creation of circuits without having to solder.  Each row of the breadboard is connected internally (not across the middle break). This mean that putting something into position 1a and 1b connects them electrically together while something plugged into position 1a and 2a are electrically isolated.  

The power rails on the left and right hand side of the breadboard are connected in a column and can be an easy way to access ground of power if you choose to route it there.  The color red and by extension the red column is often used for power while blue is often associated with ground and thus used for a ground connection.

Getting Started:

Setting up the Hardware

Take care when wiring the button to the GPIO, you want to connect the correct pins.  In it’s 4 pin package only some pins are isolated in the inactivated state.  A pair of pins that come out of the same side of the button are isolated as well as diagonal partners.  A multi meter on the diode setting can also be used to test if there is a connection between pins in an activate/inactivated state. Once you have identified the correct pair of pins attach one to 5V and the other to GPIO4.

Additionally it can be difficult to get the short legs of the push button to correctly make contact with the breadboard, I find that using needle nose pliers to straighten the legs of the push button and then inserting them 2 at a time to be a successful method.

Setting up Node-Red

Start Node-Red and navigate to 127.0.0.1:1880.  Drag a Raspberry Pi input node and a Debug node into the flow area.

Double click on the Raspberry Pi input node to open its configuration menu.  Set the Pin to GPIO4.  Set Resistor? to pulldown.  You can Name the node anything, I chose “button”.

The debug node can be left with it’s default settings, wire the button node to the debug node and deploy the flow.

If everything has been put together correctly you should see the number under the button node in the Node-Red flow change from a 0 to a 1 when the button is activated and from 1 to 0 when the button is deactivated.  You should also be able to see a record of the button activation in the debug tab of Node-Red.

Whats Next?

RPi Node-Red: LED Tutorial

Goal:

Turn a light emitting diode(LED) on and off using a button in Node-Red.

What You Will Learn: 

  • Basic circuit prototyping
  • Basic Node-Red programming

What You Need to Know:

  • Node-Red Basics

Optional

Parts List:

  • At Least 1 LED
  • At Least 1 resistor
  • Breadboard
  • At least 2 male-female jumper wires

What is a Breadboard?

A breadboard is a prototyping platform that allows the easy creation of circuits without having to solder.  Each row of the breadboard is connected internally (not across the middle break). This mean that putting something into position 1a and 1b connects them electrically together while something plugged into position 1a and 2a are electrically isolated.  

The power rails on the left and right hand side of the breadboard are connected in a column and can be an easy way to access ground of power if you choose to route it there.  The color red and by extension the red column is often used for power while blue is often associated with ground and thus used for a ground connection.

What is a Resistor?

A resistor is an electrical component that has a specific value of resistance measured in ohms.  Resistors are used to restrict the amount of current in a circuit or facilitate a drop in voltage.

What is an LED?

An LED is a light emitting diode.  Diode’s are a class of electronic components that are designed to only allow current to pass through them in one direction.  This means that LEDs are polar meaning they have an anode (positive) and a cathode (negative). Current must flow into the anode and the cathode must be attached to ground.  

There are several methods of identifying the anode and cathode:

  • Look at the lengths of the pins, If the LEDs’ pin have not been modified the longer pin is the anode and the shorter pin is the cathode
  • Look at the sides of the LED a portion of the case will be flat the pin on that flat side is the cathode 
  • Look inside of the bulb, each pin is connected to a flat piece of metal internally.  The pin that is attached to the larger piece is the cathode

An LED must have a current limiting resistor between it and ground or between it and power.  An unprotected LED will pull more and more current until it burns itself out. The value of the resistor will have an effect on the LEDs’ luminosity.  The higher the value of resistance the resistor has the less current will get to the LED and thus the dimmer the bulb will shine. The amount of current can be calculated in milliamps with Ohm’s Law, using the value in ohm’s for resistance

\text{current} = \frac{\text{voltage}}{\text{resistance}}

Getting Started:

Setting up the Hardware

If you are following the wiring diagram make sure to attach the cathode (straight leg in the diagram) of the LED to a resistor and then to GND (ground).  Attach the anode (leg with the kink) to GPIO #18.

Setting up Node-Red

Start Node-Red and navigate to 127.0.0.1:1880.  Drag two inject nodes and a Raspberry Pi output node into the flow area

Double click on the Raspberry pi output node to open its configuration menu.  Set the Pin to the same pin you physically connected the LED to. If you are following the wiring diagram this is GPIO18.  Leave the Type as Digital Output.  We can also Name the node, since I used a green LED I named it “Green led”, this follows a good programming practice of accurately and succinctly naming variables.

Double click one of the inject nodes.  Use the drop down menu to change the Payload data type number, type a “1” in the text box.  Name the inject node “on”.

Double click the unmodified inject node.  Use the drop down menu to change the Payload data type to number, except type a “0” in the text box.  Name this node “off”.

1 and 0 correspond for to values of a binary digit(bit) true and false, high and low, on and off respectively.  These are called boolean values and are a fundamental data type in Computer Science.

Connect the inject nodes to the output node and then deploy the flow.

The green square under the Green LED node tells us what state the LED is in, 1 being on and 0 being off.  Injecting a different value will change the state from off to on or from on to off.

Whats Next?

RPi Node-Red: LED+Buzzer+PIR(motion)

This tutorial will set up the pir(motion) sensor to trigger a LED and or buzzer.

Wiring diagram:

The leftmost pin of the PIR is connected to ground, the middle pin is connected to a 100 ohm resistor then to GPIO4 – 7, the rightmost pin is connected to +5v.

The positive leg of the LED is connected to 12 – GPIO18, the negative leg is connected to a 1k ohm resistor and then to ground.

One leg of the buzzer is connected to 16 – GPIO23 and the other leg to ground.


Start node-red and open a web browser at 127.0.0.1:1880

Drag a rpi-gpio in node and a rpi-gpio out node into the flow area.

Open the rpi-gpio in node settings; set the pin to the pin the middle leg of the pir is wired to(GPIO4 – 7 if following the wiring diagram).

Open the rpi-gpio out node settings; set the pin to the same pin the LED is wired to(12 – GPIO18 if following the wiring diagram), check the “Initialize pin state” box and set to “low”.

Link the rpi-gpio in and rpi-gpio out nodes together and deploy.

The LED should turn on when the pir detects movement for a period of time before turning off again, there is a delay period where the pir will not activate the LED after turning it off.  Both of these periods(LED activation time, delay between deactivation and next detection) can be tweaked with the knobs located on the sensor.


Now let’s add in the buzzer.

Into the flow area drag in a rpi-gpio out node and connect it to the rpi-gpio in node.

Open the new rpi-gpio out node settings; set the pin to the same pin the buzzer is connected to(16 – GPIO23 if following the wiring diagram), change “Type” to “PWM output”, and set “Frequency” to “100”.

Deploy the flow, now when the pir detects motion it will always sound the buzzer in addition to lighting the LED.

RPi Node Red + Camera

RPi Node-Red: Intro + Sense Hat

To start Node-RED, you can either:

  • On the Desktop, select Menu > Programming > Node-RED.
  • Or run node-red-start in a new terminal window.
  • You should now the able to access Node-Red locally in your web browser: http://127.0.0.1:1880

Later on if you want to stop node red

  • From the terminal run the command node-red-stop

Node-Red and the Sense HAT

  • Once you have started node red you need to open up your web browser and go to ……
  • The website at this address is being hosted locally on your Raspberry Pi – but can be accessed remotely as well
  • On the left nodes pane scroll all the way down to find the “Raspberry Pi” section
  • Drag Sense-Hat output node into blank window area
  • Notice if you select this node and click on the “info” tab on the right of the window you will find hints about its use
  • In the left nodes pane select and drag into the main window the “Inject Node”
  • Double click on the inject node to access its configuration menu
  • Set the inject node payload to 1,1,red
  • Save the inject node
  • Connect the inject node with the sense-hat output
  • Click on the ● to the right of the inject node and drag it to the ● and the left of the Sense HAT output node
  • Click Publish at the top right of the window
  • Click the Inject button on the left side of the Inject node.

Example of Node-Red display showing the status of an off grid power system in Hawaii:

https://www.10bt.org:1880/ui/#/0

https://hilo90mhz.com/generator/

RPi GPIO + Scratch 2.0

In this activity we will use Scratch 2.0 to light LED bulbs and sense a button push.

Begin by lighting a LED bulb with an always on 3.3 v pin and a resistor. If this isn’t working, check your connections or see the post on getting started with GPIO. Change the pin for the 3.3 volt to pin 17 on the GPIO board and open Scratch 2.0.  Follow the steps outlined below.