Solution to 5v relay not clicking but led is ON

EDIT: Under this line is my original post, but my solution was actually incorrect.
Experienced people helped me solving it, please read the entire thread to see what i did wrong.

ORGINAL POST:
Hey everyone, first of all i am new here, just starting out with Arduino.

I encountered a problem with my 5v relay that was not clicking, but as soon as it got signal, the LED on the relay lit up slightly.

It was connected correctly: + to VCC Arduino, - to GRND Arduino and S to signal output pin 7 Arduino, all using a breadboard.
The coding was also correct and checked it multiple times.
The project is controlling 3 fans at 3 different temperatures using the DHT11 sensor and 3 relays.

My theorie of the relay not clicking was that the microcontroller of Arduino was not putting out enough current to trigger the relay's coil.
I checked the voltage of pin 7 when it was activated and it was 4.7v wich should be enough to trigger the relay.
I fixed this whole problem by using the PN2222a Transistor.
Connected Pin 3 (collector) to the VCC of Arduino using the breadboard.
Connected Pin 2 (base) to the 4.7v output of Arduino (pin 7)
Connected Pin 1 (emitter) to the Signal pin of the 5v relay.

I thought of using the transistor to amplify the microcontroller output by a solid 5v from the Arduino's internal power supply, i just connected the Arduino with the USB cable to my PC.
This is a solution that i have read nowhere, so i just wanted to have this information out here for everyone having the same issues when connecting a 5v relay to arduino that won't click.

Post your code and a schematic of your project.
A drawing speaks more than 1000 words.

Which relay module.
Some (most) are "active LOW", meaning 0volt on the relay input makes the relay click.
Which fans. too much current or no snubber diode could have welded the relay contacts together.
Leo..


It's this relay.
On "LOW", it does nothing.
If i connect S to 5V, it clicks.
Nothing connected to the newly bought relays.

That seems to be an active hig h relay.

Did you set the Arduino pin to OUTPUT, with pinMode.
Leo..

How bright is the LED when you do that, compared to when it is (presumably) connected to an Arduino output pin?

If you have not done that, it would explain the problem.

If you don't set a pin to OUTPUT, it will default to INPUT. If you then set the pin to HIGH, it will not change to be an OUTPUT, but the internal pull-up will be switched on. This will allow only a small current to flow from the pin. Not enough to make the relay click but enough to light the LED very dimly.

It will still be an INPUT, but with some weak pull up current enabled.
That small pull up current will only partially drive the relay transistor.
Leo..

That is because it make no sense and I would not recommend this solution to anyone reading this topic.

Thanks to all of you, i realised that i forgot assign the pins to OUTPUT mode.
I now adjusted the coding, and it works!
With this knowledge i agree that it makes no sense to use a transistor, and do it like i did.
I like to do it the right way and have to learn a lot about Arduino, but you guys helped me with that, and i want to thank all of you for the quick feedback.
Would you recommend me deleting my post?
Or should i leave it so people could learn from it?

Albert

//by Druhi Chakraborty
int relayPin1 = 7;
int relayPin2 = 8;
int relayPin3 = 9;
#include "DHT.h"
#define DHTPIN 4// you  can use 
#define DHTTYPE DHT11//#define DHTTYPE DHT21
                     //#define  DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);//you can also use pins 3, 4, 5, 12, 13  or 14
   // Pin 15 can work but DHT must be disconnected during program upload
#include  <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
  pinMode (7, OUTPUT);
  pinMode (8, OUTPUT);
  pinMode (9, OUTPUT);
  dht.begin();// initialize the sensor
 lcd.backlight();// turn on lcd backlight
  lcd.init();// initialize lcd
}
void loop() {
 lcd.clear();
   lcd.setCursor(0,0);//  set the cursor on the first row and column
   lcd.print("H=");
   lcd.print((float)dht.readHumidity());//print  the humidity
   lcd.print(" %");
   lcd.setCursor(0,1);//set the cursor on  the second row and first column
   lcd.print("T=");
   lcd.print((float)dht.readTemperature());//print  the temperature
   lcd.print(" C");
   delay(1000);
   lcd.clear();
   if (dht.readTemperature() >29){
    digitalWrite(relayPin1,HIGH);
  }else{
    digitalWrite(relayPin1,LOW);
  }
   if (dht.readTemperature() >30){
    digitalWrite(relayPin2,HIGH);
  }else{
    digitalWrite(relayPin2,LOW);
  }
   if (dht.readTemperature() >31){
    digitalWrite(relayPin3,HIGH);
  }else{
    digitalWrite(relayPin3,LOW);
  }
}


I added the 3 pinMode lines just below void setup, and now it works like a charm without the unneccesary transistor i used.

Leave it but edit your original post so that it is clear that this is not the proper way to solve the problem

Here is my schematic sketch, with only the first relay.

In my case, above code works fine with Arduino ide.
Same code when I try with python then relay does not respond. I observe that while flashing is in progress then relay works( leds blinks)