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;
}
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.