Close

Update [Oct 12, 11:48 PM PDT] - The (breadboard) FUFF MUX Lives!!!

A project log for F.U.F.F. Mux SAO

A Florida-flavored multiplexer for other SAOs

patrick-lloydPatrick Lloyd 10/15/2024 at 03:250 Comments

The FUFF MUX works as designed. I breadboarded each of the ICs included on the PCB, as well as three different I2C devices I had in my parts box:

Here are some fun pics of the breadboard in all its messy glory:


This is the `main.py` file I wrote, but the Git commit with library submodules is coming soon.

from machine import UART, Pin, I2C, Timer
from micropython_mcp9808 import mcp9808
from mpu9250 import MPU9250
from ak8963 import AK8963
import VL53L0X
import mcp23017
import tca9548a
import time, utime

'''
TODO:
Yaks to shave:
- [x] Proof of life from Pico by enumerating U2F drive on laptop and accepting a compiled program
- [x] Serial communication - UART0 is Bogarted by Thonny so we have to use UART1 for traditional USB-serial comms
- [x] I2C hello world - do a scan and see at least one device at the expected address
- [x] I2C Read and write MCP9808 temp sensor (at least individually)
- [x] I2C Read and write MPU9250 IMU sensor (at least individually)
- [x] I2C Read and write VL53L0X ToF sensor (at least individually). Don't forget to remove the P&P Kapton

Meat & Potatoes
- [x] I2C Read and write TCA9548A I2C Mux (at least individually)
- [x] I2C Read and write MCP23017 GPIO expander (at least individually). Consider switches 
- [x] I2C read & write through multiplexer

'''

uart1 = UART(1, baudrate = 115200, tx = Pin(4), rx = Pin(5))
i2c0 = I2C(0, scl = Pin(1), sda = Pin(0), freq = 100000)
i2cmux = tca9548a.TCA9548A(i2c0)

def test_ext_uart_mon():
    print("test_ext_uart_mon: Testing UART transmission to external serial monitor...")
    
    txData = b'hello UART, from RP2040!\n\r'
    uart1.write(txData)
    print(txData)
    
    print("test_ext_uart_mon Complete")
    print()

def test_i2c_scan_chain():
    print("test_i2c_scan_chain: Testing I2C read capabilities:")
    
    print("Devices on I2C0: ")
    print(" ".join(hex(n) for n in i2c0.scan()))
    
    print("test_i2c_scan_chain Complete")
    print()

def test_temp_sensor():
    print("test_temp_sensor: Testing MCP9808 temperature sensor writing and reading")

    temp = mcp9808.MCP9808(i2c = i2c0, address = 0x18)
    
    for tempres in mcp9808.temperature_resolution_values:
        print(f"Writing temperature resolution setting: {tempres}")
        temp.temperature_resolution = tempres
        print("Reading temperature resolution setting: ", temp.temperature_resolution)
        for sample in range(10):
            print(f"Reading temperature {sample+1}/10: {temp.temperature:.4f}°C")
            time.sleep(0.2)
                   
        print("test_temp_sensor Complete")
        print()

def test_imu():
    print("test_imu: Testing MPU9250 IMU writing and reading")
    
    dummy = MPU9250(i2c = i2c0)

    print(f"Reading MPU9250 id: {hex(dummy.whoami)}")
    print(f"Writing calibration info for magnetometer")
    
    ak8963 = AK8963(
        i2c0,
        offset=(-136.8931640625, -160.482421875, 59.02880859375),
        scale=(1.18437220840483, 0.923895823933424, 0.931707933618979)
    )
    
    imu = MPU9250(i2c = i2c0, ak8963 = ak8963)
    
    for sample in range(10):
        print(f"Reading MPU9250 Sensors {sample+1}/10")
        print(f"Accel: {imu.acceleration}")
        print(f"Gyro:  {imu.gyro}")
        print(f"Mag:   {imu.magnetic}")
        print(f"Temp: {imu.temperature}")
        time.sleep(0.1)
        
    print("test_imu Complete")
    print()

def test_tof():
    print("test_tof: Testing VL53L0X ToF Lidar writing and reading")
    
    tof = VL53L0X.VL53L0X(i2c0)
    
    print(f"Writing pulse period info for ToF")
    tof.set_Vcsel_pulse_period(tof.vcsel_period_type[0], 18)
    tof.set_Vcsel_pulse_period(tof.vcsel_period_type[1], 14)
    
    tof.start()
    tof.read()
    for sample in range(10):
        print(f"Reading ToF Sensor {sample+1}/10: {tof.read()}")
        time.sleep(0.1)
        
    tof.stop()

    print("test_tof Complete")
    print()

def test_gpio_expander():
    print("test_gpio_expander: Testing MCP23017 16-bit GPIO expander writing and reading")
    
    gpio_exp = mcp23017.MCP23017(i2c0, 0x20)
    
    print("Writing config values")
    gpio_exp.config(interrupt_polarity=1, sequential_operation=0, interrupt_mirror=1)
    # A7 is connected to my test switch
    gpio_exp.pin(7, mode=1, pullup=True)
    for sample in range(5):
        print(f"Reading switch {sample+1}/5: {gpio_exp.pin(7)}")
        time.sleep(1)
    
    print("test_gpio_expander Complete")
    print()

while(True):
    print("Starting FUFF Tests")

    #test_ext_uart_mon()
    
    # disable all 8 channels
    i2c0.writeto(0x70, b'\x00')
    test_i2c_scan_chain()
    
    # enable channel 0 (SD0,SC0)
    i2c0.writeto(0x70, b'\x01')
    test_i2c_scan_chain()
    test_temp_sensor()
    
    #enable channel 1
    i2c0.writeto(0x70, b'\x02')
    test_i2c_scan_chain()
    test_imu()
    
    # enable channel 2
    i2c0.writeto(0x70, b'\x04')
    test_i2c_scan_chain()
    test_tof()

    # enable channel 3
    i2c0.writeto(0x70, b'\x08')
    test_i2c_scan_chain()
    test_gpio_expander()

    print("FUFF Tests Complete. Restarting in 10s")
    time.sleep(10)

Discussions