How use RS485 with ardunio to transfer data of sensor

I am hoping to use only two signal data lines of 3 sensors and send them one after the other using RS485. This was done to check whether it can be sent to an order first. Can you correct it?

int enablePin = 8;  

int pushval = A0;
int pushva2 = A1;
int pushva3 = A2;

int potval =0 ;
int potva = 0;
int i =0;

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20, 16,2);


void setup()

{

  Serial.begin(9600);            // initialize serial at baudrate 9600:

  pinMode(enablePin, OUTPUT);

  pinMode(pushval,INPUT); 
  pinMode(pushva2,INPUT); 
  pinMode(pushva3,INPUT); 

  digitalWrite(enablePin, HIGH);  //  (always high as Master Writes data to Slave)

  lcd.init();
  lcd.backlight(); 
  lcd.print("Hello, world!");
  delay(1000);
  lcd.clear();
}

void loop()

{   
     
 // int potval = analogRead(pushval);
 // int potva2 = analogRead(pushva2);
 // int potva3 = analogRead(pushva3);

  for(i=0; i<3; i++){
    if(i==0)
     int potva = analogRead(pushval);
     Serial.println(potva); 
     lcd.clear();
     lcd.setCursor(0,0);
     lcd.print(potva);
     
    if(i==1)
     int potva = analogRead(pushval);
     Serial.println(potva); 
     lcd.clear();
     lcd.setCursor(0,0);
     lcd.print(potva);
     
    if(i==2)
     int potva = analogRead(pushval);
     Serial.println(potva);   
     lcd.clear();
     lcd.setCursor(0,0);
     lcd.print(potva);

  delay(300);

  }
}
![2518262626|690x400](upload://tF8EL4dJ53UI3QgRQnHtl61MkI6.png)
![14616446130|690x406](upload://niU3pu5hjkgpVHUtFL4D3VG7rpK.png)

Not without knowing what doesn't work as expected.

Show us a schematic of your connections.
Which RS485 board do you have.
What is receiving the data you are trying to send.
Which Arduino do you have.

Is it an UNO? You should use a software serial port if it is, so that the hardware serial port is free for debugging etc.

You might need to edit your post if you intended to upload images as it appears they have been put inside your code block.

My main purpose is to connect SHT 21 and three MQ sensors esp32. But the sensor unit is located 5m away. So I think it is not possible to use arduino uno with an ADS 1115 unit through the i2c protocol. And only two data lines should be used between the sensor unit and the esp32 chip unit.

Therefore, the MQ sensor signal is converted into a digital signal by arduino uno and the signal of the SHT 21 is expected to be sent to the eso32 chip unit through RS485. now i have do it to one sensor,

U.S If it is not possible, can you suggest a suitable method for it?



aside from the if statements lacking braces resulting in the LCD being updated each iteration, what exactly is the problem? is there data being received?

Yes, it can be done, quite easily. There are libraries available to do the under-the-covers management of the bus direction. Look for "auto485.h" on the web and read the examples. There are also threads here on the forum, though I've not read through them to see if there are any issues with it. I haven't experienced any.

LCD not update with POT2 and POT3 only change with POT1

int enablePin = 8;  

int pushval = A0;
int pushva2 = A1;
int pushva3 = A2;

int potval =0 ;
int potva = 0;
int i =0;

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20, 16,2);


void setup()

{

  Serial.begin(9600);            // initialize serial at baudrate 9600:

  pinMode(enablePin, OUTPUT);

  pinMode(pushval,INPUT); 
  pinMode(pushva2,INPUT); 
  pinMode(pushva3,INPUT); 

  digitalWrite(enablePin, HIGH);  //  (always high as Master Writes data to Slave)

  lcd.init();
  lcd.backlight(); 
  lcd.print("Hello, world!");
  delay(1000);
  lcd.clear();
}

void loop()

{   
     
 // int potval = analogRead(pushval);
 // int potva2 = analogRead(pushva2);
 // int potva3 = analogRead(pushva3);

 for(i=0; i<3; i++){
    if(i==0);{
     int potva = analogRead(pushval);
     Serial.println(potva); 
     lcd.clear();
     lcd.setCursor(0,0);
     lcd.print(potva);
     
    }

    if(i==1);{
     int potva = analogRead(pushval);
     Serial.println(potva); 
     lcd.clear();
     lcd.setCursor(0,0);
     lcd.print(potva);
    }

     
    if(i==2);{
     int potva = analogRead(pushval);
     Serial.println(potva);   
     lcd.clear();
     lcd.setCursor(0,0);
     lcd.print(potva);
    }

     
   delay(300);

  }
}

Your three analog reads in loop() read the same pin, write to the same location on the LCD. Why?
As well, you declare new local variables (int potva) for each read, even though you have a global by the same name. Why? Frankly, there are more 'why?' questions to be answered, but that will do for a start.

doubt you want the semicolon. it becomes the body of the if statement. the following block in braces becomes unconditional

1 Like

thank you, now it ok, can you help me next step

#include <LiquidCrystal.h>   

LiquidCrystal lcd(2,3,4,5,6,7);    // set the LCD address to 0x27 for a 16 chars and 2 line display
int enablePin = 8; 
int ledpin = 10;
int i =0 ;

#include <Wire.h>
#include <Adafruit_MCP4725.h>
Adafruit_MCP4725 dac;
// Set this value to 9, 8, 7, 6 or 5 to adjust the resolution
#define DAC_RESOLUTION    (9)

void setup() 

{

  lcd.begin(16,2);
  lcd.print("CIRCUIT DIGEST");
  lcd.clear();

  delay(50);

  Serial.begin(9600);                   // initialize serial at baudrate 9600:
  pinMode(ledpin,OUTPUT);
  pinMode(enablePin, OUTPUT);

  Serial.println("MCP4725A1 Test");
  // MCP4725A1 address is 0x62 (default) 
  // MCP4725A1 address is 0x63 (ADDR pin tied to VCC) 
  // MCP4725A1 address is 0x60 (ADDR pin tied to GND) 
  dac.begin(0x60); //I have my ADDR pin connected to GND so address is 0x60

  delay(10);

  digitalWrite(enablePin, LOW);        //  (Pin 8 always LOW to receive value from Master)
}


void loop() 


{                                                  
  while (Serial.available())                   //While have data at Serial port this loop executes
   {
      lcd.clear();
      int pwmval = Serial.parseInt();            //Receive INTEGER value from Master throught RS-485
      for(i=0; i<3; i++){
       if(i==0){
         int adc0 = pwmval;
         Serial.println(adc0); 
         lcd.clear();
         lcd.setCursor(0,0);
         lcd.print(adc0);
       }

       if(i==1){
         int adc1 = analogRead(pwmval);
         Serial.println(adc1); 
         lcd.clear();
         lcd.setCursor(0,0);
         lcd.print(adc1);
       }

       if(i==2){
         int adc2 = analogRead(pwmval);
         Serial.println(adc2);   
         lcd.clear();
         lcd.setCursor(0,0);
         lcd.print(adc2);
       }
      delay(50);
                 
      }

   }


}

this code my SLAVE CODE, this section my target divide the signal according to POT and provide to MOP4725, it part ok but i manly need divide the signal correctly

Still writing the values to the same point in the LCD. Try three different locations, like this:

        lcd.setCursor(0,0);
         lcd.print(adc0);
and
         lcd.setCursor(0,5);
         lcd.print(adc1);
and
        lcd.setCursor(0,10);
         lcd.print(adc2);

also, move the lcd.clear() to the top of the loop(), and only use it once,not before every lcd.print(). Your wiping the screenb between every write.

consider this approach

char s [80];

void loop ()
{
    if (Serial.available ()) {
        lcd.clear ();
        int pwmval = Serial.parseInt ();    // read ADC port #

        sprintf (s, " %d %d %d", pwmval, analogRead (pwmval), analogRead (pwmval));

        lcd.clear ();
        lcd.setCursor (0,0);
        lcd.print (s);
    }
}
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_ADS1X15.h>
Adafruit_ADS1115 ads;
//LCD===================================================================
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20, 16,2);

