Relay 5V to dc motor not working

I have been trying to program my Arduino Uno to sense blue color with a sensor and to cut power to a DC motor as soon as the blue color reaches a certain threshold.

So far the color sensor has worked mostly fine. But the relay (set to Normally Open) doesn't work.

The relay's power supply is the Arduino's 5V outlet which turns the relay on (or at least makes the Power on light for the Arduino light up)

So its:
Relay power --> Arduino 5V
Relay ground --> Arduino grd
Relay signal --> digital pin 13

The color sensor is attached to the Arduino's 3.3V if that matters

The wiring is:

power source(batteries) --> motor --> relay --> ON/OFF switch --> power source

The code in the Arduino is:


   #define S0 8
   #define S1 9
   #define S2 10
   #define S3 11
   #define sensorOut 12
   #define motor 13
int frequency = 0;
int threshold = 220;
void setup() {

  pinMode(A1,OUTPUT);
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(sensorOut, INPUT);
  pinMode(motor, OUTPUT);
  
  // Setting frequency-scaling to 20%
  digitalWrite(S0,HIGH);
  digitalWrite(S1,HIGH);
  
  Serial.begin(9600);
}



void loop() {
   
  //photo diode initialization
  // Setting red filtered photodiodes to be read
  digitalWrite(S2,LOW);
  digitalWrite(S3,HIGH);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  // Printing the value on the serial monitor
  Serial.print("B= ");//printing name
  Serial.print(frequency);//printing color frequency
  Serial.println("  ");
  delay(500);
  
 
  if (frequency>threshold)
  {
  digitalWrite(motor,LOW);
  Serial.println("motor is off");
  delay(500);
  }
  else {
  digitalWrite(motor,HIGH);
  Serial.println("motor is on");
  delay(500);
  }
  
  }

I've tried:
-using a different code that just turns the motor on and off regardless of the color sensor (motor stayed on no matter what)
-Switching digital pin 13 to 7 and 6 just in case pin 13 burned out

  • getting another relay

I'm kinda new to this but it worked when I tried this same setup a few weeks ago, and now it's not working at all.
What's going on and how do I fix it?

Your topic was MOVED to its current forum category as it is more suitable than the original

Are you trying to use the Arduino output to directly drive a relay coil?

Post a picture of your components you are using all wired up.

I can't access the setup right now but I took a few pics before I left, hope it helps for now, if not I'll get them tomorrow.

But yes, The relay is being powered by the Arduino
The relay is a basic 1 channel 5V module
image

Yes an Arduino digital pin is not capable of providing enough current to drive a relay, especially one with a 5V coil. What relay is this, or is it a relay module and not actually a relay.

Sorry I'm new to the forum only one pic at a time

Its a
Tolako 5v Relay Module 5V Indicator Light LED 1 Channel Relay Module for Arduino ARM PIC AVR MCU

So you chose the blurred one with bad lighting?

#define S0 8
#define S1 9
#define S2 10
#define S3 11
#define sensorOut 12
#define motor 13
unsigned long PulseLength = 0;
const unsigned int threshold = 220;

void setup() {
  pinMode(A1, OUTPUT);
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(sensorOut, INPUT);
  pinMode(motor, OUTPUT);
  digitalWrite(S0, HIGH);// Setting PulseLength-scaling to 20%
  digitalWrite(S1, HIGH);
  //photo diode initialization
  // Setting red filtered photodiodes to be read
  digitalWrite(S2, LOW);
  digitalWrite(S3, HIGH);
  Serial.begin(9600);
}

void loop() {
  PulseLength = pulseIn(sensorOut, LOW);
  Serial.print("B= ");  Serial.println(PulseLength);

  if (PulseLength > threshold)  {
    digitalWrite(motor, LOW);
    Serial.println("motor is off");
  }
  else {
    digitalWrite(motor, HIGH);
    Serial.println("motor is on");
  }
  delay(500);
}

The relay module does not look like an optoisolated relay module, but I could be wrong. Post the link to the relay's documentation or even the link to where you bought it from.

The Uno does not have enough output to directly drive a relay coil.

I got it off Amazon, Heres the link:

1 Like

Thanks for posting the link.

I use these

AmazonSmile: SunFounder Relay Module Compatible with Arduino and Raspberry Pi 5V DC Trigger by HIGHLO (HIGH Trigger) : Electronics

After reading the specs of the relay module you posted a link to, the specs do not indicate to me that the relay can be driven directly by a GPIO pin.

But there is something that looks like a surface mount FET by the cathode of the diode.

I can see something black.
It's a pity that the manufacturers didn't include "transistor input" in the description.
"suitable for microcontroller"

That's what you get from buying from sellers who have no idea what they are selling.

The pin you are connection your Relay I think is not capable to send the required power to the Relay to work

Isn't that what he said?

I wondered about that and decided the circuit may have something more to do with the wide Vin coil range to temper the current through the built in LED.

Maybe u can use mosfet

Is there a delay on your line?