Go Down

Topic: Bootloader without turning pins on/off when booting up (Read 2730 times) previous topic - next topic

fungus

The AVR chips disconnect their inputs when they reset. They're floating, not connected to anything. It's a hardware feature, nothing anybody can do about it.

OTOH if you get rid of the bootloader then your program will start up in microseconds. The difference between maintaining the pin state and your program setting pin state at startup will be tiny.
No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

westfw

Quote
if you get rid of the bootloader then your program will start up in microseconds.

I'm afraid not.  There are times built in to allow the clocks to stabalize and similar.  Up to about 100ms worth...  See the datasheet chapter on clock options.


dc42

I haven't yet seen a relay board that needs pullup or pulldown resistors to avoid the relays coming on during power up. Perhaps you should post your code?
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

StealthRT

Here is my code:
Code: [Select]

int relay_1 = 14; //analog pin A0 -> digital to relay 1 [for door unlock]
int relay_2 = 15; //analog pin A1 -> digital to relay 2 [for door lock]
int relay_3 = 16; //analog pin A2 -> digital to relay 3 [My garage door UP/DOWN]
int relay_4 = 17; //analog pin A3 -> digital to relay 4 [Wifes garage door UP/DOWN]
int relay_5 = 18; //analog pin A4 -> digital to relay 5 [n/a]
int relay_6 = 19; //analog pin A5 -> digital to relay 6 [n/a]
int relay_7 = 13; //Digital pin D13 to relay 7 [n/a]
int relay_8 = 12; //Digital pin D12 to relay 8 [n/a]
String readString;

void setup()
{
 pinMode(relay_1, OUTPUT); // sets the analog pin A0 to digital as output [for door unlock]
 pinMode(relay_2, OUTPUT); // sets the analog pin A1 to digital as output [for door lock]
 pinMode(relay_3, OUTPUT); // sets the analog pin A2 to digital as output [My garage door up/down]
 pinMode(relay_4, OUTPUT); // sets the analog pin A3 to digital as output [Wifes garage door up/down]
 pinMode(relay_5, OUTPUT); // sets the analog pin A4 to digital as output [n/a]
 pinMode(relay_6, OUTPUT); // sets the analog pin A5 to digital as output [n/a]
 pinMode(relay_7, OUTPUT); // sets the digital D13 as output [n/a]
 pinMode(relay_8, OUTPUT); // sets the digital D12 as output [n/a]
 Serial.begin(115200);
}

void loop()
{
 while (Serial.available())
 {
   delay(1);
   if (Serial.available() >0)
   {
     char c = Serial.read();
     readString += c;
   }
 }
 
 if (readString == "frontDoorUNLOCK") {
   digitalWrite(relay_1, HIGH);
   delay(1000);
   digitalWrite(relay_1, LOW);
   Serial.println("unlocked");
 } else if (readString == "frontDoorLOCK") {
   digitalWrite(relay_2, HIGH);
   delay(1000);
   digitalWrite(relay_2, LOW);
   Serial.println("locked");
 }
 
 if (readString == "myGarage") {
   digitalWrite(relay_3, HIGH);
   delay(500);
   digitalWrite(relay_3, LOW);
   Serial.println("done");
 } else if (readString == "wifesGarage") {
   digitalWrite(relay_4, HIGH);
   delay(10000); //Needed to bypass damaged sensors on the garage door
   digitalWrite(relay_4, LOW);
   Serial.println("done");
 }
 
 if (readString == "testRelay") {
   digitalWrite(relay_1, HIGH);
   delay(500);
   digitalWrite(relay_1, LOW);
   digitalWrite(relay_2, HIGH);
   delay(500);
   digitalWrite(relay_2, LOW);
   digitalWrite(relay_3, HIGH);
   delay(500);
   digitalWrite(relay_3, LOW);
   digitalWrite(relay_4, HIGH);
   delay(500);
   digitalWrite(relay_4, LOW);
   digitalWrite(relay_5, HIGH);
   delay(500);
   digitalWrite(relay_5, LOW);
   digitalWrite(relay_6, HIGH);
   delay(500);
   digitalWrite(relay_6, LOW);
   digitalWrite(relay_7, HIGH);
   delay(500);
   digitalWrite(relay_7, LOW);
   digitalWrite(relay_8, HIGH);
   delay(500);
   digitalWrite(relay_8, LOW);
 }
 
 readString = "";
 delay(100);
}


StealthRT



Unless they are pulled to a safe level those pins are floating for a while after the board is powered up, they could then do anything and you relay board will follow suite. If the board uses any form of latched logic you are in trouble.

Do you have a schematic of the relay board?

Not true about the possibility of floating input condition. Most of those asian relay boards use a 'current loop' input through the optoisolators input led and are an active low input to turn on the relay (arduino output pin needs to be set to LOW to turn on a relay), so only a +5vdc wire (from the arduino) and a wire(s) from the arduino output pin(s) need to be wired to the relay board to operate the channels, no pull-up or pull-downs required and no arduino ground wire is needed to just operate the optoisolator channels. However the relay board itself does require +5vdc and ground to power the relay coils so if one is going to power the relay coils with the arduino's +5vdc voltage source then a ground is also required from the relay board to a arduino board. However that defeats the advantage of having optoisolators in the first place, so that is rather strange. However most of these relay boards I've seen have a jumper clip so that if the jumper is removed The arduino's +5vdc is not wired to the relay coil circuit but just to the optoisolators input led pins, so you are then free to use an external source of +5vdc voltage to provide power for the relay coil circuits and take full advantage of the isolation properties of the optoisolators.

