Analog Inputs - is writing before read allowed?

I connect some PT100 transmitters (output 0-5V) to the Arduino analog inputs.
If I disconnect them, the Value of the inputs are not 0, so I can’t check if they are not connected.
Now to my question.
Is it allowed to analogWrite directly before i analogRead , even if there is a voltage on the pin?

for example:

analogWrite(temperature_1_pin, 0);
temperature_1_value = analogRead(temperature_1_pin);
analogWrite(temperature_2_pin, 0);
temperature_2_value = analogRead(temperature_2_pin);

I tried it and it works, but i don’t know if the Arduino will be damaged soon.

Complete sketch (be careful, it’s in German :slight_smile: )

[color=blue]
// Bibliotheken:
#include <LiquidCrystal.h> //LCD Display Ansteuerungsbefehle

//Allgemeine Variablen
int i=0;       // Loop-Zähler
int i_max=20;  //Festlegung, in welchem Loop die Mittelwerte gebildet werden.
int j=0;       // Hilfsvariable zum Zeilen Überschreiben.

//Displaykonfiguration:
int zeilen=4;        //LCD Zeilen
int spalten=20;     //Spalten bzw. Zeichen je Zeile
int rs=12;          //PWM Anschluss des RS Displayanschlusses 
int e=11;           //PWM Anschluss des E (Enable) Displayanschlusses 
int d4=5;           //PWM Anschluss des D4 Displayanschlusses 
int d5=4;           //PWM Anschluss des D5 Displayanschlusses 
int d6=3;           //PWM Anschluss des D6 Displayanschlusses 
int d7=2;           //PWM Anschluss des D7 Displayanschlusses 

// Initialisiert die Analogeingänge
// Temperatursensor 1
const int temperatur_1_pin = A0;       // Zuweisung des Analogeingangs
int temperatur_1_wert = 0;             // Speicher für den 10 Bit-Wert
int temperatur_1 = -9999;              // Speicher für den Durchschnittswert in °C
bool pt100_1_connected = false;        // Merker, ob Sensor angeschlossen

 
// Temperatursensor 2
const int temperatur_2_pin = A1;    // Zuweisung des Analogeingangs
int temperatur_2_wert = 0;          // Speicher für den 10 Bit-Wert
int temperatur_2 = -9999;           // Speicher für den Durchschnittswert in °C
bool pt100_2_connected = false;     // Merker, ob Sensor angeschlossen




// Initialisiert das Display und übergibt die Anschlüsse
LiquidCrystal lcd(rs, e, d4, d5, d6, d7);



 
void setup() {
  // Initialisiert die Displaygröße
  lcd.begin(spalten, zeilen);
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  // Grundgestell Anzeige
  if (i==0)
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Temperatur 1:");
    lcd.setCursor(0, 2);
    lcd.print("Temperatur 2:");
  }
 
//Einlesen der Eingänge
analogWrite(temperatur_1_pin, 0); //Zieht den Eingang kurz vor der Messung auf 0
temperatur_1_wert += analogRead(temperatur_1_pin);
analogWrite(temperatur_2_pin, 0); //Zieht den Eingang kurz vor der Messung auf 0
temperatur_2_wert += analogRead(temperatur_2_pin);


//Für Programmstart
if (temperatur_1 < -9000 || temperatur_2 < -9000)
{
temperatur_1_wert = temperatur_1_wert * i_max;
temperatur_2_wert = temperatur_2_wert * i_max;
i = i_max;
}


 //Mittelwertbidlung und Ausgabe
