### Controlling the input voltage
import time
from smbus2 import SMBus, i2c_msg
# Constants for the MCP4725
MCP4725_ADDR = 0x60 # Default I2C address for MCP4725, might vary
MCP4725_WRITE_DAC = 0x40
# Adjust as needed based on your OPA548 setup
V_MAX = 12.0
V_REF = 3.3 # Voltage reference for MCP4725, connected to 3.3V in this example
# Calculate the value to send to the MCP4725
def voltage_to_dac_value(voltage):
return int((voltage / V_REF) * 4095)
def set_voltage(bus, voltage):
value = voltage_to_dac_value(voltage)
# Break the 12 - bit value into 2 bytes.
byte1 = (value >> 4) & 0xFF
byte2 = (value & 0x0F) << 4
# Write the value to the DAC.
msg = i2c_msg.write(MCP4725_ADDR, [MCP4725_WRITE_DAC, byte1, byte2])
bus.i2c_rdwr(msg)
def main():
voltage = float(input("Enter desired voltage (0-12V): "))
if 0 <= voltage <= V_MAX:
with SMBus(1) as bus: # Change '1' to '0' on older RPi versions
cmatsu5