— please check an updated code sample on the following post —
http://lab.guilhermemartins.net/2013/08/01/new-basic-arduino-serial-communication/
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); }
yes, and yes
the code for the two pots is on the post!
yes it’s possible
serial comm takes both digital pins 0 and 1, they must be free in order to be able to upload code to the arduino.
you will notice if you ever use anything connected to those pins ;)
thanx a lot ….all those answers are very much usefulllll….again thanx for the help i ll implement it soon
wat abt the other pins in ardunio….that us other than digital and analog pins…
please speak plain english, and please be objective
every time I try to upload the reciver code it gives me error ( stray ‘/’ in program ) please help
following serial reading in rx
?HÌz;X?HÌz;X?HÌz;X?HÌz;X?HÌz;X?HÌz;X?HÌz;X?HÌz;X?HÌz;X?HÌz;X:H?HÌz;X?HÌ
i like the way the problem is solver..
is it possible to simoultanouslt send all 6 analog read values via serial instead of 2.
and receive correctly..
can u pls help me with the code..
thanks
hello sir……..
i want the details abut how to interface3arduinos……..coz i need to transmitt an array of data through GSM interfaced with arduino
I have never done such a thing, please check the Arduino forums, I’m sure you will find your answer there.
How did you setup the xbee configuration for the receiver and transmitter using X-CTCU?
Thank You
yes X-CTU
Thank you soooo much your example code was unspeakably useful. I appreciate you taking the time to post about your project, it will save me hours of research!
great! glad to know that it helped you!
I can’t understand because it doesn’t work: the receiving code stamp on serialmonitor the ascii caracter converted like: 505352505351505353505348505352505351505353505348505352505351505353505348505352505351505353
So the LED doesn’t turn ON
Hi! why the code ‘BYTE’ wrong and the warning :
“As of Arduino 1.0, the ‘BYTE’ keyword is no longer supported. Please use Serial.write() instead.”
how to solve the problem?
thanks
I got warning :
“As of Arduino 1.0, the ‘BYTE’ keyword is no longer supported. Please use Serial.write() instead.”
how to solve it?
thanks
just use Serial.write(your value);
your value must be 1 BYTE (0 – 255);
Hello, how much for a Code that can send analog readings from accelerometer and aslo 4 push buttons and recibe these data to another xbee with arduino ?
hello Hector, apologize but I won’t have the time to make this code for you, have a look on the arduino forums, I’m sure you will find what you need
good luck
No xbee configuration is necessary ? just wire these things and they are ready to go ?
your xbees need to be paired in order to communicate
I want to establish a communication between ping sensors(ultrasonic dist. sensors) and xbee.
the ping sensor transmits a signal and the xbee receives it and instructs another ping sensor(which is attached to this xbee) to transmit a signal to the first ping sensor.
I never connected a sensor directly to the xbee module.
Sincerely hope you find a way and if you dare, share it here with the rest of us ;)
I want to transmit analog sensor data wirelessly using Arduino. Will it be possible using the Xbee??
sure, just send it over the serial port