This will be my first post on Arduino projects, so instead of starting with the basics, I shall try something new. In this post, I am going to do a simple chat program using two Arduino Uno.
1. 2 x Arduino Uno (or any other Arduino boards)
2. 2 x Jumper Wires
3. 2 x USB Cables
The hardware setup is actually pretty simple. Just connect pin 10 of each Arduino to pin 11 of the other using the jumper wires. The theory behind this is that pin 10 is the RX pin and pin 11 is the TX. To communicate serially, you have to connect the TX to RX and RX to TX. Once that is done, connect it to your PC.
Once you had successfully connected to your PC, then you can start programming your Arduino boards. I assume that you have your Arduino IDE installed, if not, head to this link here and download it. Arduino IDE download
In your PC, you will need to open two session of Arduino IDE. Make sure they can access two different ports. It must be opened differently and you should not open the other session in the same window. This will not work. The logic is that you need to open two IDE sessions so that your computer can communicate with both your Arduino serially, and print the results in the serial monitor. Once the IDE is up, download the below sketch to both of the Arduino.
/*<br> Simple Chat Program Receives from the hardware serial, sends to hardware & software serial. Receives from software serial, sends to hardware serial. The circuit: * RX is digital pin 10 (connect to TX of other device) * TX is digital pin 11 (connect to RX of other device) created 16 August 2014 modified 16 August 2014 by William Chang Wei Tan based on SoftwareSerial example */ #include <SoftwareSerial.h>SoftwareSerial chat(10, 11); // RX, TX
int text;
void setup() { // open hardware serial, TX = 1, RX = 0 Serial.begin(9600); Serial.println("Starting Chat Program..."); // set the data rate for the SoftwareSerial port chat.begin(9600); delay(1000); // delay 1s to stabilize serial ports chat.println("Hello World"); }
void loop() { if (chat.available()) Serial.write(chat.read()); if (Serial.available()) { Serial.print("Me:\t"); while (Serial.available()) { text = Serial.read(); chat.write(text); Serial.write(text); } chat.println(); Serial.println(); } }
Now you can download the sketch to your Arduino and compile it. Once it's complete, open the serial monitor and start chatting with the other Arduino!
You've just created your very first chat program!
For more awesome stuffs, please visit my blog here