Skip to content

Commit 6e2e4ff

Browse files
Added Serial protocols
Missing example for UART with image (will add when I have a sensor)
1 parent ad6dd60 commit 6e2e4ff

File tree

8 files changed

+203
-0
lines changed

8 files changed

+203
-0
lines changed
Loading
Loading
Loading
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
---
2+
title: Communication Protocol - I2C
3+
description: Learn how to use I2C on Micropython
4+
author: Pedro Sousa Lima
5+
---
6+
7+
#### Introduction
8+
Inter-Integrated Circuit, or **I2C**, is one of the most widely used serial communication protocols, especially in the Arduino ecosystem. It enables multiple devices to communicate over just two wires, making it both efficient and versatile as you can easily add new modules more easily as the connections all happen across the same bus and standetizes them across manufacturers (and product lines).
9+
10+
11+
#### How I2C Works
12+
I2C communication relies on two wires:
13+
- **SDA (Serial Data):** Transfers data between devices.
14+
- **SCL (Serial Clock):** Synchronizes data transmission.
15+
16+
Each device on an I2C bus has a unique address, allowing the controller to communicate directly with a specific device. This capability makes it possible to connect up to 128 devices on the same two wires (128 is the maximum number of different adresses you can have as the adress is a 7 bit value and you can only express 128 unique values using 7 bits).
17+
18+
![I2C Diagram](assets/i2c.png)
19+
20+
21+
#### Key Features of I2C
22+
1. **Two-Wire Simplicity:** Reduces hardware complexity.
23+
2. **Address-Based Communication:** Each device has a unique address.
24+
3. **Bidirectional Communication:** Enables data transfer in both directions.
25+
26+
27+
#### Example: Reading Sensor Data Using I2C
28+
In this example, we will connect an **LIS3DHTR accelerometer** to an Arduino and read its data via I2C.
29+
30+
**Circuit Diagram:**
31+
![LIS3DHTR circuit.](assets/circuitAccelerometer.png)
32+
33+
**Setup Instructions:**
34+
1. Connect the LIS3DHTR to your board as shown in the circuit diagram.
35+
2. Install the required `lis3dh` module by running the following command:
36+
37+
```python
38+
mip.install("https://raw.githubusercontent.com/tinypico/tinypico-micropython/master/lis3dh%20library/lis3dh.py")
39+
```
40+
41+
3. Copy the following code to your `main.py` file and run it.
42+
43+
**Code:**
44+
```python
45+
import lis3dh, time, math
46+
from machine import Pin, I2C
47+
48+
i2c = I2C(sda=Pin(8), scl=Pin(9))
49+
imu = lis3dh.LIS3DH_I2C(i2c, address=0x19)
50+
51+
last_convert_time = 0
52+
convert_interval = 100 #ms
53+
pitch = 0
54+
roll = 0
55+
56+
# Convert acceleration to Pitch and Roll
57+
def convert_accell_rotation( vec ):
58+
x_Buff = vec[0] # x
59+
y_Buff = vec[1] # y
60+
z_Buff = vec[2] # z
61+
62+
global last_convert_time, convert_interval, roll, pitch
63+
64+
# We only want to re-process the values every 100 ms
65+
if last_convert_time < time.ticks_ms():
66+
last_convert_time = time.ticks_ms() + convert_interval
67+
68+
roll = math.atan2(y_Buff , z_Buff) * 57.3
69+
pitch = math.atan2((- x_Buff) , math.sqrt(y_Buff * y_Buff + z_Buff * z_Buff)) * 57.3
70+
71+
# Return the current values in roll and pitch
72+
return ( roll, pitch )
73+
74+
# If we have found the LIS3DH
75+
if imu.device_check():
76+
# Set range of accelerometer (can be RANGE_2_G, RANGE_4_G, RANGE_8_G or RANGE_16_G).
77+
imu.range = lis3dh.RANGE_2_G
78+
79+
# Loop forever printing values
80+
while True:
81+
# Read accelerometer values (in m / s ^ 2). Returns a 3-tuple of x, y,
82+
# z axis values. Divide them by 9.806 to convert to Gs.
83+
x, y, z = [value / lis3dh.STANDARD_GRAVITY for value in imu.acceleration]
84+
print("x = %0.3f G, y = %0.3f G, z = %0.3f G" % (x, y, z))
85+
86+
# Convert acceleration to Pitch and Roll and print values
87+
p, r = convert_accell_rotation( imu.acceleration )
88+
print("pitch = %0.2f, roll = %0.2f" % (p,r))
89+
90+
# Small delay to keep things responsive but give time for interrupt processing.
91+
time.sleep(0.1)
92+
```
93+
94+
**Expected Output:**
95+
In the REPL terminal, you will see accelerometer data displayed every second. For example:
96+
![Expected Output](assets/repl-i2c-sensor.gif)
97+
98+
```
99+
x = 0.001 G, y = -0.002 G, z = 1.000 G
100+
pitch = -0.12, roll = 0.04
101+
```
102+
103+
104+
#### Summary
105+
I2C is a highly efficient protocol that enables multiple devices to communicate on just two wires. In this example, we demonstrated how to read data from an I2C-connected sensor, opening the door to integrating even more devices into your Arduino projects.
106+
Loading
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
title: Communication Protocol - SPI
3+
description: Learn how to use SPI on Micropython
4+
author: Pedro Sousa Lima
5+
---
6+
7+
#### Introduction
8+
The **Serial Peripheral Interface (SPI)** is a high-speed serial communication protocol widely used in embedded systems, including Arduino. It allows data exchange between devices using a straightforward and fast four-wire setup. Having a known and stadertized pinout makes easier to add new modules to your project as they remain the same across manufacturers (and product lines).
9+
10+
11+
12+
#### How SPI Works
13+
SPI communication uses four main lines:
14+
- **COPI (Controller Out, Peripheral In):** Sends data from the controller to the peripheral.
15+
- **CIPO (Controller In, Peripheral Out):** Sends data from the peripheral to the controller.
16+
- **SCK (Serial Clock):** Synchronizes data transfer between devices.
17+
- **CS (Chip Select):** Activates a specific peripheral for communication.
18+
19+
The controller initiates communication by enabling the peripheral through the **CS** line. Data is transmitted bit by bit in synchronization with the clock signal on the **SCK** line.
20+
21+
22+
23+
#### Key Features of SPI
24+
1. **Full-Duplex Communication:** Simultaneous data transmission and reception.
25+
2. **High Speed:** Faster than I2C, suitable for performance-critical applications.
26+
3. **Multiple Devices:** Supports multiple peripherals, each with a dedicated **CS** line.
27+
4. **Simple Protocol:** No addressing required, reducing communication overhead.
28+
29+
30+
31+
#### Example: Connecting an SPI Device
32+
In this example, we explore how to wire and interact with SPI devices using an Arduino. Many sensors and displays use SPI for their speed and reliability.
33+
34+
**Wiring Diagram:**
35+
![How to wire SPI devices.](assets/spi.png)
36+
37+
**Key Pins:**
38+
1. **COPI** (Controller Out, Peripheral In) – Sends data to the peripheral.
39+
2. **CIPO** (Controller In, Peripheral Out) – Receives data from the peripheral.
40+
3. **SCK** (Serial Clock) – Synchronizes data transfer.
41+
4. **CS** (Chip Select) – Enables the specific peripheral.
42+
43+
**Example Operation:**
44+
SPI communication involves enabling a peripheral via the **CS** line, sending data through **COPI**, and receiving responses via **CIPO**.
45+
46+
47+
48+
#### SPI in Practice
49+
Many common devices, such as high-speed sensors, displays, and memory modules, utilize SPI for its speed and efficient design. The protocol's simplicity makes it a go-to choice for connecting peripherals requiring high data throughput.
50+
51+
52+
53+
#### Summary
54+
SPI is a robust, high-speed protocol that provides an efficient means of communication between an Arduino and peripherals. Its simplicity, speed, and flexibility make it an indispensable tool for a wide variety of electronics projects. With SPI, you can reliably handle everything from sensors to displays in performance-critical environments.
Loading
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: Communication Protocol - UART
3+
description: Learn how to use UART on Micropython
4+
author: Pedro Sousa Lima
5+
---
6+
7+
#### Introduction
8+
The **Universal Asynchronous Receiver-Transmitter (UART)** protocol is one of the simplest and most commonly used communication protocols. It is ideal for straightforward one-to-one communication between devices, making it a staple in embedded systems, including Arduino projects.
9+
10+
11+
#### How UART Works
12+
UART operates using two main lines:
13+
- **TX (Transmit):** Sends data from one device to another.
14+
- **RX (Receive):** Receives data from the transmitting device.
15+
16+
Unlike protocols like I2C or SPI, UART is asynchronous, meaning it does not require a clock signal to synchronize communication. Instead, both devices agree on a common baud rate, which determines the speed of data transmission.
17+
18+
19+
#### Key Features of UART
20+
1. **Two-Wire Simplicity:** Minimal wiring requirements with only TX and RX lines.
21+
2. **Asynchronous Communication:** No clock line needed.
22+
3. **Configurable Baud Rates:** Common rates include 9600, 115200, and others.
23+
4. **One-to-One Communication:** Designed for direct communication between two devices.
24+
25+
26+
#### Example: Communicating via UART
27+
UART is widely used for debugging, serial monitors, and communication with modules like GPS or GSM. In this example, we connect an Arduino to a UART-enabled device.
28+
29+
**Wiring Diagram:**
30+
![How to wire UART devices.](assets/uart.png)
31+
32+
**Key Pins:**
33+
1. **TX:** Connects to the RX pin of the receiving device.
34+
2. **RX:** Connects to the TX pin of the transmitting device.
35+
3. **GND:** Establishes a common ground between devices.
36+
37+
38+
#### Practical Example
39+
The example provided in the original article demonstrates how UART facilitates direct, real-time communication between devices. When implemented, UART ensures reliable transmission of data, making it invaluable for basic communication needs.
40+
41+
42+
#### Summary
43+
UART remains a cornerstone protocol for simple, direct communication between devices. Its ease of use and minimal hardware requirements make it an essential tool for debugging and connecting peripherals in Arduino and other embedded systems.

0 commit comments

Comments
 (0)