We developed this project in partnership with JLCPCB and Robo Lúdico School. The used board files are available for download. Download the 2 boards for projects with Arduino.
Link to download the Arduino compatible board: https://bit.ly/ArduinoGerberFilesPCB
Link to download the shield: https://bit.ly/3fNSNRI
I made the project with JLCPCB. They released a coupon to earn 5 units of that card. The coupon is: JLC-REBE
The process is simple:
Download the gerbers files from the project link;
- Download the gerbers files from the project link;
Visit the website: https://www.jlcpcb.com/PFC
- Visit the website: https://www.jlcpcb.com/PFC
Upload the Gerber files;
- Upload the Gerber files;
Apply the JLC-REBE coupon in the payment area.
- Apply the JLC-REBE coupon in the payment area.
Let's start the project!
IntroductionGenerally in industries, we have several machines and equipment (PLCs, CNC, frequency inverters) controlling and monitoring the most diverse industrial processes.
For all of this equipment to work together, it is essential that there is a communication network between them.
However, the industry is an environment that suffers from the presence of many electromagnetic interferences due to the activation of motors, solenoids, and other actuators.
So the communication network needs to be robust, in view of this need, the RS485 communication standard was created.
The RS485 communication standard is the physical layer of the communication network, and several protocols such as Modbus, Profibus, and others can be implemented.
The RS485 communication standard is suitable to be implemented in the industry, as it accepts several network topologies such as Star and ring, for example.
It allows communication over long distances and uses twisted pair cable. This cable model reduces electromagnetic interference since we have a differential communication signal and this contributes to being more immune to noise and electromagnetic interference.
In addition to RS485 communication, there is also RS232 communication. It allows network communication in point-to-point mode and suffers more from electromagnetic interference.
However, many devices have RS232 communication and need to transmit data over long distances. For this, we need to use the integrated circuit called MAX485.
This integrated circuit plays a role in adapting the TTL logic level to the logic level required for RS485 communication.
Nowadays there are already ready and low-cost modules with this integrated circuit. They allow signal conditioning and facilitate communication between different equipment.
Thus, RS485 communication can be used in any type of environment due to its robustness.
So in this article, we will learn how to monitor the temperature of an environment and receive the measured value through serial / RS485 communication between two Arduinos.
The Arduino Slave (Transmitter) will perform the temperature measurement using a DS18B20 digital sensor and will send this measured value via serial / RS485 to the Arduino Master (Receiver) that will write on the LCD display 20 x 4 I2C the received temperature value.
Therefore, through this article you will learn:
- Perform the circuit assembly on the protoboard;
- Understand the operation of the RS 485 module;
- Understand how the DS18B20 sensor works;
- Create a communication protocol;
- Carry out remote temperature monitoring using a wired communication network;
- Earn your own Arduino Compatible Board of the JLCPCB.
Now, we will start the complete presentation of the development of the project Temperature monitoring with the DS18B20 sensor using RS485 serial communication.
Developing the Temperature Monitoring Project with the DS18B20 sensor using RS485 serial communicationIn figure 3 we have the schematic circuit necessary for the assembly of the project.
The Arduino Uno will be the master (receiver), it will receive the temperature measurement and will display on the LCD display 20 x 4 I2C.
The Arduino Nano will be the slave (transmitter), it will read the DS18B20 sensor and send the measured value through the RS485 module.
Now let's go to the list of materials needed to set up the project.
The Project with DS18B20 and RS485 Module for ArduinoThe temperature sensor DS18B20 is a digital temperature sensor, because to transmit the information it uses the protocol 1 - Wire.
The 1 - Wire protocol was manufactured by Dallas Semiconductor and Maxim.
The 1 - Wire bus uses the master/slave concept for the devices.
The microcontroller is the master and the peripherals are the slaves.
In manufacturing, each device receives a unique ID which is the device identification number (address), so that it can be identified on the bus when there are many devices.
Information format:
The 1 - Wire uses only one data line and uses short and long pulses to represent 1s and 0s. A 60-microsecond pulse means a 0, a 15-microsecond pulse means a 1.
It is necessary to use a pull-up resistor of 4K7 between the VCC and the signal pin of the DS18B20 sensor so that the communication between the microcontroller and the sensor is stable.
After assembling the transmitter and receiver circuit on the protoboard, we can see the physical assembly on the protoboard in figure 6.
On the left, we have the receiver circuit and on the right the transmitter.
The Transmitter Circuit CodeNext, we have the complete code of the transmitting circuit.
#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");
}
}
For the Transmitter code, we will include 3 libraries: Serial Software, Dallas Temperature, and One Wire.
Serial software is responsible for emulating serial communication by software.
Dallas Temperature and One Wire libraries that have the necessary functions to read the DS18B20 temperature sensor.
#include <SoftwareSerial.h>
//bibliotecas para leitura do sensor DS18B20
#include <OneWire.h>
#include <DallasTemperature.h>
Mapeamento de hardware da ligação dos pinos do módulo RS 485 ao Arduino
#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
Cria o objeto RS485_serial com os pinos de RX e TX
SoftwareSerial RS485_serial (RX_485, TX_485); //Variável que irá armazenar o valor lido de temperatura para ser transmitida via serial.
byte TX [4]; //Constantes definidas para habilitar ou não a transmissão e recepção de dados via serial/RS485.
#define RS485transmit HIGH
#define RS485receive LOW
//Pino onde está conectado o sensor
const int pino_sensor = 5;
//Pino onde o sensor está conectado
OneWire bus (pino_sensor);
//Armazena o endereço do sensor
DallasTemperature sensors (&bus);
DeviceAddress sensor;
unsigned int tempo=0; Armazena o tempo corrente
//variavel para armazenar a temperatura
float temp;
int t=0;
In the void setup function, we initiate communication with the sensor, setting the serial communication speed and RS485.
Checks whether communication with the sensor was successful.
Configure the RS485 communication control pin as an output with the command: pinMode (RS485_control, OUTPUT).
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");
}
}
In the void loop function, the sensor is read every 200 ms using the millis( ) function and assembles the package to transmit the measured value.
if((millis()-tempo) > 200)
{
tempo=millis();
sensors.requestTemperatures(); método que requisita a temperatura
temp=sensors.getTempC(sensor); reaaliza a leitura da temperatura do sensor
digitalWrite(RS485_control, RS485transmit); habilita o modo de transmissão
t = temp * 100; Transforma o valor lido de temperatura para um valor inteiro.
TX[0] = t/ 255; Armazena na posição 0 o valor da divisão da temperatura por 255 ( 1 byte).
TX[1] = t % 255; Armazena na posição 1 o valor do resto da divisão de temperatura por 255.
This operation is performed so that we can divide the temperature value into two bytes, as it is not possible to transmit a float value directly using serial communication.
Transmits the value read via RS485 serial communication.
for (int i=0; i<4; i++)
{
RS485_serial.write(TX[i]);
}
Prints the value on the computer's serial port for debugging only.
Serial.print("Temperatura: ");
Serial.print(temp);
Serial.println(" oC");
}
}
Next, we will present the complete code of the receiving circuit.
The receiver circuit codeFirst, we have the complete code of the receiving circuit.
#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 ");
}
}
In the receiver, we will use three libraries Software Serial, Wire, and LiquidCrystal_I2C.
Software Serial emulates serial communication on any Arduino Nano pin.
Wire and LiquidCrystal_I2C libraries responsible for I2C communication and communication with the I2C LCD display.
#include <SoftwareSerial.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
Lcd object, in these objects we have to put the display address.
In my case the address is 0x27,
// Addr,En, Rw,Rs,d4,d5,d6,d7, backlight, polarity
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7,3,POSITIVE);
Hardware mapping with pins on the RS485 module connected to the Arduino Nano.
#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); //Pinos de comunicação serial RS485.
byte RX[4]; //Variavel que irá receber o valor de temperatura através da comunicação serial RS485.
//Constantes que habilitam ou não a transmissão e recepção de dados.
#define RS485transmit HIGH
#define RS485receive LOW
// variavel para armazenar a temperatura
float temp;
//Variável para armazenar o tempo
unsigned long tempo =0 ;
In the void setup we start the communication with the LCD display, we configure as a pin the output that enables or not the reception of data through RS485 communication. Write a message on the LCD display.
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 ");
}
In the void loop function, the temperature value is received every 500 ms.
void loop()
{
if((millis()-tempo)>500) Verifica se já passou 500 ms
{
lcd.clear();
tempo=millis(); Armazena o tempo atual
digitalWrite(RS485_control,RS485receive); habilita o modo de recepção de dados
if(RS485_serial.available () >0) Verifica se tem algum dado disponível na comunicação RS485
{
for (int i =0; i<4; i ++)
{
RX[i] = RS485_serial.read(); Leitura da serial RS485 e armazena os valores no vetor RX até a posição 4
}
Take byte 0 of the RX vector multiplies by 255 and adds up with byte 1 of the RX vector and divides by 100 to obtain the temperature value.
temp = ((RX[0] * 255) + RX[1]) /100; Conversão de temperatura
Serial.print(" Temperatura : ");
Serial.print( temp );
Serial.println(" *C ");
}
lcd.setBacklight(HIGH); //Liga o blacklight
lcd.setCursor(0,0); //Posiciona o cursor na linha 0 e coluna 0
lcd.print(" COMUNICACAO RS485 "); //Escreve a mensagem
lcd.setCursor(0,1); //Coluna 0 linha 1
lcd.print(" SENSOR DS18B20 "); //Escreve mensagem
lcd.setCursor(0,2); //Coluna 0 linha 2
lcd.print(" TEMP "); //Escreve a string Temp
lcd.setCursor(7,2); //Coluna 7 linha 2
lcd.print( temp); //Escreve o valor de temperatura
lcd.setCursor(11,2);
lcd.print(" *C ");
}
}
Next, we will see the result of the project.
Results of the ProjectAfter programming the transmitter and receiver, we will have the following results.
In this article, we learned how to use a new temperature sensor the DS18B20, and transmit its temperature value to another Arduino using RS485 communication.
In our article we do not use any proprietary protocol, we have developed a simple serial communication protocol to send and receive data.
A Big bonus of JLCPCB.COMThe JLCPCB want offer 10 units of this Arduino compatible PCB for your projectsfor$2 in your first order with the link:Earn my PCBs Arduino Compatible.
Comments