Nicola Wrachien
Published © GPL3+

12-Channel USB-Powered Relay Board

A very simple USB board that allows you to control 12 relays for lighting, open electric gates, etc.

BeginnerFull instructions provided2 hours2,076
12-Channel USB-Powered Relay Board

Things used in this project

Hardware components

ITACA Innovation uChip
Available on https://shop.itaca-innovation.com
×1
onsemi BC337-40
×12
3.3 kOhm, 1/4W, 5% resistor (TH)
This resistor value isn't critical. Can be lowered or increased (up to 15k is fine).
×12
1.5 kOhm, 1/4W, 5% resistor (TH)
Adjust it to achieve your desired luminosity. Our LEDs were very bright. Much smaller value (e.g. 330 Ohm) might be required for other kinds of LED. We used precision resistors just because we had a reel of them (and no 1.5 kOhm 5%!).
×12
100 uF Capacitor, WV >= 6.3V
×1
270 Ohm, 1/4W, 5% resistor (TH)
×1
2-poles screw terminal, 5.08 mm spacing
The 13th is required only if you want to externally provide 5V
×13
10x16 cm perfboard
You just need 6.35x16 cm, but that's not a standard board! You can also use a PCB.
×1
5 mm LED: Red
5 mm LED: Red
Any LED.
×1
3 mm LED: Red
3 mm LED: Red
In our project we used a .1.8 mm LED.
×12
1N4148 – General Purpose Fast Switching
1N4148 – General Purpose Fast Switching
×12
TE Connectivity PCN-105D3MHZ relay
×12
KS-01Q-01 pushbutton
Optional user buttons. You can modify the sketch to have them do something!
×2
HEX STANDOFF M3 NYLON 6MM (e.g. CBMFTS210A)
×4
Micro-USB to USB Cable (Generic)
Micro-USB to USB Cable (Generic)
×1
16-PIN DIP IC SOCKET
×1
HEX nylon NUT
×4

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Schematics

Schematics in PNG format.

Example of LLayout

Complete KiCAD project

Code

Sketch for the relay board

C/C++
Create a new sketch on Arduino, copy and paste, upload, enjoy!
/* Simple USB relay controller for uChip. Copyright 2019 Nicola Wrachien www.next-hack.com 
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.   
 * 
 ****************************************************************************************
 * USAGE:
 * Open a serial connection, and first write the relays you want to operate (A = relay 0, 
 * B= relay 1, and so on), and then write 1 to switch them on, or 0 to switch them off.
 * Any other character will abort the command.
 * 
 * Examples (remove quotes "):
 * "ABC1"     // turns on relays 0 (A), 1 (B), 2 (C) 
 * "BD0"      // turns off relays 1 (B) and 3 (D). 
 * (assuming you have 12 relays):  "AQ1"    (aborts the command once Q has been received. 
 *                                1 does nothing, because no relay was selected after Q)
 * "ABC2": aborts the command, as 2 is not a valid character.
 */
uint32_t time;
uint32_t relays = 0;
//const uint8_t pins[] = {1, 2, 3, 4, 5, 6, 10, 9, 7, 15, 12 , 11};
const uint8_t pins[] = {1, 2, 3, 4, 5, 6, 7, 15, 12,  11, 10 , 9};

void setup() 
{
  // set all outputs default low.
  for (int i = 0; i < sizeof(pins); i++)
  {
    pinMode(pins[i], OUTPUT);  
    digitalWrite(pins[i], 0);
  }
  // set output to 5V
  REG_PORT_DIRSET0 = PORT_PA15;
  REG_PORT_OUTSET0 = PORT_PA15;  // Quickly enable 5V buck regulation!
  // open serial and wait till it's ready!
  SerialUSB.begin(9600);
  while (!SerialUSB);
  pinMode(LED_BUILTIN, OUTPUT);
}
void loop() 
{
  if (millis() - time > 1000)
  {
    time = millis();
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  }
  // now get some data and process it
  if (SerialUSB.available() > 0)
  {
    char c =SerialUSB.read();
    if (c >= 'A' && c < 'A' + sizeof(pins))       
    {
      relays |= 1 << (c - 'A');   // set to one the bit corresponding to the relay we want to turn off/on
    }
    else if (c == '0')
    { // we want to turn off all the previously selected relays.
      for (int i = 0; i < sizeof(pins); i++)
      {
        if (relays & (1 << i))
          digitalWrite(pins[i], 0);
      }
      relays = 0; // deselect relays
    }
    else if (c == '1')
    {
      for (int i = 0; i < sizeof(pins); i++)
      {
        if (relays & (1 << i))
          digitalWrite(pins[i], 1);
      }      
      relays = 0; // deselect relays
    }
    else // any other chars... cancel the command
      relays = 0;    
  }
}

Credits

Nicola Wrachien

Nicola Wrachien

3 projects • 2 followers
I was born, then I got my master, a PhD. I forgot about all what's in between.

Comments