This week objective is to make 2 boards with their own micro controllers communicate with each other.
I had no experience in doing communication, so I decided that I'll use over wire communication which was the easiest for me after reading about the others such as I2C and SPI, so I used the Serial communication using the Tx and Rx.
First I need to clarify some terminologies:
Tx: Data Transmit Pin
Rx: Data Receive Pin
Master Board: this the board I send data at the beginning to initiate a conversation between board, then waits for the slave board to confirm the messeage delivery.
Slave Board: this the board I receive data on from the master board and do whatever instructed to do (In my example LED turns ON) and send a message back to the master board to tell it that the message is delivered successfully.
Those (Rx & Tx) are the essence of my communication technique.
In order not get confused as I did while selecting which pin will be the Rx and which one will be the Tx I'll explain for you breifly how.
I'll explain the situation as if you need only one way communication between the board and the FTDI and same will go with any other board.
First, focus on who's the master board, who's the microcontroller you're uploading the code on which's sending the signal.
For instance, my input device board is the master and I picked (Pin 2) to be the Tx which will send the sensor signal, but you'll ask yourself whether to connect that pin to the FTDI's (Rx OR Tx??), you want to receive this data on the ftdi, right ?
So you connect the board pin you assigned (Tx) to the pre-assigned pin of the FTDI (Rx).
My recommendation is to use the FTDI breakout board as it's easier to assign any pin as Rx or Tx and connect the opposite for the FTDI, otherwise you'll have to abide by the FTDI order and choose the corresponding pins upon it!
I brought my Input device week board to be the master board and send signal to the slave board.
First I wrote a code to send and receive data in the Master board then the slave.
The Master Code: (Each line explained on its own)
#include <SoftwareSerial.h> // This is a library that make us initialize the communication to use the TX & RX pin. #define RX 1 // This the pin I'll use later on to receive the confirmation message from the slave board. #define TX 2 // This the pin I'll send my data through, to the slave board. SoftwareSerial Serial(RX, TX); // giving the pin variables to the softwareSerial library after defining them in the line upward. int val=0; // variable to store the values coming from the reading process. void setup() { Serial.begin(9600); // define the baud rate that the communication will start on. pinMode(3,OUTPUT); // set the pin connected to the board LED as output to give it (0v or 5v) pinMode(4,INPUT); // set the pin connected to the the sensor as input to receive signal from (0:ON or 1:OFF) } void loop() { //#########################################Sending Data to Slave######################################### val = digitalRead(4); // read the input pin and store it in "val" variable. if (val == 0) { // checks if the variable "val" value is 0:ON so turn on the LED on the board. digitalWrite(3,HIGH); // turning the LED on. Serial.write('S'); // Sending a message using the TX pin that the slave board will receive and turn on the slave LED ON! delay(1000); // delay used to read every second not all the time. } else { digitalWrite(3,LOW); // turning the LED off. delay(1000); // delay used to make the LED off at least for a second while switching the trigger value. } //#########################################Receiving confirmation from the Slave######################################### if (Serial.available()) { // if there's any data coming to the RX pin int rec = Serial.read(); // store this data in the variable "rec". if (rec == 'R') { // Verify that the data received is the R we will ask for as a confirmation from the slave board Serial.println(" Received Back"); // print in the serial monitor this message to confirm the receival. } } }
I brought my Electronics design week board to be the slave board and receive signal from the slave board.
After writing a code to send signal to the slave board and wait for signal coming from the slave board to confirm the receival part.
I started preparing the slave board to receive upcoming signal from the master board and send confirmation messeage back to the master if the message is valid.
The Slave Code: (Each line explained on its own)
#include <SoftwareSerial.h> // This is a library that make us initialize the communication to use the TX & RX pin. #define RX 0 // This is the pin that will receive the data from the Master board to turn on the LED. #define TX 1 // This is the pin that will send the #define LED 8 // Names the LED pin to be easily accessed. SoftwareSerial Serial(RX, TX); // giving the pin variables to the softwareSerial library after defining them in the line upward. int val=0; // variable to store the values of the reading. void setup() { Serial.begin(9600);// define the baud rate that the communication will start on. pinMode(LED,OUTPUT); // set the pin connected to the board LED as output to give it (0v or 5v) } void loop() { //#########################################Receiving data from the Master######################################### if (Serial.available()) { // if there's any data coming to the RX pin. int ser = Serial.read(); // Read it and store this data in the variable "ser". if (ser == 'S') // Verify that the data received is S from the master board and act upon it. //#########Sending Confirmation of receival to the Master#########) { digitalWrite(LED,HIGH); // turning the LED on. Serial.write('R'); // Sending a message using the TX pin that the master board will receive on the RX pin to confirm the receival of the data. } else { digitalWrite(LED,LOW); // turning the LED off. } } }
First, I brought my FTDI board, input board and Hello Echo board.
Then I uploaded the Master code for the Input board and the slave for the Hello echo board.
To upload each code to their corresponding board please check Embedded Programming week
lastly, after uploding code to each board, I connected them as shown below.
You'll find Higher Resolution picture in the downloads at the page bottom.
I used the breadboard to supply common 5V and GND for both boards as shown above.
I connected the Tx of the Master board to the Rx of the Slave board and vice versa. (Yellow Color for Rx, Red for Tx, White is 5V and Black is GND)
If you followed this connection, the code above will work just fine :D
I used the breadboard again to connect the the RX of the FTDI to the junction that involves the (Master Tx and The Slave Rx), because the Signal we want to capture on the serial monitor first intiates from the Master which's the "S" and after the slave board receives the signal successfully and send the confirmation back to the master the master sends the confirmation message "Received Back".