
(3.2-orientation)=
# 🧩 3.2 Serial Communication

Design and execute software to read data from a mass balance using serial communication.

```{contents}
:depth: 3
```

## 🔰 Tutorial

Serial communication is a method of transmitting data between devices one bit at a time. It's widely used in scientific instruments, industrial equipment, and embedded systems due to its simplicity and reliability. In this tutorial, we'll focus on RS232, a common serial communication standard, and how to interface it with modern microcontrollers.

### The Stars of Our Show 🌟

Let's meet the key players in our serial communication setup:

1. **Raspberry Pi Pico WH** 🥧
   This little powerhouse is our brain! It's a versatile microcontroller with built-in UART capabilities, making it perfect for serial communication projects.

   Fun fact: The 'WH' stands for 'with headers' - it comes pre-soldered for easy use! 🔌

2. **Analytical Balance** ⚖️
   We're using the A&D HR-100A, a high-precision scale that can measure up to 102g with an accuracy of 0.0001g!

   Did you know? This balance is so sensitive it can detect the weight of a fingerprint! 👆💨

3. **RS232 to TTL Converter** 🔄
   This is our translator! It converts the RS232 signals from the balance to the 3.3V logic that our Pico understands.

   Imagine it as a language interpreter between the balance and the Pico! 🗣️🔊

