Pedro52
Published © GPL3+

Arduino with NeoPixel Optocouplers Controlling Many Relays

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

IntermediateFull instructions provided15,718
Arduino with NeoPixel Optocouplers Controlling Many Relays

Things used in this project

Hardware components

LilyPad Rainbow LED (strip of 7 colors)
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
Resistor 10k ohm
Resistor 10k ohm
27 k Ohm
×4
Photo resistor
Photo resistor
also called LDR (Light Dependent Resistor) https://en.wikipedia.org/wiki/Photoresistor
×4
RobotGeek Relay
RobotGeek Relay
or https://www.conrad.nl/p/relaiskaart-5vdc-4-voudig-voor-arduino-raspberry-pi-etc-095841
×4
LED Lamp
×1
Arduino UNO
Arduino UNO
any Arduino type can be used, in this application I used a Node MCU ESP32
×1

Software apps and online services

Movie

Hand tools and fabrication machines

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

Story

Read more

Schematics

Fritzing Diagram NeoPixel Optocouplers with relays

Set up for controlling multiple relays or other actuators from a single Arduino pin

Code

Sketch for controlling multiple relays via selfmade NeoPixel Optocouplers

Arduino
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);
    }

Credits

Pedro52

Pedro52

6 projects • 47 followers

Comments