if (i >=i_max)
 {
   temperatur_1_wert = temperatur_1_wert / i_max;
   temperatur_1 = map(temperatur_1_wert, 0, 1023, 0, 400);
   temperatur_1_wert = 0;
   ClearLine(1);
     if (temperatur_1 < 400 && temperatur_1 > 0)pt100_1_connected = true;
     else pt100_1_connected = false;
       if (pt100_1_connected == true)
       {
        lcd.setCursor(0, 1);
        lcd.print (temperatur_1);
        lcd.setCursor(3, 1);
        lcd.write(0xDF); //° Zeichen
        lcd.setCursor(4, 1);
        lcd.print ("C");
       }
       if (pt100_1_connected == false)
       {
        lcd.setCursor(0, 1);
        lcd.print ("Nicht angeschlossen");  
       }

   temperatur_2_wert = temperatur_2_wert / i_max;
   temperatur_2 = map(temperatur_2_wert, 0, 1023, 0, 400);
   temperatur_2_wert = 0;
   ClearLine(3);
     if (temperatur_2 < 400 && temperatur_2 > 0)pt100_2_connected = true;
     else pt100_2_connected = false;
       if (pt100_2_connected == true)
       {
        lcd.setCursor(0, 3);
        lcd.print (temperatur_2);
        lcd.setCursor(3, 3);
        lcd.write(0xDF); //° Zeichen
        lcd.setCursor(4, 3);
        lcd.print ("C");
       }
       if (pt100_2_connected == false)
       {
        lcd.setCursor(0, 3);
        lcd.print ("Nicht angeschlossen");  
       }
  i=0;
 }
i++;
delay(200);
}

//Löscht eine Zeile auf dem Display
int ClearLine(int zeile)
{
    for (j=0; j<spalten; j++)
    {
        lcd.setCursor(j, zeile);
        lcd.print(" ");
    }
  
  return 0;
}
[/color]

Is it allowed to analogWrite directly before i analogRead ,

Absolutely. But you're talking about two completely different sets of pins, so I don't see the point of the question .

Please, please use code tags.

There are no pins which support both analogWrite and analogRead.

Perhaps what you need is a pull down resistor so that when the wire is disconnected you can rely on the pin to read zero.

analogWrite works only on PWM pins. It doesn't generate an analog voltage (except on two pins on the Arduino Due)

analogRead works only on analog input pins. You normally connect the output of an analog sensor to an analog input pin.

Thanks for the fast replys.

But i tried it.

analogWrite(temperature_1_pin, 0);
temperature_1_value = analogRead(temperature_1_pin);

If i disconnect analog pin A0 (temperature_1_pin) with this code, i got my warning.

If i use this code

//[analogWrite(temperature_1_pin, 0);
temperature_1_value = analogRead(temperature_1_pin);

and disconnect analog pin A0 my temperature goes up to something between 37 and 63 °C. Also i don't get my disconnect warning.

analogWrite(temperature_1_pin, 0);
temperature_1_value = analogRead(temperature_1_pin);

It makes ABSOLUTELY no sense doing an analogWrite to pin A0. All it does is a digitalWrite.

If I disconnect them, the Value of the inputs are not 0, so I can't check if they are not connected.

How about a pull-down resistor?

Ok.

This is with analogWrite(temperature_1_pin, 0)

|500x439 Error message works.

And this without

|500x437 Error message works not and temperature is increased and fluctuates.

So you can see, i don't lie and it has a function. I think the best way is to let the Arduino run some hours. If it isn't dameged and works up to tommorow, i'll let you know.

So you can see, i don't lie and it has a function. I think the best way is to let the Arduino run some hours. If it isn't dameged and works up to tommorow, i'll let you know.

Try READING THE DOCUMENTATION. analogWrite does NOT work on the analog input pins

Mark

Well,yes it does, but only in the same way that analogWrite works on digital pins.

Reference:

On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 - 13 and 44 - 46. Older Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.

and

The analogWrite function has nothing to do with the analog pins or the analogRead function.

Have you tried an analogWrite (pin,0) or a an analogWrite (pin, 255) on either a digital pin (that doesn't support PWM) or an analog input pin?

in the same way that analogWrite works on digital pins.

on a pin that is not capable of PWM, analogWrite() will write a digital 1 if the value is >127, and a digital 0 if the value is <= 127. So the “analogWrite(pin, 0)” that the OP is using works “correctly” on any pin, and does exactly why they want it to do. (perhaps needing a pinMode() in there somewhere.) (digitalWrite(pin,0) would have done exactly the same thing, and been a more accurate description of what happens.)

Writing a 0 to a pin that IS connected to an analog source may not be a good idea - it will essentially short the analog source to ground, which may result in currents that are not healthy for either the analog source or the AVR…

Thanks for all the comments. And i think westfw is right, the short curcuit is the Problem. I'll think over and change that.

Hi,

PT100 transmitters (output 0-5V)

These transmitters, are they programmable, most have a user definable fault output level, if they have a fault. Usually 0V or 5V, in other words values that you would not get in normal operation.

Then you could set your pull resistor as an Up or Down.

Tom... :)