#define DAC_ADDRESS 0x61 // MCP4725 address for DAC 1, 2, 3
#define DAC1_ENABLE 2
#define DAC2_ENABLE 3
#define DAC3_ENABLE 4

//=====================================================================



void setup() {
  Serial.begin(9600);
  Wire.begin(); // initialize I2C bus

  lcd.init();
  lcd.backlight(); 
  analogReference(INTERNAL);
  ads.begin();

  pinMode(DAC1_ENABLE, OUTPUT);
  pinMode(DAC2_ENABLE, OUTPUT);
  pinMode(DAC3_ENABLE, OUTPUT);

}

void loop() {
  int16_t adc0, adc1, adc2;
  float Vadc0, Vadc1, Vadc2;
  
  adc0 = ads.readADC_SingleEnded(0);
  adc1 = ads.readADC_SingleEnded(1);
  adc2 = ads.readADC_SingleEnded(2);

  Vadc0 = (adc0 * 0.1875)/1000;
  Vadc1 = (adc1 * 0.1875)/1000;
  Vadc2 = (adc2 * 0.1875)/1000;

  int convert0 = map(adc0,0,26640,0,4095);
  int convert1 = map(adc1,0,26640,0,4095);
  int convert2 = map(adc2,0,26640,0,4095);

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(adc0);
  lcd.setCursor(8,0);
  lcd.print(convert0);
  lcd.setCursor(0,1);
  lcd.print(Vadc0);
  lcd.setCursor(8,1);

  // send values to DACs
  writeDAC(DAC1_ENABLE, adc0); // set DAC1 to maximum value (1023)
  writeDAC(DAC2_ENABLE, adc1); // set DAC2 to half of maximum value
  writeDAC(DAC3_ENABLE, adc2); // set DAC3 to minimum value (0)

  delay(1000); // wait for 1 second before changing values
}

