How to serialize data in string on 2 Arduinos

I have the code and connect it via serial to 2 Arduinos, why can't the writing appear on Arduino 1?

Arduino 1

void setup() {
 Serial.begin(9600);
}
void loop() {
  Serial.println(0);
}

Arduino 2

void setup() {
 Serial.begin(9600);
}
void loop() {
  if(Serial.available()) {
    char serialinput = Serial.read();
    if (serialinput =='0') {
      Serial.println("Example");
    }
    Serial.println(serialinput);
  }
}

have a ready through this tutorial:

hope that helps....

1 Like

Hello bimosora

Try this:

void setup() {
 Serial.begin(9600);
}
void loop() {
  Serial.println("0");
}

it doesn't work paulpaulson

I want to connect 2 arduinos, arduino 1 as a 16x2 lcd and arduino 2 as installed modules like temperature, etc.

Hello bimosora

You are right.

I need to have a second coffee in the morning.

You might code a second serial interface by using SoftwareSerial libary.

1. WIth Arduino UNO-1, you have connected a 2x16 LCD -- is it correct? Is this LCD an I2C LCD? Please, Show picture.

2. With Arduino UNO-2, you have conncetd a temperature tempertaue -- is it correct? What type of temperatue sensor it is -- LM35 or DS18B20 or BME280? Please, show the picture.

3. You want to connect them using UART Port -- is it correct? If so, you need to use software UART Ports (SUART Port) for both UNOs as the hardware UART ports are engaged with their respective Serial Monitor and PC.

You may try the following hardware setup (Fig-1) that uses SUART Ports:


Figure-1:

4. Do you want to acquire temperature signal and show on the LCD?

Arduino UNO-1 Sketch:
...pending

Arduino UNO-2 Sketch:
...pending

1 Like

I want to make 2 Arduino connections with serial, I want Arduino 1 to display string data which is processed on Arduino 2 then I select it using the serial input installed on Arduino 2 then it appears on Arduino 1

Below is my code that works correctly on digitalwrite, what if digitalwrite is changed to Serialprint("Example") or another string code that will be processed on a 16x2 LCD on Arduino 1

//Arduino 2

char serialinput  = ' ';
int in1 = 2;
int in2 = 3;
int in3 = 4;
int in4 = 5;
//int motor_speed = 0;
 
void setup(){
   pinMode(in1, OUTPUT);
   pinMode(in2, OUTPUT);
   pinMode(in3, OUTPUT);
   pinMode(in4, OUTPUT);
   Serial.begin(9600);
}
void loop(){
   if(Serial.available())
   {
      char serialinput = Serial.read();
      if (serialinput =='0') { digitalWrite(in1, HIGH); }
      if (serialinput =='1') { digitalWrite(in2, HIGH); }
      if (serialinput =='2') { digitalWrite(in3, HIGH); }
      if (serialinput =='3') { digitalWrite(in4, HIGH); }

      if (serialinput =='6') { digitalWrite(in1, LOW); }
      if (serialinput =='7') { digitalWrite(in2, LOW); }
      if (serialinput =='8') { digitalWrite(in3, LOW); }
      if (serialinput =='9') { digitalWrite(in4, LOW); }
      Serial.println(serialinput);
   }
}
//Arduino 1
void setup(){
  // Serial port for debugging purposes
  Serial.begin(9600);
  Serial.println();
}
 
void loop(){
  Serial.println(6);
  Serial.println(1);
}

that's decimal 48..
or Serial.println(48);
You're not enclosing the number in quotes, look up ascii chart..

Also sending at loop speed gonna jam things up pretty quick..
Wait for ACK

good luck.. ~q

show how you wired your boards.
if you connect both boards with UART, then you are not able to check if second board receive something or not, same with its answer "Example". you need second interface for yourself on one board or on both, where board will send messages for you.

try

//Arduino 1
void setup(){
  // Serial port for debugging purposes
  Serial.begin(9600);
}
 
void loop(){
  Serial.print('7');
  delay(1000);
  Serial.print('1');
  delay(1000);
}
//Arduino 2

const int in1 = 2;
const int in2 = 3;
const int in3 = 4;
const int in4 = 5;
 
