Categories

5 Volt 4 Channel Arduino Relay Module example

In this example we will take a look at another relay module, this time this one is called the 5 Volt 4 Channel Arduino Relay Module , here is a picture of the module

This relay module provides four relays that are rated for 7A at either 28VDC or 10A at 125VAC. Each relay hasĀ  a Normally Open (NO) and a Normally Closed (NC) contact. This module could be used in various automation projects such as switching lights and motors.

You can see that each relay is controlled by a corresponding input pin called In1 to IN4, there are also LEDs that show if the input is low or high. Another benefit is that each relay is optically isolated using an LTC-817.

 

Module Schematic

I found this schematic online for the module

 

Schematic

The following schematic shows how to connect the module to an Arduino, I have only shown one device (lightbulb) connected to one relay

 

Code

Here is some test code

int RelayControl1 = 4;    // Digital Arduino Pin used to control the motor
int RelayControl2 = 5;  
int RelayControl3 = 6;  
int RelayControl4 = 7;  
 
 
void setup()  
{   
  Serial.begin(9600);
  pinMode(RelayControl1, OUTPUT);
  pinMode(RelayControl2, OUTPUT);
  pinMode(RelayControl3, OUTPUT);
  pinMode(RelayControl4, OUTPUT);
}
 
 
void loop()  
{
 
  digitalWrite(RelayControl1,HIGH);// NO1 and COM1 Connected (LED on)  
  delay(1000);
  digitalWrite(RelayControl1,LOW);// NO1 and COM1 disconnected (LED off)  
  delay(1000);
  digitalWrite(RelayControl2,HIGH);
  delay(1000);
  digitalWrite(RelayControl2,LOW);
  delay(1000);
  digitalWrite(RelayControl3,HIGH);
  delay(1000);
  digitalWrite(RelayControl3,LOW); 
  delay(1000);     
  digitalWrite(RelayControl4,HIGH);  
  delay(1000);
  digitalWrite(RelayControl4,LOW); 
  delay(1000);
}

 

Links
1PC/LOT 5V 4-Channel Relay Module Shield for Arduino ARM PIC AVR DSP Electronic 5V 4 Channel Relay Newest

Share

Lowpower library example for Arduino

There are some situations where you need to save power when you are creating battery powered projects, the lowpower library is one solution (we will look at others later)

 

Code

You will need to install the Lowpower library – https://github.com/rocketscream/Low-Power

This example will flash an LED connected to pin 4 on and off but does not use the delay function

#include "LowPower.h"
int led = 4;
void setup()
{
pinMode(led, OUTPUT);
}
void loop()
{
digitalWrite(led,HIGH);
// ATmega328P, ATmega168
LowPower.idle(SLEEP_1S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF, SPI_OFF, USART0_OFF, TWI_OFF);
digitalWrite(led,LOW);
LowPower.idle(SLEEP_2S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF, SPI_OFF, USART0_OFF, TWI_OFF);
}
Share

PS2 JOYSTICK SHIELD V2.0 code example

In this code example we will take a look at the PS2 JOYSTICK SHIELD V2.0.

The X-axis of the joystick is connected to pin A0 of Arduino, the Y-axis to pin A1, power supply to 5V and ground to GND.

There are four buttons on the PS2 JOYSTICK SHIELD . D3 should be connected to digital pin 3; D4 to digital pin4; D5 to digital pin 5; D6 to digital pin 6. The other small button is reset button.
Generally, PS2 rocker can be used for controlling small vehicles. It is mainly comprised of two 10K potentiometers and a button switch. The five ports are VCC, X, Button, Y and GND.

 

Code

char button0=3;
char button1=4; 
char button2=5; 
char button3=6;
char joybutton=2;
 
void setup(void)
{
  //set all as inputs
  pinMode(joybutton, INPUT); 
  pinMode(button0, INPUT);      
  pinMode(button1, INPUT);     
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
 
  //set all high
  digitalWrite(joybutton, HIGH);
  digitalWrite(button0, HIGH);  
  digitalWrite(button1, HIGH);  
  digitalWrite(button2, HIGH);
  digitalWrite(button3, HIGH);
 
  Serial.begin(9600); 
}
 
void loop(void)
{
  Serial.print(analogRead(0));
  Serial.print(",");
  Serial.print(analogRead(1));
  Serial.print(",");
 
  //Read the value of the buttons and print it on the serial port.
  Serial.print(digitalRead(button0)); 
  Serial.print(digitalRead(joybutton));    
  Serial.print(digitalRead(button0)); 
  Serial.print(digitalRead(button1));  
  Serial.print(digitalRead(button2));  
  Serial.println(digitalRead(button3)); 
 
  delay(1000);
}

 

Links
PS2 JOYSTICK SHIELD V2.0 for Arduino