Also if your going to be using the android's +5vdc to power the relay board then you must also connect the androids ground to the arduino ground for the opto input circuits to work properly, that is probably the source of your problems.

Here is a a typical asian relay board (a single channel version) which has a schematic drawing that should show better what I was trying to explain above.  http://www.ebay.com/itm/251061549983?ssPageName=STRK:MEWAX:IT&_trksid=p3984.m1423.l2649

Lefty

_____
Rob



The relay board is powered by an external power supply. However, i DID have to attached the ground to the arduino also because when i did not, they relays did not come on after 5v was sent from the pins. After hooking up the arduinos' ground also, it worked.

dc42

Assuming it takes +5V (not ground) on an output pin to activate the corresponding relay, then the initialization code looks OK to me. However, you are using digital pin 13, which the bootloader writes to, to connect one of the relays. So that's one relay you can expect to operate at power up.

Your use of a string to accumulate characters is unsafe. If you feed enough characters into it, your program will crash. You should limit the number of characters you accumulate, and preferably accumulate them in a character array to avoid the dynamic memory allocation used by the String class.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

dc42


The relay board is powered by an external power supply. However, i DID have to attached the ground to the arduino also because when i did not, they relays did not come on after 5v was sent from the pins. After hooking up the arduinos' ground also, it worked.


There should be two separate "ground" connections on the relay board: one for the negative side of the power a supply that you use to power the relay board, and one for the negative side of the opto isolator inputs - only this one needs to be connected to Arduino ground.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Coding Badly


Upload an empty sketch.  Do the relays still knock about?

StealthRT



The relay board is powered by an external power supply. However, i DID have to attached the ground to the arduino also because when i did not, they relays did not come on after 5v was sent from the pins. After hooking up the arduinos' ground also, it worked.


There should be two separate "ground" connections on the relay board: one for the negative side of the power a supply that you use to power the relay board, and one for the negative side of the opto isolator inputs - only this one needs to be connected to Arduino ground.


2 separate grounds yes. One from the 5v/ground of the USB to the arduino nano and then ground from the arduino pin to the ground of the power supply. The power supply 5v/ground to the relay board.

westfw

Quote
[floating pins] could then do anything and you relay board will follow suite.

I would think that the usual bipolar-transistor driven relay board actually requires substantial current (mA rather than uA) to turn on.  An IC-driven relay board could be much more sensitive...

Graynomad

Yes, at the time it wasn't known (by me anyway :)) what was driving these relays.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

retrolefty


Quote
[floating pins] could then do anything and you relay board will follow suite.

I would think that the usual bipolar-transistor driven relay board actually requires substantial current (mA rather than uA) to turn on.  An IC-driven relay board could be much more sensitive...



Actually the majority of those asian 'relay boards' that actual post a simple schematic (often showing only a single channel) use the same type of input logic. On the 2 channel relay board I have I can 'activate' an input channel by just having two wires to the arduino, one to an arduino's output pin and one to the arduino 5V shield pin. The current flow (I measured 2ma to activate an opto's input) from 5V pin through a 1k ohm resistor, in and out of the opto's input led, then in and out of a series on board indicator led (red) and then on to the relays channel input pin on to the arduino's output pin, and when set LOW allows 2ma to flow and activate the optoisolator channel. Note that no arduino ground connection is required to just activate a channel's optoisolator on or off.

A arduino ground connect is required if you wish the arduino to provide the +5vdc to power the opto's output transistor, the current switching npn transistor and of course the relay coil, however in that case you are defeating the benefit of having complete opto isolation between the arduino and the relay card. In this case the current to power the relay is being pulled from the arduino's 5V pin, not each digital output pin. I didn't measure the 5vdc current draw to activate a relay coil ( I just measured that the arduino output pin sinks 2 ma to activate the opto's input) but it would be whatever the DC coil resistance is for the relay(s).

However to take full advantage of the ability to have complete galvanic isolation from the arduino to the relay card there is a Vcc jumper on the relay board that allows one to use an external +5vdc power source to provide the relay power and then again one would not have a ground wire from that arduino wired to the relay board or the external 5 vdc power source.

So it can be a little confusing in that relays themselves offer galvanic isolation between the circuit they are controlling via their contacts and the rest of the relay boards circuity, but there is available on these boards the ability to offer an additional level of isolation between the arduino and the relays coil driving circuitry, by pulling a jumper and providing an external 5vdc source.

So in closing I can state that these asian relay boards are fairly cleaver and flexible in there design options and are certainly inexpensive. Features like the opto isolated inputs, the on-board led indicators are nice to have. However their typical documentation is either totally missing or very sketchy at best. The fact that one needs to output a low to activate a channel is probably the first thing that confuses beginners using these boards. Add to that the ability to have two different methods of powering the relay coil circuity and you have a lot of room for confusion for beginners.

Lefty

westfw

If your relays are "active-low", you can avoid momentary ON glitches by setting the output state BEFORE you set the pin to an output:
Code: [Select]
digitalWrite(pin, HIGH);
pinMode(pin, OUTPUT);

StealthRT


If your relays are "active-low", you can avoid momentary ON glitches by setting the output state BEFORE you set the pin to an output:
Code: [Select]
digitalWrite(pin, HIGH);
pinMode(pin, OUTPUT);



Should that not be:
Code: [Select]

digitalWrite(pin, LOW);
pinMode(pin, OUTPUT);

Notice the "low" part instead of "high"? High would turn it on?

StealthRT

I stand corrected.. it was only pin 13 that was coming on/off on reset/power.

Resoldered it to another pin so this is now solved. BUT! with a lot of helpful tips and input for when i do just need to run code and not the stock bootloader.

Thanks for your help everyone!

Go Up