How to write the code to connect 2 Tinusaur boards – for beginners?
This tutorial is written specifically for Tinusaur Boards with Shield EDUx3IO and Shield LEDx2 boards but could be used as a reference for any other ATtiny85-based board. This is a beginners’ tutorial which means that the code is written using the block-based development environment Blocktinu.
Prerequisites
We will need 2 Tinusaur Boards with Shield EDUx3IO and Shield LEDx2 boards that are already connected. Check the Tutorials page to see how to do that.
Your setup might look different if you’re using an earlier version of the boards.
How does it work?
The principle on which the communication between 2 microcontroller boards works is very simple.
One of the boards (let’s call it K1) will switch one of the input/output pins from 1 to 0 and, after a short period of time, back to 1, while the other board (let’s call it K2) will wait and detect that change. This will accomplish the task of one board sending a signal to another.
At this point, we’re not going to do any more complicated form of communication between the 2 boards.
Main Board Button
The Main Board button could be connected to either the PB3 port or to the RST (reset) line of the ATtiny85 microcontroller. This function is switched by the 2×2 header, and the jumper cap that we put on it.
The capability for 2 boards to communicate also uses the PB3 port.
This means that when we want to create such communication, we will not be able to use the button programmatically via the port PB3. However, we can still use the button as a reset.
This is a limitation that is not easy to avoid, given the small number of pins on our ATtiny85 microcontroller.
Such limitation could be avoided if we use for the communication another pin, different from PB3. In that case, we would have to be more knowledgeable about programming and manipulating the input/output ports of the ATtiny85 microcontroller. The details about how to do that are the subject of a different tutorial.
Another “side-effect” is that if we use the button on port PB3 and the communication capabilities at the same time, pressing the button could be used for testing and debugging purposes, as it will cause the PB3 port to go to 0 (zero) when pressed.
Programming K1
Let’s start with creating the program for the first board – the one we called K1. This is the board with the Shield EDUx3IO. This board will emit (or send) signals so the other one will receive them.
Remember to set the Blocktinu to use the Shield EDUx3IO boards from the Shields menu.
To trigger the event we will use the photoresistor – remember that we cannot use the button programmatically because the PB3 port is already used for communication.
Let’s check if the light to which the photoresistor is exposed goes below a certain value.
When that happens, let’s make the white LED blink once.
After that, we send a signal out through the wire. To do this, we will use a simple command block called SIG, which was designed for that purpose.
After that, we will wait 1 second before continuing.
In short, this program does the following: every time we block the light in front of the photoresistor, it sends a signal out.
Let’s build the program, upload it to the K1 board, and continue with the program for the K2 board.
Programming K2
Let’s now create the program for the other board – the one we called K2. This is the board with the Shield LEDx2. It will expect signals from the other board and, when that happens, will take action.
Remember to set the Blocktinu to use the Shield LEDx2 boards from the Shields menu.
The program should have the red LED switched on and check for an incoming signal. When that happens, it should turn off the red LED and turn on the green LED. After 1 second, it switches the green LED off and the red LED off.
So, start creating the program.
To check if there is an incoming signal, we will use a simple command block called CHK, which was designed for that purpose.
When that happens, we do the LED sequence as described above.
This program should now receive the signal from the other board and switch the LED according to the description above.
Let’s build the program and upload it to the K2 board.
Testing
Make sure that the boards are properly connected and have the appropriate program uploaded and powered.
Every time we block the light in front of the photoresistor on K1, it sends a signal to K2, which causes the red LED to go off and the green LED to go on. After 1 second – the green LED goes off, red LED goes on.
Mission accomplished!
🙂
Some features to keep in mind
This is important.
On the K2, the checking for incoming signal with the CHK block happens at the exact moment the block is used. This means that if the signal arrived a bit earlier we will miss it.
Let’s see an example where this could happen.
Let’s add another delay of 1 second after the LED sequence.
In this case, if we have just finished the LED sequence and are still waiting for that last 1 second if a new incoming signal arrives, we will not be able to detect it because we are still stuck in the delay block.
This is something to keep in mind when we write such code.
Another approach
There is another way of accomplishing similar tasks. It is by using the so-called “blocking” function, which is implemented by another block called LSN or “Listen”. What it does is block, i.e., wait until the actual signal comes without doing anything else.
This may sound a bit extreme, but in some cases, it is a much more useful solution.
Let’s see an example of doing it this way.
In this case, when the program reaches the LSN block, it will remain stuck until we receive a signal from the other board.
This is a different approach for the same solution, and in specific cases, it has some advantages.
Conclusion
Connecting 2 Tinusaur Boards is not complicated at all. It allows boards to exchange signals and work on much more complicated projects.
It is possible to connect 3 or more Tinusaur boards together, but this is the subject of another tutorial.
Copyright © 2024 Tinusaur. All Rights Reserved. Authors: The Tinusaur Team
License: CC-BY-SA-3.0, Creative Commons Attribution–ShareAlike License.
Retain in your derivative work a note about the original authors and a link to the Tinusaur website.