For this week we had two assignments, one individual and the other group assignment.
Individual Assignment:
Design and build a wired and or a wireless network connecting at least two processors.
When it comes to connecting two different processors, there are many ways of doing so. But they mainly fall into two categories, wired and wireless.
Wireless includes bluetooth, wifi and radio.
I chose to go with bluetooth. Yet I did not have all the necessary components. We have the bluetooth module HC-06 available in the lab and supply range. The HC-06 module can only serve as a slave, as in it can only receive commands and relay them accordingly. In order to have two processors communicate, we need the HC-05 which can serve as a master and slave which would in turn connect to one or more processors linked to an HC-06. Since I have an iPhone which uses bluetooth 4.0, this requires a BLE module which is called HM-10. I was not able to communicate with it or get it running. But I did connect and test the HC-06 to both my arduino and personal PCB but could only connect through them with my laptop (macbook pro) and controlled them through serial communication on the serial monitor of my arduino IDE. It was relatively simple and easy to do pictures and codes are as posted below.
This is the HC-06 bluetooth module. As you can see the pinouts are straight forward, one goes to Vcc (3.3V) and another to GND. the Tx and Rx go on the Arduino UNO respectively on the Rx and Tx (pin 0, 1)
When it comes to communicating with the Arduino through bluetooth simply upload the code (shown below) to the Arduino while having the Rx and Tx disconnected as it might interfere with the upload.
Code:
int LED = 13;
void setup() {
// initialize serial:
Serial.begin(9600);
// initialize the led pin
pinMode(13, OUTPUT);
}
void loop() {
while (Serial.available()) {
char inChar = (char)Serial.read();
switch(inChar) {
case '1':
digitalWrite(13, HIGH);
break;
case '0':
digitalWrite(13, LOW);
break;
case '2':
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
break;
}
int val = digitalRead(switch1);
if (val == HIGH) {
Serial.println("0");
}else{
Serial.println("1");
}
Serial.println(inChar);
}
}
Once the code is uploaded to the Arduino you can now disconnect it and plug it into a different power source or keep it as this would not matter anymore. To connect to the board through blutooth, as stated above I used my laptop and the steps are as follows.
Open bluetooth preferences, scan nearby devices and select the HC-06. The default password for this module would be '1234' or '0000'.
Once the devices pair, go to the arduino IDE and select Tools>Port>HC-06. To control the board through the serial monitor.
Once the port is selected, open the arduino IDE Serial Monitor.
In this code I set it to turn on the LED when I input '1' to the serial monitor, '0' to turn it off and '2' for it to blink. A demo of this is shown in the attached video below.
As for the wired communication, I tried serial and I2C communication. With serial I basically had one arduino with an ultrasonic sensor with a code that includes the necessary code to run the ultrasonic sensor and a library that allows it to send serial information through specicfied pins to another arduino that is set up to receive that data. The results (distance) that come through the ultrasonic sensor to the arduino and onto the second arduino where the data is shown.
The ultrasonic sensor is connected to one Arduino and that arduino is then connected to another PCB through specified pins that act as transmitter and receiver. The second PCB which has the receiver code uploaded, displays the distance showed by the ultrasonic sensor on the first PCB. The codes are as follows:
Sender Code:
#include
int trigPin = 7; //Trig - green Jumper
int echoPin = 6; //Echo - yellow Jumper
long duration, cm, inches;
void setup() {
//Serial Port begin
Serial.begin (9600);
mySerial.begin(9600);
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
cm = (duration/2) / 29.1;
inches = (duration/2) / 74;
Serial.println(cm);
mySerial.println(cm);
mySerial.println(" cm");
delay(250);
}
Receiver Code:
#include
//pins used for the communication, wired respectively
SoftwareSerial mySerial(8,9);
void setup(){
mySerial.begin(9600);
Serial.begin(9600);
}
void loop(){
//reading the data sent
if (mySerial.available()>0){
Serial.write(mySerial.read());
}
}
For serial communication, connect the two boards through pin 8 and 9 (in this case following the code)to pins 5 and 8 and share GND
The board used in this example is from the Input/Output Devices Assignment, check the linked page to follow the process of the making of this PCB.
Video of this process:
Lastly we tried the I2C connection which basically is having one master processor that sends to multiple slave processors, these processors are identified in the main master code and sent commands respectively.
To make it easier to reproduce this code and process, I have made the master code to give commands to one slave processor where you would be able to clearly see in the slave code that it identifies itself based on the code of the master, in this case it was '9'. You would notice that the master only sends a signal to the slave, the slave has its own process that gets initiated when the signal from the master is received.
Master Code:
#include
int x = 0;
void setup() {
// Start the I2C Bus as Master
Wire.begin();
}
void loop() {
Wire.beginTransmission(9); // transmit to device #9
Wire.write(x); // sends x
Wire.endTransmission(); // stop transmitting
x++; // Increment x
if (x > 5) x = 0; // `reset x once it gets 6
delay(500);
}
Slave Code:
#include
int LED = 13;
int x = 0;
void setup() {
// Define the LED pin as Output
pinMode (LED, OUTPUT);
// Start the I2C Bus as Slave on address 9
Wire.begin(9);
// Attach a function to trigger when something is received.
Wire.onReceive(receiveEvent);
}
void receiveEvent(int bytes) {
x = Wire.read(); // read one character from the I2C
}
void loop() {
//If value received is 0 blink LED for 200 ms
if (x == 0) {
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
delay(200);
}
if (x == 1) {
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(500);
}
if (x == 2) {
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(1000);
}
}
Video of I2C connections with one slave and two masters, notice how the built in pin 13 led blinks when it receives the signal.
In this case for the I2C connection, we had the boards share GND by linking them and connect them through the Analog pins of the Arduino UNO A4 and A5