I am trying to control a 12V relay with a transistor but can't seem to get it working perfectly. The relay coil will switch from NC to NO when I plug in the signal from Arduino pin3 (output) and will switch back when I unplug the signal, but the system will not respond to the signal that I have created with a simple code.
(I also have a servo working in this code but that is fine)
//airspeed sensor/servo motor:
#include <Servo.h>
Servo myservo;
int potpin = A1;
int airspeedval; //variable to read the value from the analog pin
const int motorRelay = 3; //connected to transistor on sep breadboard
void setup()
{
myservo.attach(12);
pinMode(motorRelay, OUTPUT);
}
void loop()
{
airspeedval = analogRead(potpin); //reads the value of airspeed sensor
airspeedval = map(airspeedval, 0, 1023, 0, 179);//Scales analog pin signal to use with servo
//could change the vaule you of (0, 179) to cut off the lower end of the servo angle
myservo.write(airspeedval); //sets the servo according to the scaled value
delay(5005); //waits for the servo to get there
digitalWrite(motorRelay, HIGH);
delay(5000);
digitalWrite(motorRelay, LOW);
delay(200);
}
Attached is a diagram of my circuit:
(note I am not controlling the 230 VAC or Lamp as this is just a very similar circuit I found)
Any help with this would be much appreciated as I am preparing my project for exhibition.
Do you power the servo motor with the Arduino 5V pin ? That 5V pin does not supply enough current for a servo motor. Test you sketch without the servo motor connected.
You use pin 3, set it as OUTPUT and make it HIGH and LOW. That should be okay.
Perhaps the contacts of the breadboard are bad. Or the relay coil requires too much current. Or the wrong transistor is used.
Could you tell which relay and which transistor you use, and could you make a photo, so we can see the wiring ?
You are trying to switch a 12V relay with a 5V tension from the Arduino, it is not likely to work well.
Even if it does occasionnaly, it would not be reliable anyway.
Except from that, the Fritzing drawing has a few catastrophic mistakes.
First of all: Never put 230 (or 115) volts to a breadboard.
They aren't meant for that.
Then the one who made that drawing, made a big mistake too.
He connected a wire from the lamp to the 5 volt power supply of the Arduino.
This wire was meant to go 1 row further so it would be connected to the relay contact.
You might think that this way, the switch would never be closed and there would impossibly be any dangerous voltage to the 5 volt line.
But you are introducing the 230 VAC to the 5 volts, even though these aren't completed circuits yet.
This way you only need to have a single fault, and you touching the Arduino might be it.
This is just some mistake which can be made by someone not knowing exactly what he is doing.
The problem is that this was put online somewhere (i think i know where, but i'm not going to say "instructables"), for any one without the faintest notion of electronics to copy and rebuild.
I say toss the transistor and use a prebuilt h bridge to control the relay. transistors can be a pain to use without a significant understanding of how each type works and how to implement them.
Ogmudbones:
I say toss the transistor and use a prebuilt h bridge to control the relay.
Why would you use an entire h-bridge to control a single relay? I mean - it would work - but it is massive overkill.
Instead - if going for "pre-built" - there are more than a few available relay modules out there for microcontrollers like the Arduino that are easy to use. Most will switch up to 230 VAC @ 10 amps or thereabout.
Otherwise - using a high-current relay/solenoid driver IC can be easier - for instance, the venerable ULN200x line of darlington drivers are purpose-made for this application. There likely are MOSFET variants out there (though IDK whether you can get them in DIP packaging - I haven't looked).
Ogmudbones:
transistors can be a pain to use without a significant understanding of how each type works and how to implement them.
Well - the only way to gain that understanding is to study them. Understand the math involved for picking the right resistors, how to pick the proper transistor, differences between PNP and NPN, amplifier topologies for BJT (common emitter, common base, common collector), etc. There are similar topics to study for MOSFETs, too.
There is tons of information available on the internet, but figuring out which is best can be difficult.
The best thing to do is to get a book or two. Horowitz's "Art of Electronics" and/or Grob's "Basic Electronics" should be required reading for anyone wishing to get a solid understanding of electronics in general.
Really, the PowerSwitch Tail is great. 120/240VAC (there are different models) and at least 10A. And all controllable with Arduino! No relay or transistor required.
JimboZA:
Just ordered 3rd Ed.... ostensibly for my undergrad student daughter. Wish I could afford 2 copies, but the weak ZAR makes it very pricey....
Both are textbooks, and as such, tend to be crazy expensive for a recent edition - but I would think the 3rd edition would be much cheaper. But then, you mention exchange rates...sigh.