void setup(){
   pinMode(in1, OUTPUT);
   pinMode(in2, OUTPUT);
   pinMode(in3, OUTPUT);
   pinMode(in4, OUTPUT);
   Serial.begin(9600);
}
void loop(){
   if(Serial.available()>0)   {
      char serialinput = Serial.read();
      if (serialinput =='0')  digitalWrite(in1, HIGH); 
      if (serialinput =='1')  digitalWrite(in2, HIGH); 
      if (serialinput =='2')  digitalWrite(in3, HIGH); 
      if (serialinput =='3')  digitalWrite(in4, HIGH); 

      if (serialinput =='6')  digitalWrite(in1, LOW); 
      if (serialinput =='7')  digitalWrite(in2, LOW); 
      if (serialinput =='8')  digitalWrite(in3, LOW); 
      if (serialinput =='9')  digitalWrite(in4, LOW); 
   }
}

Can it later appear on the serial monitor as low and high instead of 0 0 0?

of course

1. This is the Layout (Fig-1) of Serial Monitor.


Figure-1:

2. The OutputBox is a Text-type Monitor which means that -- if we want to see a charcater/digit on it, we must send ASCII Code (Fig-2) of that charcater/digit onto the Serial Monitor.


Figure-2:

3. By executing the above quoted line (Serial.println(6));, you probably have wanted to see 6 on the OutputBox of Serial Monitor; but, 6 has not appeared (will not appear). To see 6, you msut execute the following code that contains the ASCII code of 6 (0x36) as the argument.

Serial.println(0x36);
or
Serial.print('6'); // '6' will be converted into 0x36 (00110110); 0x for hex-base

Doesn't work

This is my code that I am trying currently

//Arduino 1
void setup() {
 Serial.begin(9600);
}
void loop() {
  Serial.write(0);
  delay(1000);
}
//Arduino 2
// wire i2c
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

// bme280
#include <SPI.h>
#include <Adafruit_BMP280.h>

#define BMP_SCK  (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS   (10)

Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);
// end

// bh1750
#include <BH1750.h>

BH1750 lightMeter;
//end

// tm1637
#include <TM1637Display.h>
#define CLK PB15
#define DIO PA8
TM1637Display display(CLK, DIO);
// end

const int ledPin =  PC15;
const int ledSTM32 =  LED_BUILTIN;
int ledState = HIGH;
int state ;

const int sensorMin = 0;     // sensor minimum
const int sensorMax = 4096;  // sensor maximum

int analogInput = PB0;
float vout = 0.0;
float vin = 0.0;
float R1 = 22000.0;
float R2 = 3000.0;
int value = 0;
int i = 0;

unsigned long previousMillis1 = 0;
const long interval1 = 8000;

unsigned long previousMillis2 = 0;
const long interval2 = 500;

unsigned long sensorMilis_1 = 0;
const long sensorinterval_1 = 500;

unsigned long sensorMilis_2 = 0;
const long sensorinterval_2 = 500;

const uint8_t maxvolt[] = {
  SEG_G,
  SEG_G,
  SEG_G,
  SEG_G
};

