Conversion of units

Hi all,

I am reading an ADC value 0-5V from a potentiometer. It is printing 0 to 5000mV on the serial monitor fine as per code below. I now want the reading to show 0.00 to 5.00V with all the values inbetween. I've tried dividing by 1000 and using a float but it just prints 0.00, 1.00, 2.00, etc in whole numbers only. Any help is appreciated!

/*!
   @file readVoltage.ino
   @brief connect ADS1115 I2C interface with your board (please reference board compatibility)
   @n The voltage value read by A0 A1 A2 A3 is printed through the serial port.

   @copyright   Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
   @license     The MIT License (MIT)
   @author [luoyufeng](yufeng.luo@dfrobot.com),
   @version  V1.0
   @date  2019-06-19
   @url https://github.com/DFRobot/DFRobot_ADS1115

  Works perfectly!
  20.09.23

*/
#include <LCD_ST7032.h>// MIDAS MCCOG21605C6
LCD_ST7032 lcd;

#include <Wire.h>
#include <DFRobot_ADS1115.h>

DFRobot_ADS1115 ads(&Wire);

void setup(void)
{
  lcd.begin();
  lcd.setcontrast(24); //contrast value range is 0-63, try 25@5V or 50@3.3V as a starting value

  Serial.begin(115200);
  ads.setAddr_ADS1115(ADS1115_IIC_ADDRESS1);   // 0x48
  ads.setGain(eGAIN_TWOTHIRDS);   // 2/3x gain
  ads.setMode(eMODE_SINGLE);       // single-shot mode
  ads.setRate(eRATE_128);          // 128SPS (default)
  ads.setOSMode(eOSMODE_SINGLE);   // Set to start a single-conversion
  ads.init();
}

void loop(void)
{
  if (ads.checkADS1115())
  {
    int16_t adc0, adc1, adc2, adc3;
    adc0 = ads.readVoltage(0);

    Serial.print("A0:");
    Serial.print(adc0);
    Serial.print("V,  ");

    lcd.setCursor(0, 0);
    lcd.print("1:");
    lcd.print(adc0);
    lcd.print("mV  ");

  }
  else
  {
    Serial.println("ADS1115 Disconnected!");
  }

  delay(1000);
}

Please post the sketch where you tried this

I tried using a float below but it rounds the number to 0.00, 1.00, 2.00, etc.

#include <DFRobot_ADS1115.h>

DFRobot_ADS1115 ads(&Wire);

float mv_to_voltage;

void setup(void)
{
  lcd.begin();
  lcd.setcontrast(24); //contrast value range is 0-63, try 25@5V or 50@3.3V as a starting value

  Serial.begin(115200);
  ads.setAddr_ADS1115(ADS1115_IIC_ADDRESS1);   // 0x48
  ads.setGain(eGAIN_TWOTHIRDS);   // 2/3x gain
  ads.setMode(eMODE_SINGLE);       // single-shot mode
  ads.setRate(eRATE_128);          // 128SPS (default)
  ads.setOSMode(eOSMODE_SINGLE);   // Set to start a single-conversion
  ads.init();
}

void loop(void)
{
  if (ads.checkADS1115())
  {
    int16_t adc0, adc1, adc2, adc3;
    adc0 = ads.readVoltage(0);

    Serial.print("A0:");
    Serial.print(adc0);
    Serial.print("V,  ");

    mv_to_voltage = (adc0 / 1000);

    lcd.setCursor(0, 0);
    lcd.print("1:");
    lcd.print(mv_to_voltage);
    lcd.print("V  ");

    adc1 = ads.readVoltage(1);
    Serial.print("A1:");
    Serial.print(mv_to_voltage);
    Serial.print("V  ");

  }
  else
  {
    Serial.println("ADS1115 Disconnected!");
  }

  delay(1000);
}

This is an integer divide. You can force the divide to be floating point in various ways, for example:

    mv_to_voltage = (adc0 / 1000.0);

or

    mv_to_voltage = (float)adc0 / 1000;

Genius! Thanks,

I forgot to add the point zero!

Do you know why you added the ‘point zero’ ?
.

As it's being treated as an integer without it I guess.

1 Like

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