Hi!
I have to somehow accomplish this task, to establish a communication between two Arduino boards through 485. I follow this example here:
https://create.arduino.cc/projecthub/moreirarobotics/how-to-communicate-two-arduinos-via-rs485-621ddb
Transmitter code:
// Transmitter Nano Temp Sensor
#include <SoftwareSerial.h>
// bibliotecas para leitura do sensor DS18B20
#include <OneWire.h>
#include <DallasTemperature.h>
#define TX_485 8 // Pino DI do módulo RS 485
#define RX_485 7 // 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 pino_sensor = 5;
OneWire bus (pino_sensor);
DallasTemperature sensors (&bus);
DeviceAddress sensor;
unsigned int tempo=0;
// variavel para armazenar a temperatura
float temp;
int t=0;
void setup()
{
// inicia a biblioteca
sensors.begin();
// velocidade comunicaçao serial
Serial.begin(9600);
RS485_serial.begin(9600);
pinMode(RS485_control,OUTPUT);
Serial.println(" Monitoramento de temperatura com sensor DS18B20 ");
if(!sensors.getAddress(sensor,0))
{
Serial.println(" Sensor DS18B20 nao encontrado");
}
}
void loop()
{
if((millis()-tempo)>200)
{
tempo=millis();
sensors.requestTemperatures();
temp=sensors.getTempC(sensor);
digitalWrite(RS485_control, RS485transmit);
t = temp * 100;
TX[0] = t/ 255;
TX[1] = t % 255;
for (int i=0; i<4; i++)
{
RS485_serial.write(TX[i]);
}
Serial.print("Temperatura: ");
Serial.print(temp);
Serial.println(" oC");
}
}
Receiver code:
// Receiver Uno LCD
#include <SoftwareSerial.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Addr,En, Rw,Rs,d4,d5,d6,d7, backlight, polarity
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7,3,POSITIVE);
#define TX_485 8 // Pino DI do módulo RS 485
#define RX_485 7 // 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 RX[4];
#define RS485transmit HIGH
#define RS485receive LOW
// variavel para armazenar a temperatura
float temp;
unsigned long tempo =0;
void setup()
{
// velocidade comunicaçao serial
lcd.begin(20,4);
Serial.begin(9600);
RS485_serial.begin(9600);
pinMode(RS485_control,OUTPUT);
Serial.println(" Comunicacao RS485 ");
lcd.setBacklight(HIGH);
lcd.setCursor(0,0);
lcd.print(" COMUNICACAO RS485 ");
}
void loop()
{
if((millis()-tempo)>500)
{
lcd.clear();
tempo=millis();
digitalWrite(RS485_control,RS485receive);
if(RS485_serial.available () >0)
{
for (int i =0; i<4; i ++)
{
RX[i] = RS485_serial.read();
}
temp = ((RX[0] * 255) + RX[1]) /100;
Serial.print(" Temperatura : ");
Serial.print( temp );
Serial.println(" *C ");
}
lcd.setBacklight(HIGH);
lcd.setCursor(0,0);
lcd.print(" COMUNICACAO RS485 ");
lcd.setCursor(0,1);
lcd.print(" SENSOR DS18B20 ");
lcd.setCursor(0,2);
lcd.print(" TEMP ");
lcd.setCursor(7,2);
lcd.print( temp);
lcd.setCursor(11,2);
lcd.print(" *C ");
}
}
Even if the comments are in another language, you can understand what is happening, create variables, insert some libraries. I do not have the same temperature sensor, so I don`t need DallasTemperature, but I have an LM35.
I don't quite understand what happens in loop (), when reading the temperature and transmitting through 485.For the LM35, I wrote like this:
void loop()
{
if((millis()-tempo)>200)
{
tempo=millis();
//****** only this line is added by me *******
temp=analogRead(LM35); // LM35 connected to A0
digitalWrite(RS485_control, RS485transmit); // **** from here I don`t understand very much
t = temp * 100;
TX[0] = t/ 255;
TX[1] = t % 255;
for (int i=0; i<4; i++)
{
RS485_serial.write(TX[i]);
}
Serial.print("Temperature: ");
Serial.print(temp);
Serial.println(" oC");
}
}
Can I use the same temperature conversion for my LM35, and the same method of transmitting the value through 485 as in the tutorial?