Mapping a float

I'm working on a project where I need to map an incoming reading to an actual reading which is a float variable.
I have a power supply where I want to see the set voltage compared to the actual voltage been displayed. The reading are been taken from an MCP4822 12bit dac and been read by 16bit adc along with the input voltage coming in from the power supply.
But the trouble I'm having is that the set voltage does not match the actual voltage.

The incoming voltage reading from Dac is 1.869 and the actual voltage from the power supply is 18.43 volts and using the map function that I've seen on the map reference site but the set reading is showing 19.31 not sure if this is the correct way or miss understood what I have read.
I thought that it would be closer to 18.41 than 19.31 ?

This is my simple test code where I'm just checking and comparing by input the actual dac reading coming in.

#include <LiquidCrystal_I2C.h> // I2C LCD display only for intial setup
#include  <Wire.h> //Wire libary 
int DAC_input = A0;    // select the input pin for the potentiometer

//#########################
//# CHANNEL ONE VARIABLES #
//#########################
float volts_Set_disp_Ch1;
float Set_volts_temp = 0;
float Set_volts_1_in_min = 0.805; //This what the incoming min set voltage from Dac reading with 16bit ADC
float Set_volts_1_in_max = 2.464;  //This what the incoming max set voltage from Dac reading with 16bit ADC
float Set_volts_1_out_min = 0.00; //start of near 0
float Set_volts_1_out_max = 30.05; //Actual maxium voltage output
//####################################################
//#  Specifying the CE and CS pins & LCD Pins        #
//####################################################

LiquidCrystal_I2C lcd(0x03f, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

void setup() {

  Serial.begin(9600);
  // lcd.setBacklight(HIGH);//debuging only used for I2C LCD
  lcd.begin(16, 2); // General LCD 2 x 8 with backlight
  lcd.clear();
} 
//####################################################
//#  Main Lop starts here                            #
//####################################################
void loop() {
  Set_volts_temp = 1.869;  // This is the actual voltage coming in from dac,
  //Map float the actual voltage reading on display is 18.41 and with 1.869 coming in form dac teh set voltage reading is 19.31
  volts_Set_disp_Ch1 = (Set_volts_temp - Set_volts_1_in_min) * (Set_volts_1_out_max - Set_volts_1_out_min) / (Set_volts_1_in_max - Set_volts_1_in_min ) + Set_volts_1_out_min;
  lcd.setCursor(0, 0);
  lcd.print(Set_volts_temp , 3); // 2 dp's
  lcd.print(" ");

  if ( volts_Set_disp_Ch1  >= 9.99) {
    lcd.setCursor(0, 1);
    lcd.print(volts_Set_disp_Ch1 , 2); // 2 dp's
    lcd.print(" ");
  } else {
    lcd.setCursor(0, 1);
    lcd.print(volts_Set_disp_Ch1 , 3); // 3 dp's
    lcd.print(" ");
  }

}

Not enough information. Surely you have a voltage divider somewhere, so please post a complete, hand drawn schematic or wiring diagram, with parts and pins labeled.

It sounds like you simply need to calibrate your voltmeter. Spend some time with this excellent tutorial on calibrating sensors. Why Calibrate? | Calibrating Sensors | Adafruit Learning System

The actual output voltage from the power supply is 18.41Volts, My fluke meter 175 shows 18.41 volts and my code is working has it should and that displays 18.42 volts which is near enough for me. The output voltage from the Dac goes from 0.850 volts to 4.096 but limited to 3.78V but because the MCP3424(16bit A/d converter) can only read upto 2.048V I've used a resistor divider to reduce it to half.

I've also tested the voltage coming out the DAC with my meter and that also matches what is been displayed on the lcd.
In the code above I've tried to set the set_min_in min&max to half this figure.

I 'm reading the results off the meters then I'm just using an uno with lcd where I input Set_volts_temp manually then upload to compare the to try and understand why I can't get the correct results, This is just a quicker way to getting it to work rather that get lost and alter master code and lose myself anf try and remaber what where I've changed things
I just wanted to add a show a set voltage feature. I've also made the formula up in excel and I get the incorrect reading in that compered to the arduino version with the exact same figures.
This is my drawing

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.