How to get a 5v relay to Work

I have read most of the 5V relay topics I could find. I have come to the conclusion that I need a transistor and (flyback diode?) to grt thr arduino to activate the realy module. Please excuse me I have no electronics background do not understand most schematics past basic wiring . I would be very appreciative if someone could shopme how I modify my current project to use a transitor for my relay switch.

Here is the actual circuit.

I am attaching the led to test the circuit. Eventually I want to be able to hook up 125v light and fan to control remotely.

#include <IRremote.h>

/******************************************
 *Website: www.elegoo.com
 * 
 *Time:2017.12.12
 *
 ******************************************/

int relayPin1 = 4;

//int relayPin2 = 6;
int ledPin = 2;
 
void setup() 
{
   Serial.begin(9600);

   relayPin1, (OUTPUT);
//   relayPin2, (OUTPUT);
} 
 
 
void loop() 
{ 
  
  Serial.println("switch Relay 1");

digitalWrite(relayPin1, HIGH); // turn the relay on (HIGH is the voltage level)
 Serial.println(relayPin1);
  delay(2000);              // wait for a second
  digitalWrite(relayPin1, LOW);    // turn the relay off by making the voltage LOW
   Serial.println(relayPin1); 
   delay(2000);              // wait for a second
/*  
  Serial.println("switch relay 2");
  digitalWrite(relayPin2, HIGH);   // turn the relay on (HIGH is the voltage level)
  delay(2000);              // wait for a second
  digitalWrite(relayPin2, LOW);    // turn the relay off by making the voltage LOW
  delay(2000);              // wait for a secon
*/  
} 

Before you go any further, change the LED resistor from 220k to 220 ohms (red red brown).

Does the relay energize/click ?

What is this doing ?
relayPin1, (OUTPUT);


What is this doing ?
Serial.println(relayPin1);



/******************************************
  Website: www.elegoo.com

  Time:2017.12.12

 ******************************************/
#include <IRremote.h>
const byte relayPin1 = 4;
//int relayPin2 = 6;
const byte ledPin = 2;

void setup()
{
  Serial.begin(9600);

  pinMode ( relayPin1, OUTPUT);
  //   relayPin2, (OUTPUT);
}


void loop()
{
  Serial.println("switch Relay 1");
  digitalWrite(relayPin1, HIGH); // turn the relay on (HIGH is the voltage level)
  Serial.println( "Relay1 : ON ");
  delay(2000);              // wait for a second
  digitalWrite(relayPin1, LOW);    // turn the relay off by making the voltage LOW
  Serial.println("Relay1 : OFF ");
  delay(2000);              // wait for a second
  /*
    Serial.println("switch relay 2");
    digitalWrite(relayPin2, HIGH);   // turn the relay on (HIGH is the voltage level)
    delay(2000);              // wait for a second
    digitalWrite(relayPin2, LOW);    // turn the relay off by making the voltage LOW
    delay(2000);              // wait for a secon
  */
}

You have a relay 'module' which probably has the diode ( i can't see from the pic) , as @LarryD pointed out the resistor should be 220 Ohms.
The posted wiring diagram is OK . Try the modified sketch above to test what you have .

To operate a bare relay, you do but that is exactly why you use a relay module which is correctly designed to incorporate these.

You have apparently been reading warnings about using a separate power supply for the relay module. This is true to some extent but the relay module can probably be powered - like the LED you are using for the moment (but I suspect you intend to control something different later) from the "5V" pin given that you are powering the UNO from USB and using only one relay module. It would be best to connect a 220 µF or larger capacitor across the "+" and "GND" terminals of the relay module (noting correct polarity).

Full marks for posting code properly, you need to use "Auto Format" (Control-T) on the code in your IDE to make it read neatly.

I cleeaned up the code some.

The problem is that I am getting 1.12v from the input pin. Tried various other pins the same result. I tried two Elegoo Uno's. A nano (old bootloader) all with the same result.

How can I boost the voltage to the input?

#include <IRremote.h>

/******************************************
  Website: www.elegoo.com

  Time:2017.12.12

 ******************************************/

const byte relayPin1 = 12;
const byte ledPin = 2;

void setup()
{
  Serial.begin(9600);

}
void loop()
{
  Serial.println("switch Relay 1");
  digitalWrite(relayPin1, HIGH); // turn the relay on (HIGH is the voltage level)
  delay(2000);              // wait for a second
  digitalWrite(relayPin1, LOW);    // turn the relay off by making the voltage LOW
  delay(2000);              // wait for a second

}

You need to make control pins OUTPUT.

No call to pinMode - that's always the first thing you need to think about in a sketch....

Hi,
Constructive criticism.

Can I suggest you put you IR code aside and write some simple code.
That is develop your code in stages.

  1. Write a bit of code to control JUST the relay.
  2. Write a bit of code to read and decode JUST IR signal.
  3. When both are working on their own, THEN combine them.

It looks like you have not even tested your relay circuit.

Tom... :grinning: :+1: :coffee: :australia:

Decoded, that suggest he is getting 1.12 V when he measures the voltage at the input pin.

That would definitely explain it. He is essentially driving it with INPUT_PULLUP. :crazy_face:

thanks for trying to help!

I inadverdently took out the pinmode line from the script I originally posted. I put it back in and now it works. I have no Idea why it was not orignally.

But thank you for your help.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.