I´ve created this example to make two Arduinos talk in a simple and clear fashion.
You will see below an example with wires, and another one without wires.


- – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – -
Setup with TX and RX pins connected by wire:
On the sender Arduino there is a potenciometer, the values are read and sent through the serial port.
On the receiver Arduino one Led intensity will change accordingly the pot value.
Tx on Arduino#1 -> connect to -> Rx on Arduino#2
Rx on Arduino#1 -> connect to -> Tx on Arduino#2
NOTE: Don´t forget to disconnect Tx / Rx wires before upload

// SENDER int analogValue5, val5; void setup() { // Serial port enable Serial.begin(19200); } void loop() { // read analog pin 5 analogValue5 = analogRead(5); // remap values from the analogValue5 variable to 0 / 255 val5 = map(analogValue5, 0, 1023, 0, 255); // send the value to the serial port Serial.println(val5, BYTE); }
- – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – -
// RECIEVER byte incomingByte; void setup() { // Serial port enable Serial.begin(19200); // declare pin 11 as output, this is the LED pinMode (11, OUTPUT); } void loop() { // if there is bytes available coming from the serial port if (Serial.available()) { // set the values to the ‘incomingByte’ variable incomingByte = Serial.read(); // write the value to the pin 11 analogWrite(11, int(incomingByte)); } }
- – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – -
- – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – -
- – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – -
Wireless setup with XBee modules:
Recently I got two XBee modules, and here is the setup to make them talk to each other:
As a start I used 2 potenciometers to control the intensity of two LEDs and It works pretty well. :]
I´m using this adapters to connect the XBee to the circuit. They are very simple to assemble and to use.

- – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – -
// SENDER int analogValue2, analogValue5, val2, val5; void setup() { // start serial port at 19200 bps Serial.begin(19200); } void loop() { // read analog input analogValue2 = analogRead(2); analogValue5 = analogRead(5); // remap values val2 = map(analogValue2, 0, 1023, 253, 0); // 254 and 255 for SYNC val5 = map(analogValue5, 0, 1023, 253, 0); Serial.print(254, BYTE); //SYNC char Serial.print(val2, BYTE); Serial.print(255, BYTE); //SYNC char Serial.print(val5, BYTE); delay(150); }
- – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – -
// RECIEVER byte incomingByte, sensor1, sensor2; void setup() { // start serial port at 19200 bps Serial.begin(19200); Serial.println(“Ready!”); // led pins pinMode (5, OUTPUT); pinMode (6, OUTPUT); delay(1000); } void loop() { if (Serial.available()) { // are there any bytes available on the serial port ??? // assign bytes to the var ‘incomingByte’ incomingByte = Serial.read(); Serial.print(int(incomingByte)); // from now on is pretty clear I guess :) if ((int(incomingByte) == 254)) { sensor1 = Serial.read(); Serial.print(“Sensor 1 = “); Serial.print(int(sensor1)); } if ((int(incomingByte) == 255)) { sensor2 = Serial.read(); Serial.print(“ Sensor 2 = “); Serial.print(int(sensor2)); } } analogWrite (5, sensor1); analogWrite (6, sensor2); }

