Two different components, two different baud rates

Hi guys once again....

Now I have a very interesting problem.

So.

I have MLX90614-sensor and PCF8523-RTC-clock.

I´m trying to connect these together, but...

MLX90614 uses 9600 baud rate.
PCF8523 uses 57600 baud rate.

So.

How can I code these working together in serial monitor?

Both of them works fine when I use only one component and comment out other component codes, but when I put those together, I don´t know what to do.

In the following code, there are now two void setups because I´m so f... lost now guys :smiley:

There is my code:


        // Alkaa MLX90614-sensorin määritys
        #include <Adafruit_MLX90614.h>
        Adafruit_MLX90614 mlx = Adafruit_MLX90614();
        void setup() {
        Serial.begin(9600);
        while (!Serial);
        if (!mlx.begin()) {
        Serial.println("Yhdistys MLX sensoriin ei onnistunut. Tarkista kytkentä savun halvettya.");
        while (1);
        };
        Serial.println("================================================");
        }
        // Tähän loppuu sensorin määritys 






  // Tästä alkaa RTC-kellon määritys
  #include "RTClib.h"
  RTC_PCF8523 rtc;  

  char daysOfTheWeek[7][12] = {"Sunnunta", "Maanantai", "Tiistai", "Keskiviikko", "Torstai", "Perjantai", "Lauantai"};      // Määritetään viikonpäivät

  void setup () {
  Serial.begin(57600);   // HOX! Muista vaihtaa baud sarjamonitorista arvoon 57600!

  #ifndef ESP8266
  while (!Serial); // Tarkistetaan USB-yhteys
  #endif

  if (! rtc.begin()) {
  Serial.println("RTC-kello hukassa, tarkista kytkentä!");
  Serial.flush();
  while (1) delay(10);
  }

  if (! rtc.initialized() || rtc.lostPower()) {
  Serial.println("RTC ei ole asennettu, asetetaan seuraavaksi aika ja päivämäärä!");
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));   // Päivämäärän ja kellonajan määritys, jos RTC:stä on katkennut virta, tai patteri on irrotettu (muisti hävinnyt)
                                                      // Päivämäärä ja kellonaika asetetaan muodossa (2021,12,22, 14,53,32)
  }
  rtc.start();  // Tähän asti päästyä RTC on asentunut ja toimii oikein, joten voidaan aloittaa varsinainen ohjelma

  float drift = 43; // Kalibroi sekunnit 
  float period_sec = (7 * 86400);  // (86400 = sekunnit per päivä 1:  7 päivää = (7 * 86400) sekuntia )
  float deviation_ppm = (drift / period_sec * 1000000); //  Sekunnin poikkeama miljoonasosina
  float drift_unit = 4.34; // OFFSET-toiminto PCF8523_TwoHours-käytössä
  int offset = round(deviation_ppm / drift_unit);
 

  Serial.print("Offset is "); Serial.println(offset); // Offsetin arvo
  }
  // Tähän loppuu RTC-kellon määritys







    
        void loop () {
        DateTime now = rtc.now();
        
        Serial.print("Paivamaara: ");
        (Serial.print(now.year() , DEC )) ;
        Serial.print("-");
        (Serial.print(now.month(), DEC )) ;
        Serial.print("-");
        (Serial.print(now.day(), DEC));
        Serial.print("\n");


        Serial.print("Kellonaika: ");
        (Serial.print(now.hour(), DEC)) ; 
        Serial.print(":");
        (Serial.print(now.minute(), DEC)) ; 
        Serial.print(":");
        (Serial.print(now.second(), DEC));
        Serial.print("\n");
        
       
        Serial.print("Lampotila: \n");
        Serial.print("Ymparisto = "); Serial.print(mlx.readAmbientTempC());
        Serial.print(" \xC2\xB0"); Serial.print("C\tKohde = ");
        Serial.print(mlx.readObjectTempC()); Serial.print(" \xC2\xB0");
        Serial.println("C");Serial.print("            ");Serial.print(mlx.readAmbientTempF());
        Serial.print(" \xC2\xB0");Serial.print("F\t        ");
        Serial.print(mlx.readObjectTempF());Serial.print(" \xC2\xB0");
        Serial.println("F");
        Serial.println();
        
        if(mlx.readObjectTempC() > -20){
        tone(9, 200, 300);
        } 
        if(mlx.readObjectTempC() > -10){
        tone(9, 200, 300);
        } 
        if(mlx.readObjectTempC() > 0){
        tone(9, 200, 300);
        } 
        if(mlx.readObjectTempC() > 10){
        tone(9, 230, 300);
        }
        if(mlx.readObjectTempC() > 25){
        tone(9, 262, 300);
        }
        if(mlx.readObjectTempC() > 26){
        tone(9, 294, 300);
        }
        if(mlx.readObjectTempC() > 27){
        tone(9, 330, 300);
        }
        if(mlx.readObjectTempC() > 28){
        tone(9, 349, 300);
        }
        if(mlx.readObjectTempC() > 60){
        tone(9, 392, 300);
        }
        if(mlx.readObjectTempC() > 70){
        tone(9, 200, 300);
        }  
        

  
        Serial.print("\n");
        delay(1000); }

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

