element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum pls how do i go about communicating with 2 arduinos with the use of the tx/rx pin.
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 17 replies
  • Subscribers 392 subscribers
  • Views 1641 views
  • Users 0 members are here
Related

pls how do i go about communicating with 2 arduinos with the use of the tx/rx pin.

Former Member
Former Member over 10 years ago

i have codes for pulse sensor, temp sensor, GPS  and i need the readings from the sensors and gps to be sent to the other arduino's LCD. i just dont know how to go about doing it and i was wondering if there is someone that could help guide me.

 

//sender

int inpin=A0;

int TEMP=0;

int heartratepin=A1;

int difference;

int newHeartReading = 0;

int lastHeartReading = 0;

int measurements[5] = {0,0,0,0,0};

int historySize = 5;

int recenttotal = 0;

int Index = 0;

boolean highChange = false;

int minimumdifference = 2;

int num[5];

int average=0;

 

 

// Heart rate timing

long lastHeartbeatTime = 0;

long debounceDelay = 150;       // the debounce time; increase if the output flickers

int currentHeartrate = 0;

int lastHeartrate = 0;

//Heart rate LED

//int ledState = LOW;

//long ledOnMillis = 0;

//long ledOnInterval = 50;

int slope = ((newHeartReading-lastHeartReading)/(debounceDelay-lastHeartrate));

 

 

 

 

 

 

// Debugging string

String debugOutput = "";

void setup() {

   // start serial port at 9600 bps:

   Serial.begin(9600);

    pinMode(heartratepin,INPUT);

   establishContact();

}

void establishContact() {

while (Serial.available() <= 0) {

      Serial.println("hello");   // send a starting message

      delay(300);

   }

}

void loop() {

   // read analog input, divide by 4 to make the range 0-255:

   int analogValue = analogRead(inpin);

   float millivolts = (analogValue/1024.0)*5000;

   float celsius = millivolts/10-50;

   Serial.println(analogValue);

   Serial.print(celsius);

   Serial.print("c");

   Serial.print((celsius*9)/5+32);

   Serial.print("F");

   newHeartReading = analogRead(heartratepin);

    //calculation

  difference = newHeartReading - lastHeartReading;

  lastHeartReading = newHeartReading;

 

  // Find new recent total

  recenttotal = recenttotal - measurements[Index] + difference;

  // replace indexed recent value

  measurements[Index] = difference;

   //index + 1 index = (index + 1) % historySize;

    for (int i=0; i<5; i=i+1){

               num[i]=0;        

   }

    

              if (Index>=5)

              Index=0;

              //calculating the average

              average=recenttotal/5;

              Serial.println(average);

              delay(10);

  if (recenttotal >= minimumdifference)

  {

    // possible heart rate check timing

    if (millis() - lastHeartbeatTime >= debounceDelay)   // whatever the reading is at, it's been there for longer

    // than the debounce delay, so take it as the actual current state:

 

 

    {

  // Heart rate

// digitalWrite(ledPin, HIGH);

  currentHeartrate = 60000 / (millis() - lastHeartbeatTime);

    lastHeartbeatTime = millis();

   Serial.println(newHeartReading);

      if (currentHeartrate <= 200)// && currentHeartrate > 20)

    { 

                 

                  Serial.print("Heart Rate =" + String(currentHeartrate) + "  ");

                  delay(10);

    }

            if (currentHeartrate <= 39 and currentHeartrate >= 0){

                

                  Serial.print("I Am Sick");

                }

                 delay(10);

          if (currentHeartrate<= 79 and currentHeartrate >= 70){

                

                  Serial.print("Night Mare");

                }

                delay(10);

            if (currentHeartrate <= 69 and currentHeartrate >= 40){

               

                  Serial.print("kid Is At Sleep");

          }

              delay(10);

              if (currentHeartrate <= 99 and currentHeartrate >= 80){

                 

                  Serial.print("Kid Is Resting");

             }

                delay(10);

                if (currentHeartrate <= 119 and currentHeartrate >= 100){

                

                  Serial.print("Kid Is Playing");

               }

             

                  if(currentHeartrate > 120){

                   

                    Serial.print(" The Kid Is Having ");

                    //lcd.setCursor(0,1);

                    //lcd.print(" A Heart Attack");

                }

  

  

 

 

   // print different formats:

   Serial.write(analogValue);  // Print the raw binary value

   Serial.print('\t');             // print a tab

   Serial.write(newHeartReading);  // Print the raw binary value

   Serial.print('\t');             // print a tab

 

}

  }

}

 

 

 

