This is my make-through for the week's assignment. Grab a cup of coffee!
After knowing some basics of microcontrollers, now it's time to make them talk to each other :D. In order to do this we have to learn some communication protocols like SPI, UART and I2C.
For this experiment we used to At Mega boards that we soldered in previous weeks
Step 1: We already had code templates from the portal, we just had to edit some details like LED and push button pins.
Step 2: We uploaded both Master and Slave codes using USBasp and 6 jumpers for these connections (MISO-MOSI-SCK-VCC-GND-RST). And don't forget to choose USBasp as your programmer in Arduino IDE. with Attiny board you have to install Attiny core from boards manager in Arduino IDE. and another difference is with Attiny 8MHZ internal clock is used while in the AT mega 16MHZ external clock is used.
Step 3: In SPI connection protocol, 4 connection are made(MOSI, MISO, SCK and CS). And of course grounds must be common.
First we include our library SPI.H
then we define our slave select pin
SPI Operation (master) :
First the slave selector pin is turned low to select the board.
in the void loop is the function spi.transfer is used for switching between sending two characters sent to the slave with 500 millisecond delay in between.
Then the slave selector pin is turned high again to disconnect the slave board
SPI Operation (Slave):
same initial steps are done, in addition to another variable called slave_recieve which saved received data, and defining input and output Pins for LED and push button.
Then in the void loop a simple if condition is done to change the LED state according to sent character,
So the result is a blinking LED that turns off when we press on the pushbutton :).
I was introduced to UART communication before in the maker diploma using HC-05 Bluetooth module, but without knowing its name.
As I still have some problems with my ATTINY44 board, I used my ATMEGA board as Master and an Arduino UNO as Slave.
Our ATMEGA board is going to be controlled using USBasp programmer, and the Arduino Uno through the USB cable
Step 1: I had ready codes to use, just uploaded them and connected Tx to Rx and vice versa, and don't forget GROUNDS MUST BE COMMON.
UART Operation (MASTER): The MASTER code worked by sending two different characters in loop with 0.5 second between each ( 'a' and 's', but before this it sends the slave address. this is used when using several slaves, why just tried it but it's not useful here and we can remove it
UART Operation (SLAVE) : The slave's loop first check if there's serial data received, if there's no received data, nothing happens(keeps waiting), until it receives a character defined in the if loop to make a certain order, in our case when ut receives an 'a' it turns the led on and when 's' is recieved in turns it of.
What I learned this week is...
This week we learned 3 different communication protocols :
SPI
UART
I2C
Some learned lessons:
Better to use different jumper colors to prevent misconnecting any of the 6 ISP pins or any I/O pin, It's better for your eyes and more efficient and makes you work faster ( seems a silly mistake but it can keep you 15 minutes without finding your problem.) *those colors are made for a reason :D*
Tx is connected to Rx and vice versa, another tip that looks silly but actually it's a common mistake with me and other people, and of course use different color jumpers ;p.