void writeDAC(byte dacEnablePin, int value) {

  digitalWrite(dacEnablePin, HIGH);
  // write the value to the DAC
  Wire.beginTransmission(DAC_ADDRESS);
  Wire.write(64); // send control byte
  Wire.write(highByte(value)); // send most significant byte of value
  Wire.write(lowByte(value)); // send least significant byte of value
  Wire.endTransmission();
  digitalWrite(dacEnablePin, LOW);

}
my next is combine this code with my previous code. therefore i need previous code adc0, adc1, adc2 get outside of for loop, i can't do  it using this step, how it do

for(i=0; i<3; i++){
if(i==0){
int adc0 = pwmval;
Serial.println(adc0);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(adc0);
delay(10);
}

   if(i==1){
     int adc1 = analogRead(pwmval);
     Serial.println(adc1); 
     lcd.clear();
     lcd.setCursor(0,0);
     lcd.print(adc1);
     delay(10);
   }

   if(i==2){
     int adc2 = analogRead(pwmval);
     Serial.println(adc2);   
     lcd.clear();
     lcd.setCursor(0,0);
     lcd.print(adc2);
     delay(10);
   }
   
   //int convert = map(pwmval,0,1023,0,255);    //Map those value from (0-1023) to (0-255)
   //int DV = (convert*5)/255;        
  // dac.setVoltage((DV*4124)/5, false);        //Set voltage to 1V
  // analogWrite(ledpin,convert);               //PWM write to LE       


   lcd.setCursor(0,1);
   lcd.print(adc0); 
  //  lcd.setCursor(10,1);
   // lcd.print(DV);                        //Displays the PWM value
  }

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