Menu

Week 15

Networking

How to communicate to each other

Board talking to each other is very interesting to do for complexe system. Here is the group assignement documented by Philippe

We can talk to each other.

If you remember the week 7, electronic design, I made a little hello board connected to Rx and Tx. This week I will use that board to communicate to her twin sister. Let see if they can talk to each other using serial communication.

To communicate with the tx,rx, we need to have a master, where the information is comming from and a slave, this one answer to the master. The master will send by his tx pin ( transmiter) a message to the rx pins (receiver) of the slave to give some instruction. Than if we want to, the slave can answer back with his tx pin to the rx pin of the master. This is how it will communicate. The code will have to interact to both board and this is why arduino haved made this Software seriallibrary that can send and receive information. I start by opening that library. The most important of that code is probebly the way they call the rx

How to

1-The code

  1. Base on the exemple, I take the main part and add information around it.
  2. I add constant for the button and led and a boolean fonction to play with the button.
  3. Initializing the botton state
  4. Wait for the serial port to connect on the 57600 baud rate
  5. Once done, initialise my communication on a 4800 baud rate
  6. Basicaly, the code ask if it's fashing. If it's flashing than flash the led.
  7. The second code (the slave one) it's only the communication speed baud and the condition of the button. (if the button is push, than write 1 and than it's bigger than 0 so flash the led.

Software serial library exemple

Exemple code

Set up of the master code

Loop of the master code

Code of the slaves

Program each board with a USBtinyisp

2-Put it in place

  1. I mill a second board and fuse it like in the week 7.
  2. I don't exactly know why, but at the begginning of the fad academy making
  3. I fuse it with a small usbtinyISP
  4. In one, I put the master code (name communication) and in the second board I put the saves code.
  5. Wire everything toghetter and tatannn.

I personaly clone the base repository that the Fab Academy give me, I delete there folder doc with theire template and I replace it with the one I download from internet.

Voilà!

3-Let's put adressing

  1. In this new way of communication, I will use the I2C communication. I work basicaly with the SDA pin (serial data line) and the SCK pin (clock). With both, we can give the ridem and send data.
  2. To do so, I mill another board with only the essential we need, adding a pull-up resistance to the MOSI who is also the SDA pin and SCK pin of 4k.
  3. Whitout these pull-up resistance, the communication can not work
  4. So in this new code, it give the possibility to add a specific adress to the board. Here in the master code, we are talking on the canal 10. (It can be anynumber) The slave need to listen to the same 10 canale has well.
  5. There are fiew arduino library out there that can do the same thing, but I take the TinyWire library. This one had few years of improvement has StackExchange explain.
  6. Base on the exemple, the master code have to add in the first part:
    • the TinyWire library
    • Define the elements on every pins (Button and Led)
    • Put the adresse that your slave need to listen
  7. Second part, in the set up:
    • like always, set the pins
    • Start the library but with no speed. No need.
  8. Third part, in the main code:
    • Put some condition: If the button is press, try to transmit the letter "C"
    • If no one receive it turn on the LED on the master board to indicate a error.
  9. In the slave code, it will fallow the master code receiving all the bytes, waithing untils the closing byte arrive and try to read that "C". If yes, the LED on the second board will turn on when press the button.

I add 2 pull-up resistor on a new board with the strick minimum

Here is the code for the master board

Here is the code for the slave board

Tatamm!

The moral of this story

That week was much more harder that I expected, it took me a big amount of time figuring the serial communication but at the end Files

Contact

Annie Ferlatte

ferlatte.annie@gmail.com
Montreal, QC, Canada