This week assignment, I will implement a wired or wireless communication with network or bus addresses.
I’m planning to connect my hello world board to another microcontroller board and making two boards communicating using Tx and Rx.
In this case, I have my board as master and will send commands to another microcontroller board to do something. The communication between boards is very useful as I can build a network with different microcontroller circuits to do function using many I/O ports.
To do the communication starting with my board code, I should initialize the serial communication to write specific values according to condition, once this condition is true, the value will be sent to another microcontroller circuit. If the other circuit receives the value from my circuit, this will be in condition and it will do something accordingly
I started with my circuit, writing a code
#include < SoftwareSerial.h> SoftwareSerial mySerial(PA1,PA0); // RX, TX void setup() { mySerial.begin(9600); pinMode(PA3, INPUT); pinMode(PA7, OUTPUT); void loop() { if(digitalRead(PA3)== LOW){ mySerial.println("1"); digitalWrite(PA7, HIGH); delay(50); } else{ mySerial.println("2"); digitalWrite(PA7, LOW); delay(50); } }
The code is basically waiting for the push button to be pressed. Once the button is pressed, the LED in my board turns on and the number 1 will be transferred through my RX and TX pins (where should be connected to another circuit to communicate).
If the push button not pressed (else), the code keeps LED off and the number 2 will be transferred through my communication pins.
I used PA1 pin as Rx, and PA0 pin as Tx. The baud rate (which is the rate at which information is transferred in a communication channel) 9600 bits per second.
I tested the circuit before preparing the other code circuit, it worked well
I was planning to use my colleague’s circuit (Hussain Alhudhud), to receive the information from my board and do something accordingly. The code that I’ve prepared is waiting for the serial port to check the value if it is 1 or 2 through serial port as well
#include < SoftwareSerial.h> SoftwareSerial mySerial(PA1,PA0); //RX, TX int v; void setup() { mySerial.begin(9600); pinMode(PA7, OUTPUT); // white led } void loop() { v = mySerial.read(); if(v == 1) { digitalWrite(PA7,HIGH); else if(v == '2') { digitalWrite(PA7,LOW);
If the microcontroller circuit received 1, the LED will turn on, if it received 2, the LED will stay off
I uploaded the code using Arduino as ISP, and then I started connecting my circuit to the board to test the whole project.
The Rx in my circuit should be connected to the Tx in the other circuit, and vice versa, that means, the Tx will be connected to the Rx.
Also, I got the power from an Arduino (5v and GND) and the other circuit is connected to the same source as well.
I recorded video while the project is running
Now, I’m planning to make communication with address between my two boards, one is 328p based and the other one is hello world board that has Attiny44. In this case, one board will be considered as master and the other will be considered as slave. This communication called I2C communication protocol, the pins that need to be connected SCL and SDA from both boards
I did prepare the code for master circuit. The master circuit that I chose is my microcontroller board that has Atmega328p chip, the bins needed are exposed (SCL and SDA) which are A5 and A4 Analog pins
The code for Master circuit is:
#include < Wire.h> byte on = 100; byte off = 200; void setup() { pinMode(A1,INPUT); Wire.begin(); Wire.setClock(10000) ; } void loop() { if(analogRead(A1) > 500){ Wire.beginTransmission(55); Wire.write(on); Wire.endTransmission(); } delay(100); if(analogRead(A1) < 500){ Wire.beginTransmission(55); Wire.write(off); Wire.endTransmission(); } delay(100); }
So, basically the board has potentiometer, once the value of voltage level more than 500 (2.5 volts approximately) the board will send “on” word, on is defined as byte with 100 value. If the voltage level less than 500, the board will send “off “ , off is defined as byte with 200 value
The code has Wire.h library which allows the communication protocol I2C. Wire.setClock(10000) ; command makes sure that the communication is set with kind of synchronization between both boards even the chips are not the same. Wire.beginTransmission(55); this command set the address of transmission to be 55
The potentiometer should be connected with pin A1 as defined in the code
I connected my board to the laptop using FTDI cable, as the bootloader is already burned
I opened the code using Arduino IDE program and select the board Atmega328p
I uploaded the code and disconnected the FTDI cable
Now, I need to upload the slave board code. The slave board that I selected is Attiny44 based. I need to download another library to use instead of Wire.h library. I could download the library from this link
After downloading the library, I unzip it and copy the unzipped folder and past it in libraries folder inside Arduino folder
I started writing the slave board code:
#include < TinyWire.h> byte own_address = 55; byte received; void setup() { pinMode(7, OUTPUT); //digitalWrite(led_pin, HIGH); // config TinyWire library for I2C slave functionality TinyWire.begin( own_address ); // sets callback for the event of a slave receive TinyWire.onReceive( onI2CReceive ); } void loop() { } void onI2CReceive(int howMany){ received = TinyWire.read(); if(received==100){ digitalWrite(7, LOW); }else if(received==200){ digitalWrite(7, HIGH); } }
The code is basically waiting for the master board to send something, if the master sends 100, the LED keeps off, if it sends 200 the LED turns on. The communication adress is defined as the master circuit to be 55
I uploaded ArduinoISP to my Arduino, and then I disconnected it and started connecting the ISP wires so I can upload the code on my Attiny44 board
The code is done and uploaded successfully. Now I disconnected everything, as I will start connecting the two boards as shown below, SCL to SCL, SDA to SDA, and GND to GND
I connected the slave board with Arduino to get power (5v and GND)
I connected Arduino USB cable, and then connected the last wire which is 5v to 5v from slave to master board to power it up after powering the slave
The code worked perfectly for both circuits
Download Files' Links: