Embedded Networking and Communications
This week's assignment was about "Embedded Networking and Communications." As the name suggests, it involves integrating networking and communication capabilities into embedded systems, which are specialized devices designed for specific tasks within larger systems. This integration allows devices to share data and resources efficiently using protocols like TCP/IP, I2C, and SPI, and interfaces like Wi-Fi and Ethernet. Embedded networking and communications are essential in applications such as IoT, automotive systems, and smart homes. However, they also present challenges in security, interoperability, and power management.
Group assignment
This week's group assignment focused on establishing communication between two projects and documenting the process. I gained insights into various communication protocols and their functionalities. I found the I2C protocol particularly interesting and effective for its structured approach and efficient data transfer capabilities. If you want to check this very useful resoruce you can click here.
Comunications protocols
There are several communication protocols, but the ones used in embedded systems and electonic devices are serial communication protocols, the most commly and most used ones are:
-
UART
UART (Asynchronous Receiver-Transmitter) is a simple and widely used asynchronous serial communication protocol. It is commonly used for communication between a microcontoller and peripheral devices such as sensors, GPS modules and wireless modules. It uses 2 wires (TX and RX) for communication and does not requiere a clock signal.
-
SPI
SPI (Serial Peripheral Interface) is a synchronous serial communicaction protocol. It is commonly used for high-speed communication between microconntroller and peripheral devices, that require high data rates, such as display, memory chips and sensors. It supports full-duplex communication (simultaneous sending and receiving), typically uses four wires (clock, master-out-slave-in, master-in-slave-out, and chip select), and is efficient for short-distance sommunication.
-
I2C
I2C (Inter-Integrated Circuit) is a synchronous serial communication protocol. It is commonly used for connecting multiple devices on the same bus, such as sensors, EEPROMs, and other integrated circuits. It Supports multi-master communication (multiple devices can initiate communication), uses two wires (clock and data), and allows for addressing multiple devices on the same bus.
-
RS-232
RS-232 (Recommended Standard 232) is a standard for serial communication between devices. It was historically used for connecting computers to peripheral devices like modems, printers, and terminals. It uses a DB9 or DB25 connector and is characterized by its voltage levels and signal timing specifications.
I2C protocol
For this week's assignment, I used the I2C protocol because it is very easy to set up and highly
versatile. I believe it is the best communication protocol since it supports many devices on the
same bus by simply changing the address. Additionally, it accommodates multiple slaves and
multiple masters, making it a flexible and efficient choice.
Using this protocol, I programmed it to play the Imperial March from Star Wars. When a certain
action triggers the system, the song will play, and when the action is triggered again, it will
turn off.
Board
For this assignment, I made a new board to ensure I had enough pins, as this was an issue with previous boards. However, I forgot to add the I2C resistors, which I will explain shortly. I'll go through this explanation quickly because I already created a page on how to make PCBs. If you want to learn more about this process, you can click here.
Schematic and PCB
As I mentioned, this board is very simple; it's just a collection of pins to connect everything. I also made it smaller than usual, so it is lightweight and easy to transport.
Conections
The I2C connections are straightforward. I used the XIAO ESP32 C3 as the master. Since this
device doesn't have built-in pull-up resistors for I2C communication, you need to add them.
These resistors activate the channels because the SDA and SCL lines are active LOW by default.
Initially, I didn't realize this and didn't include the resistors on the PCB. The typical
value for these resistors is 4.7K ohms. Connect one end of a resistor to the SDA line and the
other end to 3.3V. Repeat this for the SCL line. You can connect these resistors directly to
the board; an external power source is not needed.
Once you have connected the resistors, connect the SDA and SCL lines of the XIAO ESP32 C3,
with the ESP32 and make sure to connect the ground as well.
For the buzzer or speaker, to hear it at its full potential, you need a transistor. I'm using the common 2N2222 BJT NPN transistor. The connections are as follows:
- The emitter (left pin) is connected to ground.
- The base (middle pin) is connected to a 1k ohm resistor, which is then connected to a digital pin (in this case, digital pin 5, but any pin of your choice can be used).
- The collector (right pin) is connected to one end of the buzzer or speaker.
The speaker connections are even simpler: connect one terminal to the transistor and the
other terminal to the 3.3V supply.
The push button connections are simple as well. When working with pull-up and pull-down
resistors, it's important to minimize the chance of the button sending an incorrect value.
In my case, I used a pull-up resistor.
Programs
This first code is for the master device. You can upload it to either board, adjusting the
connection configurations and specifying the pins for the buzzer and button as needed. In
my case, I uploaded this code to the XIAO ESP32 C3.
It is very important that the address is the same for both the master and the slave. In this
case, both are boards, so the range for selecting the address is between 0x00 and 0x7F. This
is because the address is 7 bits, providing 128 possibilities. Be careful, as some addresses
are reserved for specific purposes. For example, 0x00 (General Call Address) is used to send
commands to all devices on the bus. Addresses from 0x01 to 0x07 are also reserved for special
purposes and should not be used for general slave devices. Additionally, addresses from 0x78
to 0x7F are reserved for future expansions and primary purposes. The slave needs the address
the master won't need it.
This second code is for the slave device. Ensure the addresses match between the master and slave. Since this is a slave code and it doesn't need to send any data back to the master, it only requires the 'onReceive' function. This function will receive the data sent by the master when the button is pushed, allowing for one-way communication.