can i add voltage to analog input?

Is it possible to add voltage to my analog inputs?

#include "U8glib.h"

U8GLIB_PCD8544 u8g(13, 11, 10, 9, 8); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, Reset = 8

float val1,val2,volt = 0.0,sensordata;

void setup() {
  u8g.setColorIndex(1); // pixel on
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  Serial.begin(9600);

}

void loop()
{
    u8g.firstPage();
    do
    {
      //float Time;               // Read and store Sensor  data
      val1 = analogRead(A0);   //from sensor 1
      val2 = analogRead(A1);   //from sensor
      val1 = val1  * (5.0 / 1023.0);
      val2 = val2  * (5.0 / 1023.0);
      val1 = val1  + 4;
      val2 = val2  + 2;
      volt = val1 - val2;            
      //volt = volt * (5.0 / 1023.0); //equation to convert incoming value to voltage
      //sensordata = volt;
      Serial.println(volt, 4);
      u8g.setFont(u8g_font_unifont);
      //u8g.drawStr(5, 22, String(volt));
      u8g.setPrintPos(5, 22); 
      // call procedure from base class, http://arduino.cc/en/Serial/Print
      u8g.print("volt: "+String(volt,4));  
    }
    while(u8g.nextPage());
    delay(1000);
} // END void loop...

Can you explain why you want to “add voltage” to the input?

Note: when posting code, use code tags ("</>" button) instead of quotes.

jremington:
Can you explain why you want to “add voltage” to the input?

Note: when posting code, use code tags ("</>" button) instead of quotes.

voltage from reference sensor is 0.5(fixed) and another sensor voltage is 1.7(variable). i want to take differential of both voltages. Instead of making gain using operational amplifier is it possible to simply add voltages?

val1 = val1  + 4;
      val2 = val2  + 2;

Sure, you can do that. Of course, you’re not actually adding voltage. You are adding to variables val1 & val2 after reading & calculating the voltage.

voltage from reference sensor is 0.5(fixed) and another sensor voltage is 1.7(variable). i want to take differential of both voltages.

In that case, just subtract in software.

You can either subtract the raw values and then apply the 5/1023 voltage factor, or you can convert to voltage and then subtract.

i want to take differential of both voltages.

Why?

jremington: Why?

subtracting reference voltage to sensor voltage

val1 = analogRead(A0); //from reference val2 = analogRead(A1); //from sensor val1 = val1 + 4; val2 = val2 + 2; val1 = val1 * (5.0 / 1023.0); val2 = val2 * (5.0 / 1023.0); volt = val1 - val2;

If you have your own reference voltage (0-5 volts), connect it to the AREF pin and calll "analogReference(EXTERNAL);" in setup(). This will set the analog input range of 1023 max counts equal to the external reference, no scaling required.

https://www.arduino.cc/en/Reference/AnalogReference

avr_fred: If you have your own reference voltage (0-5 volts), connect it to the AREF pin and calll "analogReference(EXTERNAL);" in setup(). This will set the analog input range of 1023 max counts equal to the external reference, no scaling required.

https://www.arduino.cc/en/Reference/AnalogReference

good one i will try but my reference voltage is about 0.5 volt if i add 4 then A0 value will be 4.5 (fixed)volt. output from another sensor is 1.5 volt if i add 2 the A1 value will be 3.2 volt(varies). so i will get linear response instead of getting negative values.

v1 = analogRead(A0) * (5.0 / 1023.0);   // The 0.5V reference voltage.
v2 = analogRead(A1) * (5.0 / 1023.0);  // the about 1.7 volt variable voltage.
vdiff = v2-v1; // The difference in voltage between the two sensors.

This does exactly what you say you want in #2: the difference in voltage on the two analog lines. This will always be positive as long as your v2 remains within 0.5-5.0V.

So if your reference v1 = 0.5V, and variable v2 = 1.7V, vdiff = (1.7-0.5) = 1.2V.

If you really want to directly measure the difference between the two voltages, get an ADS1115 and set it to differential mode.

akmsr:
i will get linear response instead of getting negative values.

These two are unrelated.
Negative values can be part of a linear response, and positive values can be part of a non-linear response.

It should be (5.0 / 102*4*.0);

Why have so many people problems understanding that?

The refence voltage is very rarely exactly 5.0V, often (USB) not even close.

Hi, The maxcount from the AtoD is 1111111111 = 1023

(5.0 / 1023)* 1023 = 5.00

(5.0 / 1024) * 1023 = 4.99 If you want to get picky.

Each to his own..

Tom... :)