The baud rate set in Serial.begin(9600) it's just for the pc communication.
What's the problem?
If you need 2 different serial comunication, you can use softwareSerial library

Oh I open this up.

So.

What I´m trying to do is to show measuring temperature (MLX90614) with time and date (PCF8523) in Serial Monitor, because when I get this working, I can move to the next step, which is to show this data in 2,8" TFT screen.

I think you are taking 2 different code and paste them together.
You don't have to care about communication between Arduino (or ESP) board and sensor/rtc board because it's handled by their library.
The serial rate is just a paremeter for the communication between pc and Arduino board

Ok so stupid question, how do I paste codes?

void loop () {
        DateTime now = rtc.now();
        
        Serial.print("Paivamaara: ");
        (Serial.print(now.year() , DEC )) ;
        Serial.print("-");
        (Serial.print(now.month(), DEC )) ;
        Serial.print("-");
        (Serial.print(now.day(), DEC));
        Serial.print("\n");


        Serial.print("Kellonaika: ");
        (Serial.print(now.hour(), DEC)) ; 
        Serial.print(":");
        (Serial.print(now.minute(), DEC)) ; 
        Serial.print(":");
        (Serial.print(now.second(), DEC));
        Serial.print("\n");
        
       
        Serial.print("Lampotila: \n");
        Serial.print("Ymparisto = "); Serial.print(mlx.readAmbientTempC());
        Serial.print(" \xC2\xB0"); Serial.print("C\tKohde = ");
        Serial.print(mlx.readObjectTempC()); Serial.print(" \xC2\xB0");
        Serial.println("C");Serial.print("            ");Serial.print(mlx.readAmbientTempF());
        Serial.print(" \xC2\xB0");Serial.print("F\t        ");
        Serial.print(mlx.readObjectTempF());Serial.print(" \xC2\xB0");
        Serial.println("F");
        Serial.println();
        
        if(mlx.readObjectTempC() > -20){
        tone(9, 200, 300);
        } 
        if(mlx.readObjectTempC() > -10){
        tone(9, 200, 300);
        } 
        if(mlx.readObjectTempC() > 0){
        tone(9, 200, 300);
        } 
        if(mlx.readObjectTempC() > 10){
        tone(9, 230, 300);
        }
        if(mlx.readObjectTempC() > 25){
        tone(9, 262, 300);
        }
        if(mlx.readObjectTempC() > 26){
        tone(9, 294, 300);
        }
        if(mlx.readObjectTempC() > 27){
        tone(9, 330, 300);
        }
        if(mlx.readObjectTempC() > 28){
        tone(9, 349, 300);
        }
        if(mlx.readObjectTempC() > 60){
        tone(9, 392, 300);
        }
        if(mlx.readObjectTempC() > 70){
        tone(9, 200, 300);
        }  

In the loop I need to use both components :smiley:

And you can do it.
Have you already try the code?

yup, "redefinition of ´void setup()´

Indeed you have 2 SETUP routine.

yup, that is exactly my problem, how do I unite these?

Just copy one inside the other.

void setup () {
        Serial.begin(9600);
        while (!Serial);
        if (!mlx.begin()) {
        Serial.println("Yhdistys MLX sensoriin ei onnistunut. Tarkista kytkentä savun halvettya.");
        while (1);
        };
        Serial.println("================================================");
if (! rtc.begin()) {
  Serial.println("RTC-kello hukassa, tarkista kytkentä!");
  Serial.flush();
  while (1) delay(10);
  }

  if (! rtc.initialized() || rtc.lostPower()) {
  Serial.println("RTC ei ole asennettu, asetetaan seuraavaksi aika ja päivämäärä!");
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));   // Päivämäärän ja kellonajan määritys, jos RTC:stä on katkennut virta, tai patteri on irrotettu (muisti hävinnyt)
                                                      // Päivämäärä ja kellonaika asetetaan muodossa (2021,12,22, 14,53,32)
  }
  rtc.start();  // Tähän asti päästyä RTC on asentunut ja toimii oikein, joten voidaan aloittaa varsinainen ohjelma

  float drift = 43; // Kalibroi sekunnit 
  float period_sec = (7 * 86400);  // (86400 = sekunnit per päivä 1:  7 päivää = (7 * 86400) sekuntia )
  float deviation_ppm = (drift / period_sec * 1000000); //  Sekunnin poikkeama miljoonasosina
  float drift_unit = 4.34; // OFFSET-toiminto PCF8523_TwoHours-käytössä
  int offset = round(deviation_ppm / drift_unit);
 

  Serial.print("Offset is "); Serial.println(offset); // Offsetin arvo
  }

