14. Networking and communications¶
Group assignment¶
This week group assignment was to send a message between two projects.
We decided to connect my and Elen’s boards.
There are two codes primary and secondary. We connected joystick to primary board and at first we connect one led and after that two leds to secondary boards. When the joysticks’ x and y positions are not 0 it sent x position to the secondary board and if they equal 0 the primary board sent 0.
Primary board code
#include <SoftwareWire.h>
const int sda=0, scl=1; // replaced sda and scl pins on board
SoftwareWire Wire(sda,scl);
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
#define SW 10 // Joistick pins
#define VRY 9
#define VRX 8
int SwState = 0; // joistick state
int Xposition = 0;
int Yposition = 0;
void setup() {
Wire.begin();
Serial.begin(9600);
pinMode(sda, INPUT_PULLUP); // software level resistors for SDA SCL pins
pinMode(scl, INPUT_PULLUP);
}
void loop() {
Xposition = analogRead(VRX);
Yposition = analogRead(VRY);
SwState = digitalRead(SW);
Xposition=map(Xposition,0, 1023, -512, 512);
Yposition=map(Yposition,0, 1023, -512, 512);
Serial.print("x: ");
Serial.println(Xposition);
Serial.print("y: ");
Serial.println(Yposition);
if (Xposition !=0 || Yposition !=0) {
Wire.beginTransmission(0x9);
Wire.write(Xposition);
Wire.endTransmission();
delay(200);
}
else {
Wire.beginTransmission(0x9);
Wire.write(0);
Wire.endTransmission();
delay(200);
}
}
When the secondary board gets data from primary board and its greater than 0 first led turned on and second led turn off , and if the data is smaller than 0 second led turned on and first led turn off.
Secondary board code
#include <Wire.h>
#define I2C_SLAVE_ADDRESS 0x9 // the adress of secondary board
bool ledState;
int led1=1;
int led2=0;
byte recivedData=0;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
Serial.begin(9600);
Wire.begin(I2C_SLAVE_ADDRESS);
Wire.onReceive(receiveEvent);
}
void loop() {
if((int)recivedData > 0) {
digitalWrite(led1,HIGH);
digitalWrite(led2,LOW);
} else {
digitalWrite(led1, LOW);
digitalWrite(led2,HIGH);
}
}
void receiveEvent(int numBytes) {
while(Wire.available()){
recivedData=Wire.read();
Serial.println(recivedData);
}
}
Individual assignment¶
This week’s assignment was to design, build, and connect wired or wireless node(s) with network or bus addresses and local input or output device. At first I thought that I could use my previous designed board for this week, but decided to create board for general use. First I made schematic and after that pcb. I decided to use I2C protocol. It is a two-wire serial communication protocol using a serial data line (SDA) and a serial clock line (SCL).The protocol supports multiple target devices on a communication bus and can also support multiple controllers that send and receive commands and data. Communication is sent in byte packets with a unique address for each target device.
After all my 3 boards were ready I started programming. I used I2C protocol and connected at first 2 boards and if I would manage do it I thought I would connect 3 boards with I2C protocol.
In order to understand how to program my boards I read this article Wire I2C.
First I programmed primary board. I wrote program for endswitch. Primary board would send 1 if I did not press the endswitch and 0 if I pressed. In primary board I wrote also secondary board address that I wrote in secondary board program. I wrote program for secondary board it needs to receive information (0 or 1 ) from primary board and store it in receiveData variable, the type of variable is byte. If receiveData equal 1 the led that connected to the secondary board would turn off wait 200 milliseconds and turn on and stayed on again 200 milliseconds and if the receiveData equal 0 the led would be always on. At first I made mistake because I wrote te receiveEvent function without argument and got the error. I wrote the error text in Copilot and it gave me solution that really helped.
Here is the code for my primary board.
Primary board code
#include <Wire.h>
const int endSwitchPin =3;
int endSwitchState = 0;
void setup() {
Wire.begin();
pinMode(endSwitchPin, INPUT_PULLUP);
}
void loop() {
endSwitchState = digitalRead(endSwitchPin);
if (endSwitchState == HIGH) {
Wire.beginTransmission(0x9);
Wire.write(1);
Wire.endTransmission();
delay(200);
} else {
Wire.beginTransmission(0x9);
Wire.write(0);
Wire.endTransmission();
delay(200);
}
}
Secondary board code
#include <Wire.h>
#define I2C_Secondary_ADDRESS 0x9
bool ledState;
int led=3;
void setup() {
pinMode(led, OUTPUT);
Wire.begin(I2C_Secondary_ADDRESS);
Wire.onReceive(receiveEvent);
}
void loop() {
if(ledState==true){
digitalWrite(led,LOW);
delay(200);
digitalWrite(led,HIGH);
delay(200);
}else{
digitalWrite(led,HIGH);
}
delay(100);
}
void receiveEvent(int numBytes) {
byte recivedData=0;
if(Wire.available()){
recivedData=Wire.read();
if(recivedData==1){
ledState=true;
}else if(recivedData==0){
ledState=false;
}
}
}
After that I decided to connect 3 boards together with I2C protocol.
I changed the code of primary board. Because I need to control the two secondary boards. Again I put an endswitch that would control led in first secondary board and Servo in second secondary board.
Primary board code
#include <Wire.h>
const int endSwitchPin = 3;
int switchState = 0;
void setup() {
Wire.begin(); // Initialize I2C communication
pinMode(endSwitchPin, INPUT_PULLUP);
}
void loop() {
switchState = digitalRead(endSwitchPin);
if (switchState == LOW) {
Wire.beginTransmission(0x9);
Wire.write(1); // Command for first secondary board
Wire.endTransmission();
// Send command to second secondary (address 0x8)
Wire.beginTransmission(0x8);
Wire.write(1);
Wire.endTransmission();
} else {
Wire.beginTransmission(0x9);
Wire.write(0);
Wire.endTransmission();
Wire.beginTransmission(0x8);
Wire.write(0);
Wire.endTransmission();
delay(200);
}
}
First Secondary board (led ) code
#include <Wire.h>
#define I2C_Secondary1_ADDRESS 0x9
bool ledState;
int led=3;
void setup() {
pinMode(led, OUTPUT);
Wire.begin(I2C_Secondary1_ADDRESS);
Wire.onReceive(receiveEvent);
}
void loop() {
if(ledState==true){
digitalWrite(led,LOW);
delay(200);
digitalWrite(led,HIGH);
delay(200);
}else{
digitalWrite(led,HIGH);
}
delay(100);
}
void receiveEvent(int numBytes) {
byte recivedData=0;
if(Wire.available()){
recivedData=Wire.read();
if(recivedData==1){
ledState=true;
}else if(recivedData==0){
ledState=false;
}
}
}
Second Secondary board (servo motor) code
#include <Servo_megaTinyCore.h>
#include <Wire.h>
#define I2C_Secondary2_ADDRESS 0x8
Servo motor;
bool motorState;
int motorPin=3;
void setup() {
motor.attach(motorPin);
Wire.begin(I2C_Secondary2_ADDRESS);
Wire.onReceive(receiveEvent);
}
void loop() {
if(motorState==true){
motor.write(180);
delay(200);
}
else{
motor.write(90);
delay(200);
}
delay(100);
}
void receiveEvent(int numBytes) {
byte recivedData=0;
if(Wire.available()){
recivedData=Wire.read();
if(recivedData==1){
motorState=true;
}
if(recivedData==0){
motorState=false;
}
}
}
Conclusion¶
This week was quite interesting I used I2C protocol for connecting LCD screen but never connect boards together. At first it seemed hard but when I started programming and understand how to connect them and how to send commands from primary board everything seemed easy and I decided to make my work a little bit hard and connect second secondary board. In the future I want to use I2C protocol in my final project.