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);
}

{ 13 } 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?
Post a Comment