Arduino Uno with Sw420 and RS485 problem

Hi, I'm a beginner in Arduino and the like. I'm trying to connect an Arduino Uno (Slave) which has a SW420 sensor to an Arduino Mega (Master) which has a 16x2 I2C LCD and uses RS485 as a communication protocol. When I tried, I got a problem in the form of the SW420 sensor value could not be detected or you could say the sensor was not working on the slave node.

Code:

#include <SoftwareSerial.h>

#define TX_485 4 // Pino DI do módulo RS 485
#define RX_485 2 // Pino RO do módulo RS 485
#define RS485_control 3 // Habilita ou não a transmissão e recepção de dados
SoftwareSerial RS485_serial (RX_485, TX_485);

byte TX [4];
#define RS485transmit HIGH
#define RS485receive LOW

// pino que o sensor esta conectado
const int vs =9;
//const int pino_sensor = 7;
//OneWire bus (pino_sensor);
//DallasVibration sensors (&bus);
//DeviceAddress sensor;

unsigned int tempo=0;

// variavel para armazenar a temperatura
float temp;
int v=0;

void setup(){
  // inicia a biblioteca
//  sensors.begin();
  // velocidade comunicaçao serial
  Serial.begin(9600);
  RS485_serial.begin(9600);
  pinMode(vs, INPUT);
  pinMode(RS485_control,OUTPUT);
  Serial.println(" Tes Sensor ");

}
void loop(){
  long measurement = vibration();
  delay(50);
  if((millis()-tempo)>200){
    tempo=millis();
    Serial.print(measurement);
    temp=measurement;
    digitalWrite(RS485_control, RS485transmit);
    v = temp * 100;
    TX[0] = v/ 255;
    TX[1] = v % 255;
    for (int i=0; i<4; i++){
      RS485_serial.write(TX[i]);
    }
    Serial.print("Getaran : ");
    Serial.print(temp);
  }
}

long vibration(){
  long measurement=pulseIn (vs, HIGH);
  return measurement;
}

Any suggestions what might be wrong here?

you've been mixing variable types a bit in your code and that could be the reason why.

maybe try something like this instead:
(Compiles, NOT tested!)

#include <SoftwareSerial.h>

#define TX_485 4 // Pino DI do módulo RS 485
#define RX_485 2 // Pino RO do módulo RS 485
#define RS485_control 3 // Habilita ou não a transmissão e recepção de dados
#define RS485transmit HIGH
#define RS485receive LOW

SoftwareSerial RS485_serial (RX_485, TX_485);

// pino que o sensor esta conectado
const uint8_t vs = 9;
//const int pino_sensor = 7;
//OneWire bus (pino_sensor);
//DallasVibration sensors (&bus);
//DeviceAddress sensor;

uint32_t tempo = 0;

void setup() {
  // inicia a biblioteca
  //  sensors.begin();
  // velocidade comunicaçao serial
  Serial.begin(9600);
  RS485_serial.begin(9600);
  pinMode(vs, INPUT);
  pinMode(RS485_control, OUTPUT);
  Serial.println(" Tes Sensor ");

}
void loop() {
  int32_t measurement = vibration();

  if ((millis() - tempo) > 200) {
    tempo = millis();
    Serial.print(measurement);

    measurement *= 100;

    Serial.print(" Getaran: ");
    Serial.println(measurement);

    digitalWrite(RS485_control, RS485transmit);
    for (int i = 0; i < sizeof(measurement); i++) {
      uint8_t c = (uint8_t)measurement;
      RS485_serial.write(c);
      measurement >>= 8;
    }
  }

  delay(50);
}

int32_t vibration() {
  int32_t measurement = pulseIn (vs, HIGH);
  return measurement;
}

Please note that above code would send the LSByte out first.

hope that helps...

sensor still can't read

hmmm... can you show how you have wired up your circuits please?

you could also try a simple test:

in int32_t vibration() return a constant value, so for example:

int32_t vibration() {
  int32_t measurement = pulseIn (vs, HIGH);
  return 123;
}

if you can then see this value being outputted, then the problem is with your sensor.

hope that helps...

When experimenting with RS485, I would recommend you get a cheap USB-RS485 adapter for your PC as it will allow you to listen in to the data on the bus. It will quickly show you whether the issue is with the sender or receiver.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.