(3.1-orientation)=
# 🧩 3.1 Pumps and Pipettes

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

## 🔰 Tutorial

In this module, you will develop software to:
1. Manage a peristaltic pump's operations using a microcontroller and motor driver
2. Manipulate the linear actuator of the Digital Pipette
3. Control a stepper motor for precise positioning
4. Manage fan speed for temperature control in laboratory equipment

These skills are fundamental in creating automated liquid handling systems for various scientific applications.

### Peristaltic Pump

First, you will manage a peristaltic pump’s operations using a microcontroller and motor driver. Consider purchasing the hardware and setting it up yourself to enhance the experience. Hardware is optional for the completion of this module.

#### Bill of Materials

- [MDD3A DC Motor Driver](https://www.cytron.io/p-3amp-4v-16v-dc-motor-driver-2-channels)
- [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)
- [Adafruit Terminal PiCowbell for Pico with Pre-Soldered Sockets](https://www.adafruit.com/product/5907)
- [12V Peristaltic Pump](https://www.adafruit.com/product/1150)  (alternative: [5V peristaltic pump](https://www.adafruit.com/product/3910), but requires sufficient 5V power supply | optional: [silicone tubing](https://www.adafruit.com/product/3659))
- [12V 3A power supply](https://www.digikey.ca/en/products/detail/xp-power/VEL36US120-US-JA/6220849) (alternative: [12V 1A version](https://www.digikey.ca/en/products/detail/xp-power/VEL12US120-US-JA/5726833))
- [Jack to bare wire](https://www.digikey.ca/en/products/detail/tensility-international-corp/10-02247/6412282)
- [Alligator clip to female jumper wire pack](https://www.adafruit.com/product/4304) (only 2 required)
- [M/F silicone jumper wires](https://www.adafruit.com/product/5837) (non-silicone also OK, only 4 required)
- [Mini flathead screwdriver set](https://www.amazon.ca/dp/B08QCT9NHY/) (one for DC Motor driver terminals and a smaller one for PiCowbell terminals)
- Soldering kit (for bare wires to peristaltic pump connections)
- [OPTIONAL] [Plastic beakers](https://www.amazon.ca/Measuring-Graduated-Polypropylene-Laboratory-Experiments/dp/B083JC39W8)
- [OPTIONAL] [Blue food coloring](https://www.amazon.ca/Club-House-Food-Colour-Preparation/dp/B00HVVNFPI/)
<!-- - [OPTIONAL] [Glycerin](https://www.amazon.ca/NOW-Vegetable-Glycerine-Liquid-118ml/dp/B00PUX5SF4/) (low vapor pressure for longer-running setups, alternative: water) -->

Peristaltic pumps are the workhorses of many laboratories! 🏋️‍♀️ They're used for precise liquid handling in various applications:

- 🧪 Transferring chemicals in analytical chemistry
- 🦠 Moving culture media in microbiology
- 💊 Dispensing precise volumes in pharmaceutical research
- 🌱 Delivering nutrients in hydroponics systems

Fun fact: The pump's mechanism mimics the peristalsis of your digestive system! 😮

#### Demo

✅ Read the [MDD3A DC Motor Driver datasheet](https://docs.google.com/document/d/1ax3gSo0srTzoSr2bo8ETLym0OqrYjlk_JCZK4kxtfXg/edit?usp=sharing)

Understanding the motor driver is crucial for controlling the pump effectively.

✅ Read [PWM](https://en.wikipedia.org/wiki/Pulse-width_modulation)

Pulse-Width Modulation (PWM) is a key technique for controlling motor speed in many applications.

✅ Read [Control LED brightness with PWM](https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/7)

This tutorial provides a practical introduction to PWM control.

✅ Read [machine.PWM](https://docs.micropython.org/en/latest/rp2/quickref.html#pwm-pulse-width-modulation)

Understanding the MicroPython PWM API is essential for implementing motor control.

Use your knowledge, the documentation above, and any relevant datasheets to set up the circuit to control the peristaltic pump. After setting up the hardware, run the following code on the microcontroller (ensure you select the appropriate PWM Pin):

```python
from machine import Pin, PWM
from time import sleep

pwm = PWM(Pin(0))

pwm.freq(1000)

pwm.duty_u16(16384)  # 25% duty cycle
sleep(5)  # 5 seconds

pwm.duty_u16(0)  # stop the motor
```

You should observe the peristaltic pump turn on for five seconds.

https://github.com/sparks-baird/self-driving-lab-demo/blob/main/src/public_mqtt_sdl_demo/scripts/pump.py

#### Expanded Applications 🚀

- 🍺 Craft breweries use peristaltic pumps for precise ingredient dosing
- 🏥 In hospitals, they're crucial for IV drug delivery systems
- 🌈 They're even used in fountain displays for creating colorful water shows!

### Digital Pipette

Next, you will control the linear actuator of the "Digital Pipette" to carry out precise dispensing.

#### Bill of Materials

Based on https://github.com/ac-rad/digital-pipette

- [Actuonix L16-100-63-6-R](https://www.actuonix.com/l16-100-63-6-r)
- [Raspberry Pi Pico WH](https://www.raspberrypi.com/products/raspberry-pi-pico/?variant=raspberry-pi-pico-wh)
- [Adafruit Terminal PiCowbell for Pico with Pre-Soldered Sockets](https://www.adafruit.com/product/5907)
- [1705 Pcs Machine Screw Assortment Kit, M2 M3 M4 M5](https://www.amazon.ca/Machine-Assortment-Metric-Washers-Button/dp/B0C3926M1B/). Only the following are needed:
  - M3 10 mm screws (3)
  - M3 20 mm screw (1)
  - M3 nuts (4)
- [Shunt Regulator: Fine-Adjust LV, 1.50Ω, 15W](https://www.pololu.com/product/3778)
- [6V Step-Up Voltage Regulator U3V70F6](https://www.pololu.com/product/2892)
- [5V wall adapter](https://www.digikey.ca/en/products/detail/phihong-usa/PSAA05A-050QL6-R/6560437)
- [3D printed parts](https://github.com/ac-rad/digital-pipette/tree/main/design/stl) (if you don't want to print these yourself, STL files can be uploaded to [Shapeways](https://www.shapeways.com/), [Protolabs Network](https://www.hubs.com/), [Xometry](https://www.xometry.com/), or similar. You can use white PLA filament)
- [Electrical wire (3 feet, black)](https://www.digikey.ca/en/products/detail/cnc-tech/10981-18-1-2000-001-1-TD/17799168)
- [Electrical wire (3 feet, red)](https://www.digikey.ca/en/products/detail/cnc-tech/10981-18-1-2000-004-1-TD/17799190)
- [M/F silicone jumper wires](https://www.adafruit.com/product/5837) (non-silicone also OK, only 4 required)
- [Wire cutter/stripper combo](https://www.amazon.ca/Professional-crimping-Multi-Tool-Stripper-Multi-Function/dp/B073YG65N2)
- [Mini flathead screwdriver set](https://www.amazon.ca/dp/B08QCT9NHY/) (one for regulators and a smaller one for PiCowbell terminals)

Digital pipettes are the unsung heroes of precise liquid handling! 🦸‍♀️ They're essential in:

- 🧬 Molecular biology for DNA/RNA work
- 🔬 Preparing samples for microscopy
- 🧫 Setting up PCR reactions
- 📊 Preparing standard solutions for calibration curves

Did you know? Digital pipettes can be more accurate than traditional glass pipettes, especially for very small volumes! 🎯

#### Demo

✅ Read [Digital pipette: open hardware for liquid transfer in self-driving laboratories](https://doi.org/10.1039/D3DD00115F)

✅ Read the [Digital Pipette GitHub README](https://github.com/ac-rad/digital-pipette)

Understanding the open-source implementation is crucial for customization and troubleshooting.

https://github.com/AccelerationConsortium/ac-training-lab/blob/main/src/ac_training_lab/picow/digital-pipette/scripts/digital_pipette_picow_basic.py

Run the following code on your microcontroller:

```python
import utime
from machine import PWM, Pin

# Setup PWM
pwm = PWM(Pin(0))  # Use the appropriate GPIO pin
pwm.freq(50)  # 50 Hz frequency


def set_position(pulse_ms):
    duty = int((pulse_ms / 20.0) * 65535)
    pwm.duty_u16(duty)


# Example to set the actuator to different positions
set_position(1.1)  # Almost full retraction
utime.sleep(5)
set_position(1.9)  # Almost full extension

pwm.deinit()  # Deinitialize PWM
```

#### Cool Uses 😎

- 🍫 Chocolatiers use them for adding precise amounts of flavoring
- 🎨 Artists employ them for mixing exact paint colors
- 🧁 Bakers use them for adding food coloring or extracts in precise amounts

### Stepper Motor

You will use a stepper motor driver to control the position of a stepper motor.

#### Bill of Materials
- [Tic500 Stepper Motor Driver](https://www.pololu.com/product/3134)
- [Raspberry Pi Pico WH](https://www.raspberrypi.com/products/raspberry-pi-pico/?variant=raspberry-pi-pico-wh)
- [Adafruit Terminal PiCowbell for Pico with Pre-Soldered Sockets](https://www.adafruit.com/product/5907)
- [Electrical wire (3 feet, black)](https://www.digikey.ca/en/products/detail/cnc-tech/10981-18-1-2000-001-1-TD/17799168)
- [Electrical wire (3 feet, red)](https://www.digikey.ca/en/products/detail/cnc-tech/10981-18-1-2000-004-1-TD/17799190)
- [Electrical wire (3 feet, white)](https://www.digikey.ca/en/products/detail/cnc-tech/10981-18-1-2000-002-1-TD/17799244)
- [Wire cutter/stripper combo](https://www.amazon.ca/Professional-crimping-Multi-Tool-Stripper-Multi-Function/dp/B073YG65N2)
- [NEMA 17 Stepper Motor](https://www.adafruit.com/product/324)
- [12V 3A power supply](https://www.digikey.ca/en/products/detail/xp-power/VEL36US120-US-JA/6220849) (alternative: [12V 1A version](https://www.digikey.ca/en/products/detail/xp-power/VEL12US120-US-JA/5726833))
- [Jack to bare wire](https://www.digikey.ca/en/products/detail/tensility-international-corp/10-02247/6412282)
- [Electrical wire (3 feet, white)](https://www.digikey.ca/en/products/detail/cnc-tech/10981-18-1-2000-002-1-TD/17799244)
- [Mini flathead screwdriver set](https://www.amazon.ca/dp/B08QCT9NHY/) (one for Tic500 and a smaller one for PiCowbell terminals)

Stepper motors are the precision dancers of the motor world! 💃 They're used in:

- 🖨️ 3D printers for precise nozzle movement
- 🔭 Telescope mounts for tracking celestial objects
- 🤖 Robotic arms for accurate positioning
- 📷 Camera systems for smooth panning and tilting

Cool fact: Some high-end watches use stepper motors for moving the hands! ⌚

#### Demo

✅ Read specs for [Tic500 Stepper Motor Driver](https://www.pololu.com/product/3134)

✅ Read [Example I²C code for MicroPython](https://www.pololu.com/docs/0J71/12.10)

✅ Read datasheet for [NEMA 17 Stepper Motor](https://www.adafruit.com/product/324)

✅ Browse [ticlib GitHub repo](https://github.com/jphalip/ticlib)

Run the following on your microcontroller:

```python
# Import necessary modules
from machine import I2C
from ticlib import TicI2C, MachineI2CBackend
from time import sleep

# Initialize I2C peripheral and specify the address of the Tic device
i2c = I2C(1)
address = 14

backend = MachineI2CBackend(i2c, address)
tic = TicI2C(backend)

tic.halt_and_set_position(0)
tic.energize()
tic.exit_safe_start()

target_position = 500
tic.set_target_position(target_position)

# Wait until the motor reaches the target position
while tic.get_current_position() != tic.get_target_position():
    sleep(0.1)

tic.deenergize()
tic.enter_safe_start()
```

#### Unexpected Applications 🎭

- 🎰 Slot machines use them for spinning reels
- 🎹 Electronic musical instruments for key action
- 🚗 Some car headlights use them for adaptive lighting systems

### Fan Control

Temperature control is crucial in many lab setups! 🌡️ Fan controllers help in:

- 🧊 Maintaining precise temperatures in incubators
- 💻 Cooling sensitive electronic equipment
- 🔥 Managing heat in chemical reactions
- 🌿 Controlling airflow in plant growth chambers

Did you know? Some advanced fan controllers can adjust fan speed based on multiple temperature sensors, creating temperature zones! 🌡️🌡️🌡️

#### Creative Uses 🎨

- 🍷 Wine cellars use them for maintaining optimal storage conditions
- 🎸 Musicians use them in amplifiers to prevent overheating during performances
- 🏎️ Racing simulators use them to create realistic airflow sensations!

#### Bill of Materials
- [Adafruit EMC2101 I2C PC Fan Controller and Temperature Sensor](https://www.adafruit.com/product/4808)
- [Raspberry Pi Pico WH](https://www.raspberrypi.com/products/raspberry-pi-pico/?variant=raspberry-pi-pico-wh)
- [Adafruit Terminal PiCowbell for Pico with Pre-Soldered Sockets](https://www.adafruit.com/product/5907)
- [Grove to Stemma-QT connector](https://www.digikey.ca/en/products/detail/adafruit-industries-llc/4528/11627737)
- [High-quality soldering iron](https://www.amazon.ca/dp/B077JDGY1J/) (alternative: [lower quality kit](https://www.amazon.ca/handskit-Soldering-Equipment-Adjustable-Temperature/dp/B01N46T138/))
- [Solder](https://www.amazon.ca/TOWOT-Electrical-Soldering-Content-Sn60-Pd40/dp/B09JBKSHCT/) (alternative: [1](https://www.amazon.ca/MG-Chemicals-Leaded-Solder-Diameter/dp/B004258YDE/), [2](https://www.amazon.ca/TOWOT-Electrical-Soldering-Content-Sn60-Pd40/dp/B0943TNYMS/))
- [4-pin 5V fan](https://www.canakit.com/canakit-raspberry-pi-5-fan.html) (solder joints may not be great, consider buying an extra; alternative: [Raspberry Pi Active Cooler](https://www.raspberrypi.com/products/active-cooler/), which may require disassembly, alternative: [4-pin Axial Fan 50x50x15 mm 12V Bare wire](https://www.digikey.ca/en/products/detail/delta-electronics/AUC0512DB-AF00/2560614), needs to be powered externally with a 12V power supply, adjust connections accordingly)
- [Stemma QT Quick Adapter (stemma qt female to stemma qt female)](https://www.digikey.ca/en/products/detail/sparkfun-electronics/DEV-14495/7942483)
- [STEMMA QT / Qwiic JST SH 4-pin Cable with Premium Female Sockets](https://www.adafruit.com/product/4397) (note: you can insert a header pin into two of the female quick adapter sockets to create an ad-hoc Stemma QT to half-male/half-female cable. The makeshift male headers will be used to plug into the Terminal PiCowbell. See image below)

#### Demo

```{figure} ./images/fan-control-mwe.png
:scale: 12%

Minimal working example set up of fan control.
```

```{figure} ./images/fan-wiring.png
:scale: 50%

Rough electrical diagram of control board.
```

✅ Read [Adafruit EMC2101 I2C](https://www.adafruit.com/product/4808)

✅ View [MicroPython_EMC2101](https://github.com/harrybadger/MicroPython_EMC2101)

Run the following on your microcontroller:

```python
from machine import Pin, I2C
import time
from EMC2101 import EMC2101

# Constants
PIN_I2C0_SDA = Pin(8)
PIN_I2C0_SCL = Pin(9)
I2C0_FREQ = 400_000
DESIRED_SPEED = 50  # Set desired fan speed percentage (0 to 100)

# Initialize I2C bus
i2c = I2C(0, scl=PIN_I2C0_SCL, sda=PIN_I2C0_SDA, freq=I2C0_FREQ)
print(f"I2C Bus Initialized! Devices found: {i2c.scan()}")

# Initialize fan controller
fan_controller = EMC2101(i2c)
print("Fan controller object created")

# Set fan speed
fan_controller.set_duty_cycle(DESIRED_SPEED)
actual_speed = fan_controller.get_duty_cycle()

print(f"Speed set to {DESIRED_SPEED}%")
print(f"Actual speed is {actual_speed}%")
print(f"Actual RPM is {fan_controller.get_fan_rpm()} RPM")

# Monitor fan speed
while True:
    time.sleep(5)
    print(f"Monitoring Fan:")
    print(f"  Actual speed is {fan_controller.get_duty_cycle()}%")
    print(f"  Actual RPM   is {fan_controller.get_fan_rpm()} RPM")
```

### Additional Resources
- [I2C specification](https://www.nxp.com/docs/en/user-guide/UM10204.pdf)

## 🚀 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/1393644?display=full_width
:::

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

https://q.utoronto.ca/courses/393350/assignments/1499703?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/1397938?display=full_width
:::

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

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

::::
