Project tutorial
Arduino with NeoPixel Optocouplers Controlling Many Relays

Arduino with NeoPixel Optocouplers Controlling Many Relays © GPL3+

How to control many actuators like relays from one Arduino output pin? Use a strip of NeoPixel LEDs with LDRs as self-made optocouplers!

  • 6,957 views
  • 9 comments
  • 41 respects

Components and supplies

11842 01a
SparkFun LilyPad Rainbow LED (strip of 7 colors)
or similar (cut from a Led strip 5m/roll DC5V ws2812b 300led Individually Addressable 60leds/m 5050 RGB), various suppliers
×1
Mfr 25frf52 10k sml
Resistor 10k ohm
27 k Ohm
×4
Adafruit industries ada161 image 75px
Photo resistor
also called LDR (Light Dependent Resistor) https://en.wikipedia.org/wiki/Photoresistor
×4
Rg relay a
RobotGeek Relay
or https://www.conrad.nl/p/relaiskaart-5vdc-4-voudig-voor-arduino-raspberry-pi-etc-095841
×4
LED Lamp
×1
A000066 iso both
Arduino UNO & Genuino UNO
any Arduino type can be used, in this application I used a Node MCU ESP32
×1

Necessary tools and machines

09507 01
Soldering iron (generic)
Common Tools
as will be available in a normal household/workshop

Apps and online services

About this project

This project is about controlling multiple relays or other actuators from a single Arduino output pin.

This solution can be used for typical situations where the amount of available Arduino output pins is insufficient and more actuators such as relays need to be controlled in parallel from a single output pin.

The principle of operation is based on using a LED strip of a number of NeoPixels (SMD5050 LEDs with WS2812B controller chips). Every pixel is put together with an LDR (Light Dependent Resistor), thus creating a DIY optocouplers (as many as you want, only limited by the LED strip length, e.g. 300 pixels, and available 5V power supply). In this way a serial to parallel conversion is created (from 1 to many)

Every individual LED is addressed from one and the same Arduino output pin. Each LDR (in series with a 27kOhm resistor), is connected to a 5V relay. In this way many NeoPixel/LDR combinations can be controlled from 1 Arduino output PIN using the Adafruit NeoPixel library. Instead of relays also other actuator devices could be connected, using the LDRs as an input to whatever circuit.

For this project, which is part of a bigger plan, an ESP32 (NodeMCU) is used; however a normal Arduino Uno (or almost any other model) will do the job.

Step 1: Making the NeoPixel Optocouplers

The DIY optocoupler is made with the following materials:

  • a piece of LED strip consisting of 10 WS2812 LEDs (cut from a longer strip) only 4 are actually used in the project
  • a piece of dark grey foam
  • a strip of breadboard
  • black duck tape

Note: the separation of the dots equals the separations of the LEDs.

It is important to separate the light of each individual LED from the LDRs of the others to avoid "light cross talk." It turned out that in practice this is almost impossible and therefore the amount of light emitted by each LED in the "on" state has been set to low levels in the software.

The LED strip used is 10 LEDs long but only 4 pixels are used in combination with 4 LDRs in this project (to keep it simple)

Step 2: The Electronic Setup

In the breadboard set up, I used an ESP32s (NodeMCU), however any Arduino can do the job.

The circuit diagram (made with Fritzing) is as follows:

In practice this looks as follows:

As you can see only 1 output pin is used to control 4 relays in parallel. This number can go up as high as 300! (as many LEDs as available in a 5 m long strip).

The ESP32 device works at 3.3 Volt levels (an on-board 3.3V voltage regulator), while the LED strip with 5050 leds runs on 5 V. The ESP32 is fed with 5 V power (via the USB port from a 5V adaptor or 5V powerbank). The NeoPixel LEDs get the 5V supply directly from the 5 volt pin of the ESP32 and the relays used, are also 5V types.

In this example 4 optocoupler circuits are used controlling 4 relays connected to one 230V lamp each.

The output pin used is GPIO PIN 21 and the relays are controlled via pixel numbers 1, 2, 3, 4.

Step 3: Making the Software

The loop function in the Arduino Sketch is simple and consists of a number of "for"-loops to show different patterns of switching the lamps by controlling each of the relays.

To control a particular relay the following function is called from within the loop code:

void ControlRelais (int RelaisNo, bool RelaisState)  {
   strip.setPixelColor(RelaisNo, RelaisState*15, RelaisState*15, RelaisState*15);      // turn on/off the LED that belongs to RelaisNo
   strip.show();
   Serial.print(" RelaisNo "); Serial.print(RelaisNo); Serial.print(" = "); Serial.println(RelaisState);
   }

In fact all this function does is either switch on a particular LED or switch off.

