"""P9-5 Generate a sine wave using MCP4725 on I2C Bus""" import math from machine import Pin, I2C i2c = I2C(1, scl = Pin(27), sda = Pin(26), freq = 400_000) DEV_ADDR = 0x60 WAVEFORM_LENGTH = 360 # construct a data table for a sine wave RADIAN = ((2 * math.pi) / WAVEFORM_LENGTH) sinewave = [] for i in range(0, WAVEFORM_LENGTH): d = int(2047 * (math.sin(RADIAN * i) + 1)) sinewave.append(d) while True: for d in sinewave: # Create a bytearray of DAC command, high byte (D11-D4), low byte (D3-D0) ba = bytearray([0x40, (d >> 4) & 0xFF, (d << 4) & 0xF0]) # Write the bytearray to MCP4725 DAC through I2C i2c.writeto(DEV_ADDR, ba)