The main problem seems to be, that you are not adhering to the MCP's communication protocol. You just try to directly and only send the pure analog measurement to it.
Look at this image from page 18 of the MCP4725's datasheet:
It describes the operation is Fast Mode, which seems to be the correct mode for this. (Similar descriptions for the other modes can be found below that image). The first byte is done by the SoftWire library, but the following is your responsibility. The
next 2 bytes consist (in this order) of 2 bit's setting the Fast Mode, 2 bit's for the devices power down select (both zero for normal DAC operation) and then the 12 bits of the actual voltage value for the DAC.
At this point I will show another problem with your code. You read an analog voltage with the Arduinos ADC, which is a 10 bit number (of type
int) and use it in
But the parameter for
Wire.write() is defined as
uint8_t (see line92 in SoftWire.h of the Softwire library), which means, that your
int value get's casted and you loose all bits above 255 (the lower byte), because
uint8_t just cannot store more.
What you have to do: You should put the I2C data bytes together in your code and then send them. This may look like this:
byte byte1 = (aPin & 0x0F00) >> 8;
byte byte2 = (aPin & 0xFF);
This code uses bitwise operators. The
& will do an AND operation, the
>> shifts the bits to the right by the amount after the operator. The
0x denotes a value in hex representation (binary representation would be
0b). So for the first byte we are doing an AND with the value
0x0F00, which will give us a value with only the first 4 bits of the second byte of
aPin. Then we shift the result 8 bits to the right, so that the remaining data is nicely aligned to the right of your byte. We don't need to include the mode bits and power down select bits here, since they are zero anyway. If you want to set them non-zero you could bitwise OR them (of course with the correct shift, so that the bits have the correct position in the resulting byte):
byte mode_bits = 0b10;
byte power_down_bits = 0b11;
byte byte1 = (mode_bits << 6) | (power_down_bits << 4) | (aPin & 0x0F00) >> 8;
Then you can send these bytes out with I2C:
That should work, though note, that I have not tested this. I just read the datasheet, which is always a must read.
What happened with your original code: The datasheet states for the command "Write DAC Register" (which uses 4 bytes including the address byte) the following:
The device ignores any partially received data bytes if the I2C
communication with the Master ends before completing the 4th byte.
So it just drops any I2C messages, that are not complete. Though it is not stated clearly, we can be pretty sure, that the same happens also for the other operation modes. You were sending only 1 byte, so the MCP found the message incomplete and ignored it.
That said, there is a principle to work around the limit of only 2 devices on the same bus, which I also only learned recently about. You might not wanna go this way here, but it's a great idea and a valid solution to the problem with the limit.
Every MCP has 1 address pin A0, which set's 1 bit of the address, resulting in 2 possible addresses. On an I2C bus there must not be more than 1 device with the same address. But if that doubled address is never called, the bus doesn't have a problem.
You would connect the A0 pins of each MCP to one digital output pin of the Arduino each. By setting the state of these output pins you can change the address of the MCPs (requirement is, that the MCP actually checks the A0 pin during operation, but the datasheet seems to suggest this). Now you keep every A0 pin on one level, so that all MCPs have the same address. To communicate with one, you would change the state of it's A0 pin to change it's address. Now this MCP is the only MCP with this address (all other MCPs have the other address). You can now communicate with it. When you want to communicate with another MCP, you reset the first MCPs A0 pin and then change the next MCPs A0 pin.
That way you make sure, that on one of the two addresses, there will always be 1 MCP at maximum, while you never use the other address. You can use as many MCPs, as you have digital output pins to change their addresses.