//receiver

#include <SPI.h>

#include <LiquidCrystal.h>

int heartratepin=A1;

int inpin=A0;

LiquidCrystal lcd(30,31,32,33,34,35);

void setup()

{

  lcd.begin(16,2);

  Serial.begin(9600);

}

void loop()

{

  if(Serial.available()>0)

  {

    int inByte=Serial.read();

    int analogValue=analogRead(inpin);

    lcd.print(analogValue);

    lcd.print(",");

   

    int newHeartReading=analogRead(heartratepin);

    lcd.print(newHeartReading);

    lcd.print(",");

   

   

 

 

  }

}

 

i dont know what i am doing wrong

  • Sign in to reply
  • Cancel

Top Replies

  • bobcroft
    bobcroft over 10 years ago in reply to Former Member +2
    Richard, in order to see the Easy Transfer library just Google Arduino Easy Transfer library. There are informative notes and code examples. To answer your other question, yes you can display the data…
  • clem57
    clem57 over 10 years ago +1
    ricardo240 I cannot comment on the sender code since it has many things going on. But here are the things I would do: Make sure the pins are connected as TX to RX on the UART on sender and UART on receiver…
  • clem57
    clem57 over 10 years ago in reply to Former Member +1
    To understand: 1) the sensors are reading fine. 2) The UNO sends on UART to the MEGA UART but does not work. What pins are connected on each to the other? 3) The code you sent looks like snippets especially…
  • clem57
    0 clem57 over 10 years ago

    ricardo240

         I cannot comment on the sender code since it has many things going on. But here are the things I would do:

    1. Make sure the pins are connected as TX to RX on the UART on sender and UART on receiver. This hooks the sender to the receiver in both directions.

      TX   ======>   RX
      RX  <======   TX

    2. Check the pins are correct for serial communication.
    3. Serial is your debugging to USB to windows, Serial1 is the UART on Arduino. I would expect these:
        Serial.begin(9600); <== USB and Serial1.begin(9600); <=== Arduino other
    4. Where is the setup/includes? The serial interface needs to be defined saying which pins to use.

    Clem

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • gadget.iom
    0 gadget.iom over 10 years ago

    What model of Arduino are you using?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to gadget.iom

    Am using a uno for the sender and a mega for the receiver

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • clem57
    0 clem57 over 10 years ago in reply to Former Member

    On the UNO, can you use the UART and have no debugging code or USB support? Paul raises a valid concern because of limited UART.

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to clem57

    Yes, i am using a 9v battery source to power to uno so that i wouldnt have to use the usb support as the powering source

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to clem57

    The reading is taking place, am just having problems getting it to the mega for it to be displayed on the lcd screen

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • clem57
    0 clem57 over 10 years ago in reply to Former Member

    To understand:

    1) the sensors are reading fine.

    2) The UNO sends on UART to the MEGA UART but does not work. What pins are connected on each to the other?

    3) The code you sent looks like snippets especially receiver and guessing not complete. Can you attach full sketch from both? If you wish put in private blog between us only.

    4) Does the serial code work on the UNO when sent to USB? If it does that would resolve half the issue.

    5) Did you check anything from my first comment?

    Clem

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • neilk
    0 neilk over 10 years ago

    ricardo240  Hi Richard,

     

    Your problem seems to me to be the classic one of "can't see the wood for the trees"

     

    I assume that you can read the sensors correctly AND display the results on the Serial Monitor of the UNO

    I assume that you can correctly display similar data on the LCD connected to your MEGA.

     

    So, if I were you I would start a NEW simple sketch to transmit a SIMPLE KNOWN item of data from the UNO to the MEGA, displaying the received item of data on the MEGA Serial Monitor.

     

    Once you have achieved this, using the help that clem57 has already offered you, then you can make the data more complex and so develop the code in a stepwise fashion..

     

    It's how I do all my development work - identify the most difficult component and solve that FIRST.

     

    Hope this helps

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to clem57

    1. yes the sensors are working

    2. yes, i am using on the uno pin 0 and 1 for the tx and rx. on the mega i am using pin 18 and 19 for tx and rx

    3.i will send u the code now

    4.yes it does work with the usb connect it self

    5.yes i did.

     

     

    i started with the temp sensor alone

     

     

    #include <SoftwareSerial.h>

     

     

    //reciever

     

     

    #include <SPI.h>

    #include <LiquidCrystal.h>

    SoftwareSerial mySerial(1,0);

    int inpin=A0;

    LiquidCrystal lcd(30,31,32,33,34,35);

    void setup()

    {

      lcd.begin(16,2);

      Serial.begin(9600);

      //Serial1.begin(9600);

    establishContact();

    }

    void establishContact() {

    while (Serial.available()== 1) {

         // Serial.println("hello");   // send a starting message

          delay(300);

        }

    }

    void loop()

    {

      if(Serial.available()==1)

      {

        int inbyte =Serial.read();

        int sensorValue=analogRead(A0);

        lcd.print(sensorValue);

        lcd.print(",");

       

     

    #include <SoftwareSerial.h>

    int inpin=A0;

    int TEMP=0;

    int sensorValue;

    SoftwareSerial mySerial(18,19);

     

     

    void setup() {

       // start serial port at 9600 bps:

       Serial.begin(9600);

     

       // pinMode(heartratepin,INPUT);

      // establishContact();

    }

    //void establishContact() {

    //while (Serial.available()== 1) {

         // Serial.println("hello");   // send a starting message

       //   delay(300);

      

     

    void loop() {

      if (mySerial.available())

      Serial.write(mySerial.read());

      if (Serial.available())

       int sensorValue = analogRead(inpin);

       float millivolts = (sensorValue/1024.0)*5000;

       float celsius = millivolts/10-50;

       Serial.println(sensorValue);

       Serial.print(celsius);

       Serial.print("c");

       Serial.print((celsius*9)/5+32);

       Serial.print("F");

       // print different formats:

      mySerial.write(sensorValue);  // Print the raw binary value

       Serial.print('\t');             // print a tab

    }

     

       

       

     

     

      }

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to neilk

    i did but it still didnt display anything on the lcd

    this is the sample code that i decided to use as an experiment to get the communication working because i believe if this works then i can proceed to getting the rest of the things i need to do added to it.

    her is the code

     

    #include <SoftwareSerial.h>

    int inpin=A0;

    int TEMP=0;

    int sensorValue;

    SoftwareSerial mySerial(18,19);

     

     

    void setup() {

       // start serial port at 9600 bps:

       Serial.begin(9600);

     

       // pinMode(heartratepin,INPUT);

      // establishContact();

    }

    //void establishContact() {

    //while (Serial.available()== 1) {

         // Serial.println("hello");   // send a starting message

       //   delay(300);

      

     

    void loop() {

      if (mySerial.available())

      Serial.write(mySerial.read());

      if (Serial.available())

       int sensorValue = analogRead(inpin);

       float millivolts = (sensorValue/1024.0)*5000;

       float celsius = millivolts/10-50;

       Serial.println(sensorValue);

       Serial.print(celsius);

       Serial.print("c");

       Serial.print((celsius*9)/5+32);

       Serial.print("F");

       // print different formats:

      mySerial.write(sensorValue);  // Print the raw binary value

       Serial.print('\t');             // print a tab

    }

     

     

     

    #include <SoftwareSerial.h>

     

     

    //reciever

     

     

    #include <SPI.h>

    #include <LiquidCrystal.h>

    SoftwareSerial mySerial(1,0);

    int inpin=A0;

    LiquidCrystal lcd(30,31,32,33,34,35);

    void setup()

    {

      lcd.begin(16,2);

      Serial.begin(9600);

      //Serial1.begin(9600);

    establishContact();

    }

    void establishContact() {

    while (Serial.available()== 1) {

         // Serial.println("hello");   // send a starting message

          delay(300);

        }

    }

    void loop()

    {

      if(Serial.available()==1)

      {

        int inbyte =Serial.read();

        int sensorValue=analogRead(A0);

        lcd.print(sensorValue);

        lcd.print(",");

       

     

       

       

     

     

      }

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube