Devices & Components
Arduino Uno Rev3
Resistor 10k ohm
RobotGeek Relay
Photo resistor
LilyPad Rainbow LED (strip of 7 colors)
LED Lamp
Hardware & Tools
Common Tools
Soldering iron (generic)
Software & Tools
Movie
Project description
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
1/* 2 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) 3 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 4 The principle of operation is based on using a strip of a number of Neopixels (SMD5050 LEDs with WS2812B controller chips) 5 Every pixel is put together with an LDR (Light Dependent Resistor), thus creating a DIY Optocoupler 6 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 7 In this way many Neopixel/LDR combinations can be controlled from 1 Arduino output PIN using the Adafruit Neopixel library. 8 9 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 10 (of course the settings in the code will need to be adjusted, e.g. due to different Pin allocations) 11 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 12 The ESP 32 is fed with 5 V power (via the USB port from a 5V adaptor or 5v powerbank) 13 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. 14 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. 15 The output pin used is GPIO PIN 21 and the RELAIS are controlled via Pixel numbers 1,2,3,4 16 17 This code is licensed under GPL3+ license. 18*/ 19 20#include <Adafruit_NeoPixel.h> 21#define NUM_LEDS 10 22 23 24///////////////////////////////////////////////// initialise the GPIO pin 25const int RelaisPin = 21; // pin 21 sends control data (0 -3.3 V) to the Relais 26int RelaisNo = 0; // Variable for the applicable Relais to be controlled 27bool RelaisState = false; 28int r = 0; 29 30Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, RelaisPin, NEO_RGB + NEO_KHZ800); 31 32/////////////////////////////////////////////////// the setup code that follows, will run once after "Power On" or after a RESET 33void setup() { 34 Serial.begin(115200); 35 36 pinMode(RelaisPin, OUTPUT); // Initializes the RelaisPin as output 37 strip.begin(); // Initialize all LEDs to "off" 38 39 for (int t = 0; t < 10 ; t++) 40 { 41 strip.setPixelColor(t, 15, 15, 15); // After Power On all LEDs of the strip are tested once 42 strip.show(); // note that the order of colors of the WS2812 LED strip is R,G,B 43 delay (500); 44 strip.setPixelColor(t, 0, 0, 0); // And back to off 45 } 46} 47 48/////////////////////////////////////////////////// the loop code that follows, will run repeatedly until "Power Off" or a RESET 49void loop(){ 50 51 for (int r = 1; r < 5 ; r++) // switch on the 4 Relais one after another counting form 1 to 4 52 { 53 delay (500); 54 ControlRelais (r , true); 55 delay(500); 56 ControlRelais (r , false); 57 } 58 59for (int k = 4; k > 0 ; k--) // switch on the 4 Relais one after another counting form 4 to 1 60 { 61 delay (500); 62 ControlRelais (k , true); 63 delay(500); 64 ControlRelais (k , false); 65 } 66 67for (int r = 1; r < 5 ; r++) // switch on the 4 Relais in a patern 68 { 69 for (int k = 4; k > 0 ; k--) 70 { 71 delay (500); 72 ControlRelais (r , true); 73 ControlRelais (k , true); 74 delay(500); 75 ControlRelais (r , false); 76 ControlRelais (k , false); 77 } 78 } 79} 80//////////////////END of LOOP//////////////////////////////////////////////////////////// 81 82 83/////////////////////////////////////////////////// Hereafter follows the Function for controlling the Relais (called from within the loop) 84 85void ControlRelais (int RelaisNo, bool RelaisState) { 86 87 strip.setPixelColor(RelaisNo, RelaisState*15, RelaisState*15, RelaisState*15); // turn on/off the LED that belongs to RelaisNo 88 strip.show(); 89 Serial.print(" RelaisNo "); Serial.print(RelaisNo); Serial.print(" = "); Serial.println(RelaisState); 90 } 91
Downloadable files
Fritzing Diagram NeoPixel Optocouplers with relays
Set up for controlling multiple relays or other actuators from a single Arduino pin
Fritzing Diagram NeoPixel Optocouplers with relays
Fritzing Diagram NeoPixel Optocouplers with relays
Set up for controlling multiple relays or other actuators from a single Arduino pin
Fritzing Diagram NeoPixel Optocouplers with relays
Comments
Only logged in users can leave comments