RS485 and arduino(compatible) nano v3 send only after press MCU pins

Hi,

I use arduino side as in schema, arduino have ch340 as USB. On other side have usb to rs485 converter as in picture http://img.dxcdn.com/productimages/sku_296620_3.jpg

void setup() {
  Serial.begin(9600);
}
byte cntrl;
byte node = 0xAC;
byte snd[7] = { 0xAA, 0xBA, 0xCA, 0xAA, 0xBA, 0xAA, 0xFF };
void loop() {
  if(Serial.available()) {
    delayMicroseconds(50);
    cntrl = Serial.read();
  }
  if(cntrl == node) {
    digitalWrite(2, HIGH);
    delay(250);
//    Serial.write(snd, 7);
    Serial1.write(0xAC);
    delay(250);
    digitalWrite(2, LOW);
    cntrl = 0;
  }
}

Strange thing is when i press MCU and release, computer got all 7bytes correctly, on other way, when press without release, or power restart MCU, got only part of that chain, or don`t got anything. There is no bad contacts, that behaviour is on same MCU type from other sellers too.

I suppose that when press, MCU got some logical 0 or 1 or something like that and start working. But i`m not sure what can be cause, and what can do to avoid that behaviour.

From computer(os X) send over coolTerm: hex: AC and respond need to be from example, one byte, or 7 byte depend what line i use.

Strange thing is when i press MCU and release, computer got all 7bytes correctly, on other way, when press without release, or power restart MCU, got only part of that chain, or don`t got anything. There is no bad contacts, that behaviour is on same MCU type from other sellers too.

Do you press on the chip? The black chip in the middle of the PCB? In this case it's either a bad soldering (probably) or static electricity (rather improbable the way you describe it). If you mounted the Nano on a breadboard it also might be a broken PCB layer that get reconnected during the press on it.

Please explain the use of delayMicroseconds() in your sketch. If there are already bytes in the reception buffer whay waiting before reading them out?
The first delay() in the loop() is also unnecessary, the second one can be replaced by a call to Serial1.flush().

Please post a picture of your setup. What kind of device(s) is on the other end of the RS-485?

Thanks for fast reply

In next picture color is:

from Arduino to maxim rs485:

red - 5V
black - gnd
yellow - D2
green - Rx
purple - Tx

From maxim485 to usb->rs485 adapter
white - B
orange - A

I change code to this, mistake was Serial1, that was from my previous test. Current code is:

void setup() {
  Serial.begin(9600);
}
byte cntrl;
byte node = 0xAC;
byte snd[7] = { 0xAA, 0xBA, 0xCA, 0xAA, 0xBA, 0xAA, 0xFF };
void loop() {
  if(Serial.available()) {
    cntrl = Serial.read();
  }
  if(cntrl == node) {
    digitalWrite(2, HIGH);
    delay(250);
//    Serial.write(snd, 7);
    Serial.write(0xAC);
    delay(250);
    digitalWrite(2, LOW);
    cntrl = 0;
  }
}

First delay is to give MCU chance to switch what it need to switch on D2 pin, that I can start sending data, after sending data, next delay is that data can go out to cable, and then switch to LOW.

When i press and release MCU in part where need to be analog pins, then response is OK, and after each press/release response is OK. There is no keep pressing the contacts, it`s only press and release, that is weird.

I can do same schema on PCB with soldering, but not sure if contact is a problem, strange for me is that press/release thing.

Here is picture, previous upload was not success.

board-schema.png

New thing, when i catch wire for RE/DE which is short soldered everything works, when not touching that pin/wire nothing works, is there some solution to ground that or what I can do without to burnout device?

First delay is to give MCU chance to switch what it need to switch on D2 pin, that I can start sending data, after sending data, next delay is that data can go out to cable, and then switch to LOW.

Believe me, the MCU don't need that time. When the call to digitalWrite() returns the signal is available on the pin.

An Serial.flush() is exactly doing that: it waits until the send buffer is cleared and doesn't return before that.

Regarding the posted picture: the USB is usually connected to a PC, isn't it?

In your schema, you're missing the pull-up for the RO signal. It keeps the RX line of the MCU from floating while RE is not activated.
You should also keep the bus from floating while none of the participants (in your case only two) has it's sending driver active. You do that by a 470Ω resistor from A to Vcc and a same value resistor from B to GND. I don't know if your USB adapter already includes that, you might have to measure while it's not connected. These two resistors must be applied only once per bus.

Great, i put two 4k7 in way that you say, one thing is not clear to me:

In your schema, you're missing the pull-up for the RO signal. It keeps the RX line of the MCU from floating while RE is not activated.

I`m not sure if I need to set resistor between GND and RE, and not short circuit RE and DE, and what that resistor value need to have ?

Yes, usb go to computer, i use cool-term app for testing. In my previous comment there is that thing with touching D2 pin, when i touch that pin, all works.

I`m not sure if I need to set resistor between GND and RE, and not short circuit RE and DE, and what that resistor value need to have ?

I clearly stated "a pull-up for RO", so it's a resistor between RO and Vcc (a resistor to GND is a pull-down). A 10kΩ should be OK. RE and DE should stay exactly like they are in your schemata.

Yes, usb go to computer, i use cool-term app for testing. In my previous comment there is that thing with touching D2 pin, when i touch that pin, all works.

If you touch pins you may influence the signal if it's weak. Especially if a pin is floating you may change the level in a way that's unpredictable. Even worse, you may destroy the circuit if you have loaded to much static energy on you.

I was not familiar with pull-up and pull-down, thanks to put that clear.

I set as you suggest and everything works, i now test with setting pinMode in setup method as:

pinMode(2, OUTPUT);

and everything works too, but i will use it with all resistors that you suggested.

Thanks a lot for helping me !