Oh, now I feel stupid...

Is it working?

It doesn´t give an error anymore, but it doesn´t show anything in serial monitor :yawning_face:

What do you mean with "does't show anything"?
It's completely blank or show strage characters?

There was typing error, my bad :rofl:

Now I have next problem.

I have a Mcufriend Blue Shield (2.8" touch screen) with SD-card, and when I´m trying to write to card, it doesn´t work right...

//Ohjelma, jolla voi lukea ja kirjoittaa dataa SD-kortilta.

#include <SD.h>
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;

#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>

#include <FreeDefaultFonts.h>

#define BLACK   0x0000
#define RED     0xF800
#define GREEN   0x07E0
#define WHITE   0xFFFF
#define GREY    0x8410
#define BLUE    0x001F
#define CYAN    0x07FF
#define YELLOW  0xFFE0
#define ORANGE  0xFBE0
#define MAGENTA 0xF81F
#define BROWN   0x79E0
#define PINK    0xF81F


File myFile;

const int chipSelect = 4;


void setup()
{
  Serial.begin(9600);
  uint16_t ID = tft.readID();
  Serial.print("ID loydetty = 0x");
  Serial.println(ID, HEX);
  tft.begin(ID);
  tft.setRotation(1); //näytön suunnan määritys, 0 = pysty, 1 = vaaka, 2 = ylösalaisin, 3 = vaaka toisin päin
  while (!Serial) {
    
  }


  tft.print("Initializing SD card...");
 
  // SS pin = 10 Arduino unossa ja megassa SS pin = 53 (output)
  // muuten SD kirjasto ei toimi.
   pinMode(SS, OUTPUT);
   
  if (!SD.begin(chipSelect)) {
    tft.println("initialization failed!");
    return;
  }
  tft.println("initialization done.");
  // avaa tiedoston. Ohjelmalla voi avata vain yhden tiedoston kerralla.,
  
  myFile = SD.open("test3.txt", FILE_WRITE);
  
  // jos, tiedosto toimii oikein ohjelma tulostaa:
  if (myFile) {
    tft.print("Writing to test3.txt...");
    myFile.println("sd kortti");
    myFile.flush();
    
  // tiedosto sulkeutuu.
    myFile.close();
    tft.println("done.");
  } else {
    // jos, tiedosto ei aukea ohjelma tulostaa error.
    tft.println("error opening test3.txt");
  }
  
  // ohjelma aukeaa lukemista varten.:
  myFile = SD.open("test3.txt");
  if (myFile) {
    Serial.println("test3.txt:");
    tft.println("test3.txt:");
    
    while (myFile.available()) {  // ohjelma lukee tiedoston, ja kirjoittaa sen sarjamonitoriin:
      Serial.write(myFile.read());
      tft.fillScreen(BLACK);
      tft.setTextColor(WHITE);
      tft.setTextSize(2);
      tft.setCursor(10, 20);
      tft.print(myFile.read());
      myFile.flush();
      
    }
    // close the file:
    myFile.close();
  } else {
    // jos, tiedosto ei aukea ohjelma tulostaa error:
    tft.println("error opening test3.txt");
  }
}


void loop()
{
  
}

Do you see errors in code?

Details please

as @UKHeliBob said, we need more information...

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