Hi, I am a newbie with a question ..... If I am breaking rules please help me understand how to post my question. I worked out a basic circuit to turn on an led and use a relay to run a 12v motor via push button, then reset for the next push. It works, but the motor comes on at power up and runs until the button is pushed, then after the timed delay -- works fine until power is shut off.
In Setup you need to digitalWrite the relay pin to the "OFF" state, whatever this is. BEFORE you pinMode it to OUTPUT.
The default mode is INPUT. Writing an INPUT pin LOW will turn off the internal pullup resistor. Writing it HIGH will turn the internal pullup resistor on. The state of the pullup resistor has NOTHING to do with the state of the pin after it is set to an OUTPUT pin. That state must be set AFTER the pin becomes an output pin.
Most relays have an NO and an NC pin. Connect the motor to the one that does not get power when the pin controlling it is an output pin and is LOW. Then, when you digitalWrite() the pin HIGH the motor will come on.
Thanks, Not sure if I really understand .... But it works by just changing my digitalWrite (RELAY,LOW); to a HIGH. Thanks again. Any suggestions where I can read up on this? Bruce in Mt. Airy, MD
The state of the pullup resistor has NOTHING to do with the state of the pin after it is set to an OUTPUT pin. That state must be set AFTER the pin becomes an output pin.
If you do a digitalWrite of HIGH or LOW to a pin BEFORE pinMode sets it to OUTPUT, then that IS the state the output will be at when it BECOMES an output. Try it. (The pullup resistor, yes, is irrelevant to this)..
This is necessary for the (very common) opto-isolated relay board so many people are using...
(From the page mentioned above, here's example setup() code):
/* YourDuino Example: Relay Control 1.10
Handles "Relay is active-low" to assure
no relay activation from reset until
application is ready.
terry@yourduino.com */
/*-----( Import needed libraries )-----*/
/*-----( Declare Constants )-----*/
#define RELAY_ON 0
#define RELAY_OFF 1
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/
#define Relay_1 2 // Arduino Digital I/O pin number
#define Relay_2 3
#define Relay_3 4
#define Relay_4 5
void setup() /****** SETUP: RUNS ONCE ******/
{
//-------( Initialize Pins so relays are inactive at reset)----
digitalWrite(Relay_1, RELAY_OFF);
digitalWrite(Relay_2, RELAY_OFF);
digitalWrite(Relay_3, RELAY_OFF);
digitalWrite(Relay_4, RELAY_OFF);
//---( THEN set pins as outputs )----
pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_4, OUTPUT);
delay(4000); //Check that all relays are inactive at Reset
}//--(end setup )---