A LED address corresponds with the respective relay number. The LEDs are lit at a low light level, just enough to trigger the relay via the LDR, and thus avoiding light pollution (also referred to above as "light cross talk".

The result of all the effort and the particular sketch, is shown in the following movie:

showing how you can control a number of relays from 1 Arduino pin

Code

Sketch for controlling multiple relays via selfmade NeoPixel OptocouplersArduino
This sketch show how to control an number of relays via multiple optocouplers made with NeoPixels controlled from 1 ARDUINO output pin
/*
  This code for controlling multiple Relais or other actuators from a single Arduino output pin, has been developed and produced by Pierre Pennings (December 2018)
  This solution can be used for typical situations where the amount of available Arduino output pins is insufficient and more actuators such as Relais need to be controlled in parallel
  The principle of operation is based on using a strip of a number of Neopixels (SMD5050 LEDs with WS2812B controller chips)
  Every pixel is put together with an LDR (Light Dependent Resistor), thus creating a DIY Optocoupler
  Every individual LED is addressed from one and the same ARDUINO output pin and the LDR (in series with a 27kOhm resistor), is connected to a 5V Relais
  In this way many Neopixel/LDR combinations can be controlled from 1 Arduino output PIN using the Adafruit Neopixel library.
  
  For this Project, which is part of a bigger plan, an ESP 32 (NodeMCU) is used, however a normal ARDUINO UNO (or almost any other model) will do the job
  (of course the settings in the code will need to be adjusted, e.g. due to different Pin allocations)
  The ESP 32 device works at 3.3 Volt levels (an on-board 3.3V voltage regulator), while the LED strip with 5050 leds runs on 5 V
  The ESP 32 is fed with 5 V power (via the USB port from a 5V adaptor or 5v powerbank)
  The Neopixel LEDs get the 5V supply directly from the 5 volt pin of the ESP 32 and the Relais used are also 5V types.
  In this example a LED strip of 10 LED's is used, for simplicity reasons only 4 optocoupler circuits are made controlling 4 Relais connected to one 230V LEDLamp each.
  The output pin used is GPIO PIN 21 and the RELAIS are controlled via Pixel numbers 1,2,3,4
    
  This code is licensed under GPL3+ license.
*/

#include <Adafruit_NeoPixel.h>
#define NUM_LEDS  10


///////////////////////////////////////////////// initialise the GPIO pin
const int RelaisPin = 21;                           // pin 21 sends control data (0 -3.3 V) to the Relais
int RelaisNo = 0;                                   // Variable for the applicable Relais to be controlled
bool RelaisState = false;
int r = 0;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, RelaisPin, NEO_RGB + NEO_KHZ800);

/////////////////////////////////////////////////// the setup code that follows, will run once after "Power On" or after a RESET
void setup() {
  Serial.begin(115200);
  
  pinMode(RelaisPin, OUTPUT);                    // Initializes the RelaisPin as output
  strip.begin();                                 // Initialize all LEDs to "off"
  
  for (int t = 0; t < 10 ; t++)
    {
    strip.setPixelColor(t, 15, 15, 15);          // After Power On all LEDs of the strip are tested once
    strip.show();                                // note that the order of colors of the WS2812 LED strip is R,G,B 
    delay (500);
    strip.setPixelColor(t, 0, 0, 0);             // And back to off
    }
}

/////////////////////////////////////////////////// the loop code that follows, will run repeatedly until "Power Off" or a RESET
void loop(){
  
 for (int r = 1; r < 5 ; r++)                     // switch on the 4 Relais one after another counting form 1 to 4
    {
    delay (500);
    ControlRelais (r , true);
    delay(500);
    ControlRelais (r , false);                                  
    }

for (int k = 4; k > 0 ; k--)                      // switch on the 4 Relais one after another counting form 4 to 1
   {
    delay (500);
    ControlRelais (k , true);
    delay(500);
    ControlRelais (k , false);
   }                          

for (int r = 1; r < 5 ; r++)                      // switch on the 4 Relais in a patern
   {
    for (int k = 4; k > 0 ; k--)
    {
    delay (500);
    ControlRelais (r , true);
    ControlRelais (k , true);
    delay(500);
    ControlRelais (r , false);
    ControlRelais (k , false);
    }                                   
  }
}
//////////////////END of LOOP////////////////////////////////////////////////////////////  


/////////////////////////////////////////////////// Hereafter follows the Function for controlling the Relais (called from within the loop)

void ControlRelais (int RelaisNo, bool RelaisState)  {

    strip.setPixelColor(RelaisNo, RelaisState*15, RelaisState*15, RelaisState*15);      // turn on/off the LED that belongs to RelaisNo
    strip.show();
    Serial.print(" RelaisNo "); Serial.print(RelaisNo); Serial.print(" = "); Serial.println(RelaisState);
    }

Schematics

Fritzing Diagram NeoPixel Optocouplers with relays
Set up for controlling multiple relays or other actuators from a single Arduino pin
neopixel_optocoupler_relais__hNIrhGIMtD.fzz

Comments

Similar projects you might like

VU Meter on Steroids: Arduino Nano and NeoPixel WS2812B

Project in progress by Team WannaDuino

  • 22,484 views
  • 91 comments
  • 83 respects

RFID Desk Lamp

Project tutorial by Kutluhan Aktar

  • 1,642 views
  • 0 comments
  • 3 respects

Controlling a Philips Hue via a Arduino

Project tutorial by Jim

  • 6,883 views
  • 2 comments
  • 7 respects

Touch Sensor and Sound Sensor Controlling AC/DC Lights

Project showcase by Anbumachi

  • 1,161 views
  • 2 comments
  • 5 respects

NeoPixel LEDs: Arduino Basics

by Alex Glow

  • 43,195 views
  • 1 comment
  • 46 respects

Two Mode Robot Controlling through Android and Windowsphone8

Project tutorial by Anas Siddiqui

  • 16,314 views
  • 9 comments
  • 48 respects
Add projectSign up / Login