Need help with Arduino and XBee communication for soil moisture sensor project

Hi everyone,

I'm working on a project that involves two Arduino Uno boards, two XBee S2C modules, two SparkFun XBee shields, and a soil moisture sensor. My goal is to have one Arduino read the soil moisture sensor's value and transmit it to another Arduino using XBee modules. I want to display the received value on the serial monitor of the second Arduino.

So far, I've successfully set up the XBee modules in XCTU, establishing one as the coordinator and the other as a router. I can send messages from XCTU console to the Arduino serial monitor, and they show up correctly. However, I'm facing an issue when trying to send messages from the Arduino serial monitor to the XCTU console.

I'd appreciate it if someone could help me generate the code that will enable this communication between the Arduino boards using XBee modules. Any guidance or examples would be highly appreciated!

Thank you!

if your module is configured correctly and connected then it's all transparent

On your SparkFun XBee shield, the serial pins (DIN and DOUT) of the XBee are connected through an DPDT switch, which allows you to select a connection to either the UART pins (D0, D1) or any digital pins on the Arduino (D2 and D3 default).

Say you connected your xBee to pin 2 and 3 and selected the switch appropriately

Upload that code and open the Serial Monitor at 115200 bauds

#include <SoftwareSerial.h>
SoftwareSerial xbee(2, 3);

void setup()
{
  Serial.begin(115200);
  xbee.begin(9600);
} 

void loop() {
  if (xbee.available()) Serial.write(xbee.read());
  if (Serial.available()) xbee.write(xbee.read());
}

whatever you type in the Serial monitor should be sent to the other xBee and whatever you send from the other xBee will be shown in the Serial monitor

Hello, thank you for your input! I'm relatively new to this field, and this is my first experience working with these boards, XBees, and the shield. I have a follow-up question: Is the DPDT switch on the SparkFun XBee shield the one labeled "UART/Dline"? I'll attach a picture for better clarity. Additionally, regarding the provided code, should it be uploaded to both Arduinos? Thanks a lot!

yes this is the switch.

dline-switch

as we want to use D2 and D3 for the Software Serial communication, it needs to be set to D-Line

this connects the xBee IN and OUT lines to the SJ1 and SJ3 pads if you look at the schematic

image

the documentation also mentions that by default it's D2 and D3

you can see the little pads marked 2 and 3 and the bottom of your shield that are soldered

Hello,

Thank you for your response and assistance. Despite following all the instructions provided, I am still encountering an issue where nothing is being displayed on the serial monitor when I connect both XBee modules to the Arduinos. However, within XCTU, I am able to send a message from the console and it appears in the serial monitor. The problem arises when attempting to send from the Arduino serial monitor back to the console. Could you please advise on potential reasons for this issue? From there, I hope to diagnose why the two devices are not communicating via the Arduinos.

Additionally, I am curious if there is an alternative method to connect the XBee modules and communicate with Arduino without using the shield, opting instead to connect them directly.

Thank you once again.

the shield helps you adjust the voltage for your xBee and offers extra benefits.
There are simpler shields like the Xbee Explorer Regulated which provides just the voltage adapter.

which code do you use for that?

Hello, J-M-L... apologies o the late response, it worked!! thanks alot. the issue was i had not installed the XBee libraries and in the code i was calling them.. silly me :smile:
I appreciate the help.
thanks a lot.

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