coding issue with arduino

Hi guys!

I have an idea which after spending the better part of today, i have finally given up and hence this post.

the idea apparently is pretty straight forward:

i want to sense temperature and convert it into voltage.
so like for a temp i/p of 1000 the op should be 0v and for 1500 it should be full scale or 5v.

you dont need to worry about the temp senor or ADC as that has already been taken care of!

so basically i need a code which when presented with nos between 1000-1500 produces 0-5 voltage respectively.(and proportionally in between that range!)

i am using a 12 bit DAC MCP4725 as i have it handy.

i guess there has to be a formula for this or maybe some other code, i dont know.

any inputs will be appreciated.

cheers!
Marrc

Hi,
Is this associated with this?

What have you got in code now?
Please post your code using code tags.

Thanks.. Tom... :slight_smile:

i want to sense temperature and convert it into voltage.
so like for a temp i/p of 1000 the op should be 0v and for 1500 it should be full scale or 5v.

it's interesting the you have a sensor that directly reports temperature.

voltage = 5 * (s - 1000) / 500 where s is 1000-1500

gcjr:
it’s interesting the you have a sensor that directly reports temperature.

voltage = 5 * (s - 1000) / 500 where s is 1000-1500

u were spot on!
i have tried it and it works fine.
but when i connect DAC to some other pins say A6 and A7 it stops working. i checked the data sheet of MCP4725 but it dosent say specifically about which pins of arduino the DAC has to be interfaced.

so why only A4 and A5?

my code is as follows which i got from herehttps://circuitdigest.com/microcontroller-projects/arduino-dac-tutorial-interfacing-mcp4725-dac:

# include<Wire.h>
# define MCP4725 0x61  //MCP4725 address as 0x61  , 

unsigned int c,d,e,f;
byte buffer[3];
int A0state,A3state,range;

void setup() {
  // put your setup code here, to run once:
Wire.begin();   
Serial.begin(9600);
pinMode (A0,INPUT);
pinMode (A1, INPUT);
pinMode (A3, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

 c=8.192*(d-1000);  // d is temp reading, min 1000 max 1500
 getVoltage(c);
} 
  void getVoltage ( unsigned int n)
  {
    Serial.println (n);
  buffer[0]= 0b01000000;
  buffer[1]= n>>4;  // sending msb values
  buffer[2]= n<<4;  // sending lsb values
  Wire.beginTransmission(MCP4725);         //Joins I2C bus with MCP4725 with 0x61 address
  
  Wire.write(buffer[0]);            //Sends the control byte to I2C 
  Wire.write(buffer[1]);            //Sends the MSB to I2C 
  Wire.write(buffer[2]);            //Sends the LSB to I2C
  
  Wire.endTransmission();           //Ends the transmission
  delay(400);
  }

value of d has to be entered manually
i am a newbie so any inputs will be appreciated!

cheers!
Marrc

marrc:
so why only A4 and A5?

Because the DAC runs on I2C and they are the two I2C pins (SDA/SCL) on a Uno. Other Arduinos use different pins.

Steve

You should use map() function. Example:

analogWrite(map(temperature, 1000, 1500, 0, 255));

It will work as you want :slight_smile: