13. Networking and communications
Hero Shot
Assignment Overview
Group Assignment
- Send a message between two projects.
The group assignment page can be found on the Energylab 2024 website here
it is also embedded directly in the webpage below.
Individual Assignment
- Design, build, and connect wired or wireless node(s) with network or bus addresses and local input and/or output devices.
Introduction
This week’s assignment was to explore networking and communication by designing, building, and connecting nodes (wired or wireless) with network or bus addresses. I utilized:
- A distance sensor (input) for the master node to measure proximity.
- A buzzer and two servo motors (outputs) for the slave node to respond to the distance data received.
We implemented the I2C protocol for communication between the master and slave nodes.
Process Workflow
1. Concept and Network Design
- Network Structure:
- Master Node: Measures the distance using an ultrasonic sensor and sends the data to the slave node.
-
Slave Node: Processes the data to:
- Trigger a buzzer when the distance is below a threshold.
- Adjust the position of two servo motors based on the distance.
-
I2C Communication Protocol:
-
Unique addresses were assigned:
- Master Node:
0x01
- Slave Node:
0x02
- Master Node:
-
System Diagram:
2. Building and Programming the Nodes
Master Node (Input Board):
- Hardware: My custom-designed board featuring an ultrasonic distance sensor (HC-SR04).
- Functionality: Measures the distance and sends it to the slave node via I2C.
Slave Node (Output Board):
- Hardware: My custom-designed board with a buzzer and two servo motors.
- Functionality: Responds to the distance data received:
- Activates the buzzer if the distance is below 20 cm.
- Adjusts servo positions proportionally to the measured distance.
- Hardware Setup:
3. Programming and Code
Master Node Code:
#include <Wire.h>
#define SLAVE_ADDRESS 0x02
#define TRIG_PIN 2
#define ECHO_PIN 3
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Wire.begin(); // Initialize as master
}
void loop() {
// Measure distance
long duration, distance;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = duration * 0.034 / 2; // Convert to cm
// Send distance to slave
Wire.beginTransmission(SLAVE_ADDRESS);
Wire.write(distance);
Wire.endTransmission();
delay(100);
}
slave node code
#include <Wire.h>
#include <Servo.h>
#define BUZZER_PIN 9
Servo servo1;
Servo servo2;
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
servo1.attach(5); // Servo motor 1
servo2.attach(6); // Servo motor 2
Wire.begin(0x02); // Initialize as slave with address 0x02
Wire.onReceive(receiveEvent);
}
void loop() {
// Nothing in loop; everything is handled in receiveEvent
}
void receiveEvent(int bytes) {
int distance = Wire.read(); // Read distance from master
if (distance < 20) {
digitalWrite(BUZZER_PIN, HIGH); // Trigger buzzer
} else {
digitalWrite(BUZZER_PIN, LOW); // Turn off buzzer
}
int servoAngle = map(distance, 0, 100, 0, 180); // Map distance to servo angle
servo1.write(servoAngle);
servo2.write(180 - servoAngle); // Opposite angle for second servo
}
Testing and Debugging
During the testing phase, I monitored the system’s behavior and functionality through various means, including observing the serial monitor output from the master node and visual checks of the components in action.
Serial Monitor Output from Master Node
The following table summarizes the distance readings and corresponding actions taken by the system:
Distance (cm) | Buzzer State | Servo1 Angle | Servo2 Angle |
---|---|---|---|
<20 | ON | Adjusted | Adjusted |
≥20 | OFF | Adjusted | Adjusted |
Observations
1- Distance Measurements:
The distance sensor provided accurate measurements, which were correctly transmitted to the slave node.
2- Buzzer Functionality:
The buzzer activated when objects came within a distance of 20 cm or less, confirming the distance sensor’s integration with the output device.
3- Servo Motor Response:
The servo motors adjusted their angles based on the distance measured, demonstrating the effective mapping of distance to servo position.
Photos of the Testing Setup
Here are some photos taken during the testing phase, showcasing the setup and the components in action:
File
What went well / What went wrong
- The distance sensor successfully measured the proximity of objects and transmitted accurate data to the slave node.
- The buzzer activated correctly when the distance fell below the specified threshold, confirming the effective communication between the nodes.
- The servo motors responded appropriately to the distance readings, demonstrating that the mapping of distance to servo position was effective.
- The use of the I2C protocol allowed for smooth communication between the master and slave nodes, enhancing the overall system functionality.