{ 31 } Comments
I want to see you do the wireless part with ladyadas softserial library on pins 2 & 3. I am having problems.
Thanks
-Jose
SoftSerial library emulates rx / tx pins and if you want to have a reliable connection you should the original Serial protocol.
Ok, got it working here is the code below for anyone that wants to do it with the AFsoftserial software:
#include
int LEDpin = 5;
byte address;
byte data;
AFSoftSerial mySerial = AFSoftSerial(2, 3);
void setup() {
pinMode(LEDpin,OUTPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
Serial.println(“Chip 2 online”);
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
}
void loop() // run over and over again
{
if (mySerial.available()) {
// Serial.print((char)mySerial.read()); // took this out so I do not loose a byte
address = mySerial.read();
Serial.print(address);
if (address == ’2′)
{
data = mySerial.read();
if (data == ’5′);
{
digitalWrite(LEDpin,HIGH);
delay(1000);
digitalWrite(LEDpin,LOW);
}
Serial.print(data);
}
}
if (Serial.available()) {
mySerial.print((char)Serial.read());
}
delay(50);
}
Oh,
Here is the sender arduino code the above is the receiver arduino code!
#include
#define Button 6
//int inPin = 6; //
int val = 0;
int old_val = 0;
int state = 0;
int val5 = 86;
AFSoftSerial mySerial = AFSoftSerial(2, 3);
void setup() {
pinMode(Button,INPUT); //
pinMode(13, OUTPUT);
Serial.begin(9600);
Serial.println(“Chip 1 online”);
delay(2000);
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
// mySerial.println(“Whats for dinner?”);
}
void loop() // run over and over again
{
val = digitalRead(Button); // read input value and store it. Yum, fresh
// check if there was a transition
if ((val == HIGH) && (old_val == LOW))
{
state = 1 – state;
mySerial.println(2);
mySerial.println(5);
Serial.println(“Rising Edge”);
delay(250);
}
old_val = val; // val is now old, let’s store it
if (mySerial.available()) {
Serial.print((char)mySerial.read());
}
if (Serial.available()) {
mySerial.print((char)Serial.read());
}
delay(50);
Serial.println(val); //
}
must try this!
thanks for sharing! ;-)
I got our Xbee’s in the mail today. I am going to start off with your example here to try and get an understanding before attempting to use them in a robot, etc. I wish I had one of the protoshields you are using though.
Glad to know that, If you need help just say!
Protoshields are great, I advice you to get a bunch of them :-)
Hi there
Sorry – new to arduino and trying to understand basic rx tx. I’ve set up everything (the hardwired setup) exactly like you have yet the LED only goes on or off – the pot has very little control – definitely no smooth fading. Does the value of the pot matter? No idea what i could be doing wrong?
@ wesley
It’s hard to tell what might be wrong with your setup! Have you double check all the connections / wirings?
Can you send me a photo?
Para comunicar é preciso ter o TX, e o RX? ou dá para ter um master, ligado a um slave apenas com um TX(no master) e um RX(no slave)? só um fio?
Não te quero induzir em erro, mas julgo que sim, se usares apenas uma ligação de Tx (master) – Rx (slave) tens comunicação unidireccional.
Isto porque tenho um controlo remoto de apenas um canal, e queria com a penas um canal controlar vários servos/motores
sim é possivel, como pretendes fazer a distinção do sinal, ou seja, como vais fazer para mexer um servo e não outro?
I want to control a servo using 2 xbees by a potentiometer in the same manner as led . But I am having a trouble. Can anybody help me please?
//Sender Code*************************
int analogValue5, val5;
void setup() {
// Serial port enable
Serial.begin(9600);
}
void loop() {
// read analog pin 5
analogValue5 = analogRead(0);
// remap values from the analogValue5 variable to 0 / 255
val5 = map(analogValue5, 0, 1023, 0, 180);
// send the value to the serial port
Serial.println(val5, BYTE);
}
********************************************
receiver code *********************************
byte incomingByte;
#include
Servo servoMotor;
void setup() {
// Serial port enable
Serial.begin(9600);
servoMotor.attach(9);
servoMotor.write(0) ;
// declare pin 11 as output, this is the LED
pinMode (9, OUTPUT);
}
void loop() {
// if there is bytes available coming from the serial port
if (Serial.available()) {
// set the values to the ‘incomingByte’ variable
incomingByte = Serial.read();
servoMotor.write(int(incomingByte));
delay(300);
}
}
***************************************************
Hey Guilherme,
I love your work and your site. I stumbled across this post when working with XBees to control my flame effects. Thanks for making this available, I’d love to talk more about electromechanical devices and art.
Brett Levine
Hey, I am trying to establish wireless communication between 2 XBee modules by using the XBee API. So far, I have gotten the communication to work to an extent that I can control the LED of one Arduino, by the other, using wireless communication between the 2 XBees.
I, now, want to go in for wireless transmission of data i.e. strings hard-coded in the program itself should be sent by one and received by the other, upon writing the code to one Arduino, and be displayed on the receiving end’s serial monitor. I tried to tweak the code given above for this purpose, however failed miserably.
Please help!
P.S. I need it using the NewSoftSerial library, as my make of the XBee module is working best with this library.
Thanks for sharing this. I was wondering how does SYNC char work in your code? Because I am trying to send data from two analog sensors and one digital switch from one xbee module to the other one so the LEDs on the receiving side would light up correspondingly. I am trying to figure out how to “label” serial data so the receiving side would “read and interpret” the data accordingly. And it seems like you were able to do that with the SYNC char so I am hoping you can explain to me how SYNC works.
Thank you
this is a great post. for a start i would like to thank all the geniuses who come up with this software that are really making communication easy, technology has really helped us. for some of us who do not understand the language of technology but love to use its end product, all i can say is KEEP UP.
Thanx, glad to know it helped ;)
What mode is the Xbee operating in? I am assuming it is in Transparent mode? How did u set up the Xbee? or did u just use it out of the box and it worked?
Thats a really cool,easy,beginner friendly tutorial.I have ordered two xbees and will start off with this tutorial.I was completely blank in wireless communication but this helped a lot.
what does SYNC means and why is it needed ?
cool! glad to know that it helped ;)
SYNC is a byte that is used by the Receiver to know exactly what it will receive on the next time
for instance, and looking to the code, imagine you send me “255″, I know that the next value will be assigned to the var “sensor2″
How about i declare some other characters like “A”for the first potentiometer and “B” for the second potentiometer instead of 254 and 255. Will it still work ?
Yes you can do that and you should ;)
im about to buy some of the modules and i am also working on mac os. as i see you use a macbookpro what software did you use to configure the xbees? or dont you need to configure them if you use em with two arduino-boards? i am a bit confused about all the forum posts in the net about how hard it is to make xbees running on macos.
Would you please send me the Xbee firmware configuration for your setup here? Did you use API mode for xbee? Did you use one Router API and one Coordinator API? Let me know please, I am trying to do this example.
In order to configure the xbees you need to use X-CTU software provided by Digi/Maxstream, note that this application run on Windows only.
You can also use AT commands but I never did it.
Better is to check Ladyada’s tutorial: http://www.ladyada.net/make/xbee/configure.html
In order to configure the xbees you need to use X-CTU software provided by Digi/Maxstream, better is to check Ladyada’s tutorial: http://www.ladyada.net/make/xbee/configure.html
Post a Comment