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

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 and LX1972 light sensor example

The LX1972 is a low cost silicon light sensor with spectral response that closely emulates the human eye. Patented circuitry produces peak spectral response at 520nm, with IR response less than ±5%, of the peak response, above 900nm.

The photo sensor is a PIN diode array with a linear, accurate, and very repeatable current transfer function.

High gain current mirrors on the chip multiply the PIN diode photo-current to a sensitivity level that can be voltage scaled with a standard value external resistor. Output current from this simple to use two-pin device can be used directly or converted to a voltage by placing it in series with a single resistor at either of its two pins.

Dynamic range is determined by the resistors (typically in the range of 10K to 100K) and power supply values. Typically the LX1972 needs only 1.8V of headroom to operate at 1000 Lux illumination.

Internal temperature compensation allows dark current to be kept below 200nA over the full specification temperature range (-40 to +85°), providing high accuracy at low light levels. Usable ambient light conditions range is from 1 to more than 5000 Lux.The LX1972 is optimized for controlling back lighting systems in low cost consumer products such as LCD TV, portable computers, and digital cameras.

lx1972-module

FEATURES:
1. Near Human Eye Spectral Response
2. Very Low IR Sensitivity
3. Highly Accurate & Repeatable Output Current vs. Light
4. Scalable Output Voltage
5. Temperature Stable
6. Integrated High Gain Photo Current Amplifiers
7. No Optical Filters Needed

 

Code

void setup()
{
  Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
}
 
void loop()
{
      int val;
      val=analogRead(0);      //connect sensor to Analog 0
      Serial.println(val);//print the value to serial        
      delay(100);
}

Links

 

LX1972 Analog Light Sensor DIY Maker Illumination Sensor Module

Share

Arduino more Easy Module Shield examples

In our previous look at the Easy Module Shield v1 – Arduino Easy Module Shield v1. We took a look at the shield and had a simple example looking at the LDR and the pot. In this example we will show examples for the LM35 and the DHT11. In both examples we will output via the serial monitor but we will also switch an LED on if the temperature rises above a certain value.

Here is the shield and you can clearly see the DHT11, LM35 and the red and blue LEDs

multi-purpose-shield-v1

Code

Example 1 : LM35 example

#define lm35Pin A2
#define ledPin 12
 
 
//this sets the ground pin to LOW and the input voltage pin to high
void setup()
{
Serial.begin(9600);
pinMode(ledPin, INPUT);
digitalWrite(ledPin,0);
}
 
//main loop
void loop()
{
  int rawvoltage= analogRead(lm35Pin);
  float millivolts= (rawvoltage/1024.0) * 5000;
  float celsius= millivolts/10;
  Serial.print(celsius);
  Serial.print(" degrees Celsius, ");
  Serial.print((celsius * 9)/5 + 32);
  Serial.println(" degrees Fahrenheit");
  if(celsius >25)
  {
    digitalWrite(ledPin,1);
  }
  else
  {
    digitalWrite(ledPin,0);
  }
  delay(1000);
 
}

Open up the serial monitor and you should see something like this, put your hand on the LM35 and that should push the temperature higher than 25 degrees and the LED will light

25.39 degrees Celsius, 77.70 degrees Fahrenheit
25.88 degrees Celsius, 78.58 degrees Fahrenheit
26.37 degrees Celsius, 79.46 degrees Fahrenheit
26.86 degrees Celsius, 80.34 degrees Fahrenheit
25.88 degrees Celsius, 78.58 degrees Fahrenheit
25.39 degrees Celsius, 77.70 degrees Fahrenheit
25.39 degrees Celsius, 77.70 degrees Fahrenheit
25.39 degrees Celsius, 77.70 degrees Fahrenheit
24.90 degrees Celsius, 76.82 degrees Fahrenheit
24.41 degrees Celsius, 75.95 degrees Fahrenheit

 

Example 2 : DHt11 Example

 

// Depends on the following Arduino libraries:
// - Adafruit Unified Sensor Library: https://github.com/adafruit/Adafruit_Sensor
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
 
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
 
#define DHTPIN 4 // Pin which is connected to the DHT sensor.
#define DHTTYPE DHT11 // DHT 11 
#define ledPin 13
 
DHT_Unified dht(DHTPIN, DHTTYPE);
 
uint32_t delayMS;
 
void setup() 
{
 Serial.begin(9600); 
 // Initialize device.
 dht.begin();
 Serial.println("DHTxx Unified Sensor Example");
}
 
void loop() 
{
 // Delay between measurements.
 delay(delayMS);
 // Get temperature event and print its value.
 sensors_event_t event; 
 dht.temperature().getEvent(&event);
 if (isnan(event.temperature)) 
 {
 Serial.println("Error reading temperature!");
 }
 else 
 {
 Serial.print("Temperature: ");
 Serial.print(event.temperature);
 Serial.println(" *C");
 if(event.temperature >25)
 {
 digitalWrite(ledPin,1);
 }
 else
 {
 digitalWrite(ledPin,0);
 }
 }
 delay(1000);
}

 

Testing, open the serial monitor and try and alter the temperature on the device (blow on it). Blue led should light

 

Share