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?
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.
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 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.