Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / IoT / Arduino

Automation Solution Using Arduino

4.36/5 (8 votes)
30 Dec 2014CPOL3 min read 51.3K  
An automation solution using Arduino

Motivation

As an automation developer, you may find yourself required to automate products which are not designed for automation (no external interface).

For example, an old product which operates by pressing some buttons or switches.

In these cases, the solution must be physical (you need to replace the button press or switch key with something else.

A simple and cheap solution can be achieved by using Arduino controller.

In this post, I will show you how you can easily develop your own relay switch box and control it externally using your PC Com port.

What is Needed

  1. Arduino controller (I use Arduino mega 2560) - 15$ on eBay
  2. 8-Channel Relay Module for Arduino - 10$ on eBay
  3. 40 wire 2$ on eBay
  4. Arduino SDK - http://Arduino.cc/

Short Hardware Explanation

The Relay Module

The module has 8 relays, it needs to be fed by 5V from the Arduino mega board in order to operate.

It has 8 pins for controlling each relay separately, when applying logical “0” on each one of the relay ports the corresponding relay opens.

Image 1

Arduino Mega 2560

A microcontroller board based on the ATmega2560, it has 54 digital input/output pins, 16 analog inputs, 4 UARTs (hardware serial ports), a USB connection, a power jack and a reset button. It contains everything needed to support the microcontroller, simply connect it to a computer with a USB cable.

Image 2

Wiring the Setup

  1. VCC 5V from Arduino to VCC pin of the relay shield.
  2. GND from Arduino to GND pin of the relay shield.
  3. 8 wires from the Arduino board to the relay shield as follows:
    • Pin 52 from the Arduino to pin 1 on the relay shield
    • Pin 50 from the Arduino to pin 2 on the relay shield
    • Pin 48 from the Arduino to pin 3 on the relay shield
    • Pin 46 from the Arduino to pin 4 on the relay shield
    • Pin 44 from the Arduino to pin 5 on the relay shield
    • Pin 42 from the Arduino to pin 6 on the relay shield
    • Pin 40 from the Arduino to pin 7 on the relay shield
    • Pin 38 from the Arduino to pin 8 on the relay shield.

Image 3

Using the Code

Let’s define two simple commands:

  1. Open relay should be +x where x is the port number.
  2. Closing relay should be -x where x is the port number.

First, we define the relay port numbers as connected on the wire:

C++
#define RELAY1  52
#define RELAY2  50                    
#define RELAY3  48                        
#define RELAY4  46                       
#define RELAY5  44
#define RELAY6  42                     
#define RELAY7  40                        
#define RELAY8  38   

Then, we initialize the port as outputs:

C++
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
pinMode(RELAY5, OUTPUT);
pinMode(RELAY6, OUTPUT);
pinMode(RELAY7, OUTPUT);
pinMode(RELAY8, OUTPUT);

We need to assign logical “1” to the pins in order for all relay to be in off state:

C++
digitalWrite(RELAY1,HIGH);
digitalWrite(RELAY2,HIGH);
digitalWrite(RELAY3,HIGH);
digitalWrite(RELAY4,HIGH);
digitalWrite(RELAY5,HIGH);
digitalWrite(RELAY6,HIGH);
digitalWrite(RELAY7,HIGH);
digitalWrite(RELAY8,HIGH);

We set the serial port for 19200 baud and wait for connection:

C++
while (!Serial);
Serial.begin(19200);

On the loop method, we read full row from the serial port and detect if it is open or close command, for example:

sending  "-1" will cause relay 01 to close 
sending  "+1" will cause relay 01 to open 

For each command, we detect the status by detecting the first character of the command (+ or -) and the second character for the specific relay number. then we call “SetStatus” with the extracted parameters.

C++
void loop()
{
  String command;
  char charBuf[5];
  
  //waiting for data command
  if(Serial.available() > 0)
  {
    command = Serial.readStringUntil('\r');
    command.toCharArray(charBuf, 5);
  }
  else
    return;

  //-1 - off relay 01 
  //+1 - on relay 01
  
  if  (charBuf[0]=='+') //relay off
    SetStatus(true, charBuf[1]);
  else if (charBuf[0]=='-') //relay on
    SetStatus(false, charBuf[1]);  
  }

The SetStatus method gets two parameters:

  1. Boolean on – the first command character (+ or -) , open or close
  2. char relayNumber – the relay port to open or close

The complete code is as follows:

C++
#define RELAY1  52
#define RELAY2  50                    
#define RELAY3  48                        
#define RELAY4  46                       
#define RELAY5  44
#define RELAY6  42                     
#define RELAY7  40                        
#define RELAY8  38                        

void setup()
{    
  // Initialize the Arduino data pins for OUTPUT
  pinMode(RELAY1, OUTPUT);       
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);
  pinMode(RELAY4, OUTPUT);
  pinMode(RELAY5, OUTPUT);       
  pinMode(RELAY6, OUTPUT);
  pinMode(RELAY7, OUTPUT);
  pinMode(RELAY8, OUTPUT);
  
//setting all pins to high so that all relays are off at start
  digitalWrite(RELAY1,HIGH);    
  digitalWrite(RELAY2,HIGH);    
  digitalWrite(RELAY3,HIGH);  
  digitalWrite(RELAY4,HIGH);      
  digitalWrite(RELAY5,HIGH);    
  digitalWrite(RELAY6,HIGH);    
  digitalWrite(RELAY7,HIGH);    
  digitalWrite(RELAY8,HIGH);  
  
  //wait for serial connect from COM Port
  while (!Serial);
  Serial.begin(19200);
}

void loop()
{
  String command;
  char charBuf[5];
  
  //waiting for data command
  if(Serial.available() > 0)
  {
    command = Serial.readStringUntil('\r');
    command.toCharArray(charBuf, 5);
  }
  else
    return;

  //-1 - off relay 01 
  //+1 - on relay 01
  
  if  (charBuf[0]=='+') //relay off
    SetStatus(true, charBuf[1]);
  else if (charBuf[0]=='-') //relay on
    SetStatus(false, charBuf[1]);  
  }

  void SetStatus(boolean on,char relayNumber)
  {
    int relayState =0;//off 
    if (on)
      relayState =1;//on 

    switch (relayNumber) {
    case '1':
      digitalWrite(RELAY1,relayState);        
      break;
    case '2':
      digitalWrite(RELAY2,relayState);        
      break;
    case '3':
      digitalWrite(RELAY3,relayState);        
      break;
    case '4':
      digitalWrite(RELAY4,relayState);        
      break;
    case '5':
      digitalWrite(RELAY5,relayState);        
      break;
    case '6':
      digitalWrite(RELAY6,relayState);        
      break;
    case '7':
      digitalWrite(RELAY7,relayState);        
      break;
    case '8':
      digitalWrite(RELAY8,relayState);        
      break;
    }
  }

Testing the Code

  1. Connect the Arduino board to your PC
  2. Open a new project using the Arduino SDK and paste the code.
  3. Make sure to select the correct COM Port.
  4. Select the Arduino Mega Board 2560 from the board type.
  5. Download the code to the Arduino Mega Board.

Image 4 Image 5

  1. Open the serial monitor on the Arduino SDK.
  2. Select the proper baud rate (19200).
  3. Write "+1" or "-1" for opening/closing relay 1,
  4. Press the send button.

Image 6

That's it, you now have an automation solution operating on RS232.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)