### Bill of Materials
- [Raspberry Pi Pico WH](https://www.raspberrypi.com/products/raspberry-pi-pico/?variant=raspberry-pi-pico-wh)
- [USB-A to micro USB-B cable](https://www.digikey.ca/en/products/detail/stewart-connector/SC-2AMK003F/8544577)
- [5V wall adapter](https://www.digikey.ca/en/products/detail/phihong-usa/PSAA05A-050QL6-R/6560437)
- [Adafruit Terminal PiCowbell for Pico with Pre-Soldered Sockets](https://www.adafruit.com/product/5907)
- [A&D HR-100A Analytical Balance - 102g x 0.0001g](https://ceproducts.shop/collections/current-offerings/products/a-d-hr-100a-102g-x-0-0001g-analytical-balance) ([alternative link](https://weighing.andonline.com/product/galaxy-hr-series-balances/hr-100a?commerce_product=31) (requires quote), alternatives: [A&D FX-120i Reloading Scale - 122g x 0.001g](https://ceproducts.shop/collections/current-offerings/products/fx-120i-reloading-scale-122g-x-0-001g), [U.S. Solid USS-DBS46-3 0.001g](https://www.amazon.ca/U-S-Solid-Analytical-Precision-Laboratories/dp/B07V7SXFQB) - note: US Solid RS232 commands will differ)
- [2-Channel RS232 Module for Raspberry Pi Pico](https://www.waveshare.com/pico-2ch-rs232.htm) (alternatives: [Adafruit RS232 Pal](https://www.adafruit.com/product/5987) (soldering required), [Industrial RS232 to 3.3V TTL Converter TTL-232-33IE](https://www.serialcomm.com/serial_rs232_converters/rs232_rs485_to_ttl_converters/industrial_rs232_to_3.3v_ttl_converter/industrial_rs232_to_3v_ttl.product_general_info.aspx))
- [Electrical wire (3 feet, black)](https://www.digikey.ca/en/products/detail/cnc-tech/10981-18-1-2000-001-1-TD/17799168)
- [Mini flathead screwdriver set](https://www.amazon.ca/dp/B08QCT9NHY/) (one for DC Motor driver terminals and a smaller one for PiCowbell terminals)

The Raspberry Pi Pico WH is a versatile microcontroller that's ideal for this project due to its built-in UART (Universal Asynchronous Receiver/Transmitter) capabilities. The RS232 to TTL converter is crucial as it translates the voltage levels between the RS232 standard (used by many scientific instruments) and the 3.3V logic used by the Pico.

### Why This Matters 🤔

Understanding serial communication opens up a world of possibilities:

- 🔬 In labs, it allows precise data collection from various instruments.
- 🏭 In industry, it enables monitoring and control of complex machinery.
- 🤖 In robotics, it facilitates communication between different parts of a system.

### Real-World Applications 🌍

1. **Pharmaceutical Research** 💊
   Serial communication is crucial in drug development for precise measurement and data logging.

2. **Environmental Monitoring** 🌱
   Weather stations use serial communication to collect and transmit data from various sensors.

3. **Manufacturing Quality Control** 🏭
   Production lines use serial communication for real-time monitoring of product weights and dimensions.

### Demo

✅ Read [HR Analytical Balance Literature](https://weighing.andonline.com/sites/default/files/documents/HR-A_HR-AZ%20Lit_web.pdf)

This document provides an overview of the analytical balance we'll be interfacing with. Understanding the capabilities and specifications of the instrument is crucial for effective integration.

✅ Skim [HR Analytical Balance Manual](https://weighing.andonline.com/sites/default/files/documents/HR-A_HR-AZ_Manual.pdf). Read Sections 15 and 17 closely.

Sections 15 and 17 detail the RS232C interface and the commands used to communicate with the balance. This information is essential for programming our microcontroller to interact with the balance correctly.

✅ Read [this RS232 ChatGPT transcript](https://chatgpt.com/share/eb858762-b80f-4a4d-bd12-0a4a47f116f9)

This transcript provides additional context and explanations about RS232 communication, helping to reinforce your understanding of the concepts.

✅ Read about [code for Adafruit RS232 Pal](https://learn.adafruit.com/adafruit-rs232-pal/circuitpython-and-python) (note, this is for CircuitPython and Python, not MicroPython, so it's informative only)

While not directly applicable to our MicroPython setup, this resource provides valuable insights into how serial communication is implemented in Python environments.

Run the following code on your microcontroller:

```python
import machine
import utime

# Initialize UART
uart = machine.UART(1, baudrate=9600, tx=machine.Pin(4), rx=machine.Pin(5))

# Send the "Q" command and read response
uart.write(b"Q\r\n")
utime.sleep(0.1)
if uart.any():
    print(uart.read().decode("utf-8"))

# Continuous reading loop
while True:
    if uart.any():
        print(uart.read().decode("utf-8"))
    utime.sleep(1)
```

This code demonstrates how to initialize the UART, send a command to the balance, and continuously read data. The "Q" command typically requests the current weight reading from the balance. The continuous loop allows for real-time monitoring of the balance's output.

Understanding this code is crucial as it forms the foundation for more complex data acquisition and control systems in laboratory automation. You might extend this code to log data over time, trigger actions based on weight thresholds, or integrate with other instruments in a larger experimental setup.

### Expanding Horizons 🚀

Once you master this, you can:
- 📊 Create data logging systems for long-term experiments.
- 🚨 Set up alert systems for when weights go above or below certain thresholds.
- 🤖 Integrate weight data into larger automated systems, like robotic material handlers.

## 🚀 Quiz

::::{tab-set}
:sync-group: category

:::{tab-item} W 2024
:sync: w2024

:::

:::{tab-item} Sp/Su 2024
:sync: sp2024

https://q.utoronto.ca/courses/370069/assignments/1393645?display=full_width
:::

:::{tab-item} Sp/Su 2025
:sync: sp2025

https://q.utoronto.ca/courses/393350/assignments/1499705?display=full_width_with_nav
:::

::::

## 📄 Assignment

::::{tab-set}
:sync-group: category

:::{tab-item} W 2024
:sync: w2024

:::

:::{tab-item} Sp/Su 2024
:sync: sp2024

https://q.utoronto.ca/courses/370069/assignments/1397943?display=full_width
:::

:::{tab-item} Sp/Su 2025
:sync: sp2025

https://q.utoronto.ca/courses/393350/assignments/1499706?display=full_width_with_nav
:::

::::