Share

Arduino relay shield controlled by remote control example

In this example we will connect a 4 channel relay shield to an Arduino Uno and control it via an infrared remote control. We will use keys 1 to 4 to control relay 1 to 4 and also use key 0 to switch off relays.

We will code this so that if you press the 1 key the Relay will be activated, if you then press the 1 key again the relay will be switched off.

 

Image

Here is a picture of what we created, the Infrared reciever was a module that I purchased, link at the bottom

Code

The code needs the IRremote remote library to be installed. Then you need to find the codes for the various buttons on the remote control

#include <IRremote.h>
int RECV_PIN = 3;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX);
irrecv.resume();
}
}

Now upload the code above and open the serial monitor, when you press each button you should see the hex code for the keys you want to use, here is an example

0xFF30CF
0xFF18E7

Lets look at some of the code, remember that Relay 1 is pin 7 and so on

int RelayControl1 = 7;    // Digital Arduino Pin used to control the motor
int RelayControl2 = 6;  
int RelayControl3 = 5;  
int RelayControl4 = 4;

We then define some variables to hold the state of each relay, here is an example
boolean state1 = true;

The next piece of interesting piece of code is the handling of each button press, you can see we detect the hex code, if its match we then check the value of the state1
variable, if its true then the relay is switch on and the state is changed. This of course means that if the button is pressed again the value of the state 
variable will be false and we switch the relay off, and set the state vartiable to true.

      case 0xFF30CF:  
        Serial.println("1");
        if(state1==true)
        {
          digitalWrite(RelayControl1,HIGH);// NO1 and COM1 Connected (LED on)  
          state1=false; //change the state
        }  
        else
        {
          digitalWrite(RelayControl1,LOW);// NO1 and COM1 disconnected (LED off)  
          state1=true; //change the state
        }      
        break;


Here is t eh complete code example
#include <IRremote.h>
 
int RECV_PIN = 3;
int RelayControl1 = 7;    // Digital Arduino Pin used to control the motor
int RelayControl2 = 6;  
int RelayControl3 = 5;  
int RelayControl4 = 4;  
boolean state1 = true;
boolean state2 = true;
boolean state3 = true;
boolean state4 = true;
IRrecv irrecv(RECV_PIN);
decode_results results;
 
void setup()  
{
    Serial.begin(9600);
  pinMode(RelayControl1, OUTPUT);
  pinMode(RelayControl2, OUTPUT);
  pinMode(RelayControl3, OUTPUT);
  pinMode(RelayControl4, OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
}
 
 
void loop()  
{
  if (irrecv.decode(&results))
  {
    switch(results.value)
    {
      case 0xFF30CF:  
        Serial.println("1");
        if(state1==true)
        {
          digitalWrite(RelayControl1,HIGH);// NO1 and COM1 Connected (LED on)  
          state1=false; //change the state
        }  
        else
        {
          digitalWrite(RelayControl1,LOW);// NO1 and COM1 disconnected (LED off)  
          state1=true; //change the state
        }      
        break;
 
      case 0xFF18E7:  
        Serial.println("2"); 
        if(state2==true)
        {
          digitalWrite(RelayControl2,HIGH);// NO2 and COM2 Connected (LED on)
          state2=false;
        }
        else
        {
          digitalWrite(RelayControl2,LOW);// NO2 and COM2 disconnected (LED off)  
          state2=true; //change the state          
        }
        break;
 
      case 0xFF7A85:  
        Serial.println("3"); 
        if(state3==true)
        {
          digitalWrite(RelayControl3,HIGH);// NO3 and COM3 Connected (LED on)
          state3=false;
        }
        else
        {
          digitalWrite(RelayControl3,LOW);// NO3 and COM3 disconnected (LED off)  
          state3=true;           
        }
        break;
 
      case 0xFF10EF: 
        Serial.println("4"); 
        if(state4==true)
        {
          digitalWrite(RelayControl4,HIGH);// NO4 and COM4 Connected (LED on)
          state4=false;
        }
        else
        {
          digitalWrite(RelayControl4,LOW);// NO4 and COM4 disconnected (LED off)  
          state4=true;          
        }
        break;
 
      case 0xFF6897:  
        Serial.println("OFF"); 
        digitalWrite(RelayControl1,LOW);
        digitalWrite(RelayControl2,LOW);
        digitalWrite(RelayControl3,LOW);
        digitalWrite(RelayControl4,LOW);
        state1 = true;
        state2 = true;
        state3 = true;
        state4 = true;
        break;
 
      default :
          Serial.println("Invalid Option");
        break;
    }
  delay(250);
  irrecv.resume();
  }
}

 

 

Links
Expansion board 5V 4 – way relay module Relay Shield

KEYES Infrared receiver sensor module for arduino

Share