void setup() {
  analogReadResolution(12);
  Serial.begin(9600); // on PA9 PA10
  Wire.begin();
  lightMeter.begin();
  
  lcd.init();
  lcd.backlight();

  display.setBrightness(5);
  
  // Clear the display
  display.clear();

  pinMode(analogInput, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(ledSTM32, OUTPUT);
  digitalWrite(ledPin, HIGH);
  digitalWrite(ledSTM32, HIGH);
  
  if (!bmp.begin(0x76)) {
    Serial.println("Tidak ada sensor BME280, cek rangkaianmu!");
    while (1);
  }
  /*
  bmp.setSampling(Adafruit_BMP280::MODE_FORCED,         // Operating Mode
                  Adafruit_BMP280::SAMPLING_X1,         // Temp. oversampling
                  Adafruit_BMP280::SAMPLING_X1,         // Pressure oversampling
                  Adafruit_BMP280::FILTER_OFF);         // Filtering
                  // Adafruit_BMP280::STANDBY_MS_4000); // Standby time
  */
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */

  lcd.clear();
  for (i = 16; i >= 0; i--) //program increamental nilai i dari 0 sampai 15
  {
    lcd.setCursor(i, 0); //setting posisi kursor kolom sesuai nilai i
    lcd.print("Weather Station"); //menampilkan karakter
    delay(500); //menunda tampilan selama 500 ms
    lcd.clear(); //menghapus semua karakter
  }
}


void loop() {
  bmp.takeForcedMeasurement();

  if(Serial.available() > 0){
    char serialinput = Serial.read();
    if (serialinput =='0') {
      int suhu_udara = bmp.readTemperature();
      Serial.println(suhu_udara);
    }
    Serial.println(serialinput);
  }

  if(millis() - previousMillis1 >= interval1) {
    previousMillis1 = millis();
    state++;
  }
  
  if(state==4){
    state=1;
  }

  int sensorReading = analogRead(PA1);
  int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

  float lux = lightMeter.readLightLevel();
  
  switch (state){
    case 1:
      if(millis() - sensorMilis_1 >= sensorinterval_1) {
        sensorMilis_1 = millis();
        int suhu_udara = bmp.readTemperature();
        if(suhu_udara < 20){
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("SUHU");
          lcd.setCursor(0,1);
          lcd.print(suhu_udara);
          lcd.print(" *C!");
        }else if(suhu_udara > 40){
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("SUHU");
          lcd.setCursor(0,1);
          lcd.print(suhu_udara);
          lcd.print(" *C!");
        }else{
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("SUHU");
          lcd.setCursor(0,1);
          lcd.print(suhu_udara);
          lcd.print(" *C");
        }
      }
    break;
    case 2:
      if(millis() - sensorMilis_1 >= sensorinterval_1) {
        sensorMilis_1 = millis();
        int pressure = bmp.readPressure() / 100.0;
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("TEKANAN UDARA");
        lcd.setCursor(0,1);
        lcd.print(pressure);
        lcd.print(" hPa");
      }
    break;
    /*
    case 3:
        if(millis() - sensorMilis_1 >= sensorinterval_1) {
          sensorMilis_1 = millis();
          int ketinggian = bmp.readAltitude(1013.25);
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("KETINGGIAN");
          lcd.setCursor(0,1);
          lcd.print(ketinggian);
          lcd.print(" m");
        }
    break;
    */
    case 3:
      if(millis() - sensorMilis_2 >= sensorinterval_2) {
        sensorMilis_2 = millis();
          switch (range){
            case 0:
              lcd.clear();
              lcd.setCursor(0,0);
              lcd.print("CURAH HUJAN");
              lcd.setCursor(0,1);
              lcd.print("HUJAN");
            break;
            case 1:
              lcd.clear();
              lcd.setCursor(0,0);
              lcd.print("CURAH HUJAN");
              lcd.setCursor(0,1);
              lcd.print("PERINGATAN HUJAN");
            break;
            case 2:
              lcd.clear();
              lcd.setCursor(0,0);
              lcd.print("CURAH HUJAN");
              lcd.setCursor(0,1);
              lcd.print("TIDAK HUJAN");
            break;
          }
      }
    break;
  }
  if(lux < 100){
    if (millis() - previousMillis2 >= interval2) {
      // save the last time you blinked the LED
      previousMillis2 = millis();
    
        // if the LED is off turn it on and vice-versa:
        if (ledState == HIGH) {
          ledState = LOW;
        } else {
          ledState = HIGH;
        }
    
        // set the LED with the ledState of the variable:
        digitalWrite(ledSTM32, ledState);
    }
    digitalWrite(ledPin, HIGH);
  }else if(lux > 100){
    digitalWrite(ledSTM32, HIGH);
    digitalWrite(ledPin, LOW);
  }
}

Screenshot pada 2023-09-15 13-50-49

i have updated post#10.

 digitalWrite(ledSTM32, !digitalRead(ledSTM32) );

Screenshot pada 2023-09-15 13-55-03
No changes

What I want is that this code can be read on Arduino 1 as a string

int suhu_udara = bmp.readTemperature();

you show pictures where is shown what you send. what you want more?

Arduino1 is a sender, Arduino2 is receiver with display, isn't they?

and what string do you mean?

I put this code on Arduino 2

  if(Serial.available() > 0){
    char serialinput = Serial.read();
    if (serialinput =='0') {
      int suhu_udara = bmp.readTemperature();
      Serial.println(suhu_udara);
    }
    Serial.println(serialinput);
  }

and I want this line to appear on Arduino 1 either in serialprint, lcd.print or something else

int suhu_udara = bmp.readTemperature();

if(Serial.available() > 0){
    char serialinput = Serial.read();
    if (serialinput =='0') {
      int suhu_udara = bmp.readTemperature();
      Serial.println(suhu_udara);
    }
    Serial.println("int suhu_udara = bmp.readTemperature();"); //  ?
  }