MCP4725 I2C addressing question

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
sgt_willy
 
Posts: 5
Joined: Thu Aug 17, 2017 2:26 pm

MCP4725 I2C addressing question

Post by sgt_willy »

Hello, I am trying to send values to two different MCP4725's from my arduino mega. I must have something wired incorrectly or the programming is incorrect. Because if I change the address in the dac.begin() function both MCP's output that corresponding voltage. I cannot seem to find a good wiring example or arduino code that can help me. Any ideas?

Here is my code:

Code: Select all

#include <Wire.h>
#include <Adafruit_MCP4725.h>
Adafruit_MCP4725 dac; // creates DAC object
Adafruit_MCP4725 dac_1; //creates DAC_1 object



int led = 3; // led pinOut assignment
int ledOn = 255; // assigns led brightness to the variable
int ledOff = 0; // assigns led brightness to the variable
const byte numChars = 18; //check sum variable to prevent corrupt packets
char receivedChars[numChars]; // array to hold string variable
char tempChars[numChars]; //temporary array for parsing
//variables to hold new data
char messageFromPC[numChars]= {0};
int DeviceID =0; //initialize variable
int SensorValueFromPC = 0; //initialize variable
boolean newData = false; //initialize variable
int ConvertedValue = 0; //initialize variable



void setup(){
  Serial.begin(9600);
   dac.begin(0x62);
   dac_1.begin(0x63);
  pinMode(led, OUTPUT);
    
}

void loop(){
recvWithStartEndMarkers(); 
 if (newData == true){
  strcpy(tempChars, receivedChars); //this temp copy is necessary to protect original data becuase strtok() used in parsedata() replaces commas with \0
  parseData();
  showParsedData();
  newData = false;
 }


  
}

void recvWithStartEndMarkers(){
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<'; //Identifys beginning of Package
  char endMarker = '>';   //Identifys end of Package
  char rc;

  while (Serial.available() > 0 && newData == false) {

      rc = Serial.read();

      if (recvInProgress == true) {
          if (rc != endMarker){
              receivedChars[ndx] = rc;
              ndx++;
              if (ndx >=numChars){
                  ndx = numChars -1;
                                }
          }
          else{
            receivedChars[ndx] = '\0'; //terminate the string
            recvInProgress = false;
            ndx = 0;
            newData = true;
          }
      }
      else if(rc == startMarker){
        recvInProgress = true;
      }
  }
}
void parseData(){ //splits the packaged data into parts
    char * strtokIndx; //this is used by strtok() as a index
    strtokIndx = strtok(tempChars,","); //get first part- the string
    strcpy(messageFromPC, strtokIndx); //copys the string to messageFromPC
    strtokIndx = strtok(NULL,","); //this continues where the previous call left
    DeviceID = atoi(strtokIndx); //converts this part to integer
    strtokIndx = strtok(NULL,",");
    SensorValueFromPC = atoi(strtokIndx); // converts this part to a integer
    ConvertedValue = map(SensorValueFromPC, 190, 1023, 0, 4095);
    }
void showParsedData(){  //This Function will send the converted value to the appropriate DAC.
  if (DeviceID == 1){
      dac.setVoltage(ConvertedValue, false); //send to DAC 
     analogWrite(led, ledOn); // Turns on LED
  }
  else if (DeviceID == 2){
    dac_1.setVoltage(ConvertedValue, false); //sends to DAC_1
    analogWrite(led, ledOff); // Turns of LED
  }
  
  
  
}


User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: MCP4725 I2C addressing question

Post by adafruit_support_bill »

Please pose a photo showing how you have it wired.

User avatar
sgt_willy
 
Posts: 5
Joined: Thu Aug 17, 2017 2:26 pm

Re: MCP4725 I2C addressing question

Post by sgt_willy »

Ok, I apologize in advance for the wire mess. You will notice that I only have one MCP4725. I am waiting for the other in the mail. I am just trying to change the address of this one so that I know the bus address works. I thought the directions said that if I jumped AO to VCC it would change the address to 0x63.
[img]

[/img]
Attachments
Adafruit_board.jpg
Adafruit_board.jpg (927.58 KiB) Viewed 353 times

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: MCP4725 I2C addressing question

Post by adafruit_support_bill »

You have a few solder joints that need some rework. They have not flowed onto the solder pad, so the connectivity will be marginal.

This guide has some tips on identifying and repairing common soldering problems:
http://learn.adafruit.com/adafruit-guid ... n-problems

User avatar
sgt_willy
 
Posts: 5
Joined: Thu Aug 17, 2017 2:26 pm

Re: MCP4725 I2C addressing question

Post by sgt_willy »

OK I will rework the soldering. Is my wiring and code correct?

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: MCP4725 I2C addressing question

Post by adafruit_support_bill »

The wiring looks OK. I can't quite see where the SDA & SCL plug into the Arduino, but if you are getting some response, then it is probably correct.

User avatar
sgt_willy
 
Posts: 5
Joined: Thu Aug 17, 2017 2:26 pm

Re: MCP4725 I2C addressing question

Post by sgt_willy »

I can change the voltage with the code 0-4095 to 1- 5 volt output. I just do not know about the addressing.

User avatar
sgt_willy
 
Posts: 5
Joined: Thu Aug 17, 2017 2:26 pm

Re: MCP4725 I2C addressing question

Post by sgt_willy »

I figured it out. - I think- I did not know that the addr chip held the last value it received. So when I was switching addresses I thought the output voltage would go back to zero on the address that was not in service.

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: MCP4725 I2C addressing question

Post by adafruit_support_bill »

Yes, the DAC will keep the output at that level until it receives another command to change it.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”