This is my make-through for the week's assignment. Grab a cup of coffee!
Documentation again huh?
First of all, I was on a long trip, I had to cut it off to attend the in-lab session, it took me 12 hours of traveling and that was too crazy!
Anyway, the purpose of why we study communication protocols is that when we need to communicate between boards or sensors in order to create a fully functional integrated environment, and the second it is the basics of the Internet of Thing for the future!
before we can proceed further, we have to understand how communication protocols work.
the definition of it is: A communication protocol is a system of rules that allow two or more entities of a communications system to transmit information via any kind of variation of a physical quantity. The protocol defines the rules, syntax, semantics, and synchronization of communication and possible error recovery methods. Protocols may be implemented by hardware, software, or a combination of both.
we will use two types of protocols in order to make communication between two boards.
the two will be the Serial Peripheral Interface (SPI) & I2C (Inter-Integrated Circuit)
Let's start.
References, First, Second, Third
The SPI is a very common communication protocol used for two-way communication between two devices. A standard SPI bus consists of 4 signals, Master Out Slave In (MOSI), Master In Slave Out (MISO), the clock (SCK), and Slave Select (SS).
SPI is not symmetric. An SPI bus has one master and one or more slaves. The master can talk to any slave on the bus, but each slave can only talk to the master. Each slave on the bus must have its own unique slave select signal. The master uses the slave select signals to select which slave it will be talking to. Since SPI also includes a clock signal, both devices don't need to agree on a data rate beforehand.
When the master of the SPI bus wants to initiate a transfer, it must first pull the SS signal low for the slave it wants to communicate with. Once the SS signal is low, that slave will be listening on the bus. The master is then free to start sending data.
There are 4 different SPI bus standards that all have to do with the SCK signal. The 4 modes are broken down into two parameters, CPOL and CPHA. CPOL stands for Clock POLarity and designates the default value (high/low) of the SCK signal when the bus is idle. CPHA stands for Clock PHAse and determines which edge of the clock data is sampled (rising/falling). The datasheet for any device will specify these parameters so you can adjust accordingly. The most common settings are CPOL=0 (idle low) and CPHA=0 (sample rising edge).
Here is an example transfer with CPOL=0 and CPHA=0.
As in the lab session with our beloved instructor, 3askar, and with the awesome videos provided by our instructor, Ahmed ibrahim, we could do it fast and easily
we created the teams, I was with Ahmed Hossam and Ibrahim Raafat
we understood the code sample then we wrote our own code
the SPI communications protocol will be between two atmega328p boards
the code purpose was to light a LED on the Atmega board by pressing a button on the other Atmega board
So, the master code will be:
and the slave code will be
For further code explanation please jump to the third reference link mentioned above
For the connection, we have to connect the mosi, miso, common ground, sck for both together
and we connect a communication pin that will be D10 together too
and there she works
References; First, Second, Third
If the following di not satisfy your needs please jump to the second reference link then third
I2C combines the best features of SPI and UARTs. With I2C, you can connect multiple slaves to a single master (like SPI) and you can have multiple masters controlling single, or multiple slaves. This is really useful when you want to have more than one microcontroller logging data to a single memory card or displaying text to a single LCD.
Like UART communication, I2C only uses two wires to transmit data between devices:
SDA (Serial Data) – The line for the master and slave to send and receive data.
SCL (Serial Clock) – The line that carries the clock signal.
I2C is a serial communication protocol, so data is transferred bit by bit along a single wire (the SDA line).
Like SPI, I2C is synchronous, so the output of bits is synchronized to the sampling of bits by a clock signal shared between the master and the slave. The clock signal is always controlled by the master.
the goal is to use I2c to communicate between ATtiny44 and atmega328p Arduino
the function of the test is to press on a push-button on a board and that changes something on the other board
what we will do is to push a button on the Arduino and that powers a led on ATtiny
We have to connect the SDA pins of the boards together and the same with the SCL
In order to do that we have to know the pins of each board and we have to look at the pin map
I have looked at the atmega328p pin map, As following on the right:
then for the attiny44 board we made it was:
after knowing the pins, its time for the connection.
the sda, scl must be connected to pull up resesitor in order to work perfectly as mentioned in our instructors videos
we will use the integrated push button of the atmega board, the push button is connected to D3
then we will connect a led on the attiny board
the master will be the atmega328p and the slave will be the attiny the code will be :
Did you think that every thing will go smooth ??
first problem was the attiny i2c arduino library the TINYWIRE!
searching on github and found multiple libraries
i had no idea how to install library from github and after multiple tries, I eventually i managed to reach this method and realized that I was a big dumb!
the link: TINYWIRE
and then install it to arduino IDE library
uploading it with usbasp to the atmega328p and the attiny...
although I had issues at connecting at first and the i2c can not be established but when I tried to reconnect, it worked surprisingly 😅, glad to see it working and confused about what the problem was!
and there it WORKS with Arduino UNO and the attiny44
I always say, but no one listen
and the final test with Atmega328p and attiny44
For how to upload using ISP connector please jump to my week 3 review