Cant get 2 analog input readouts correctly

Hi friends,
I have a problem with getting voltge readout from analog pins in Arduino Uno. I have two TCST1103 sensors connected to those inputs (A3 and A4). Their behaviour is simple and can be checked by measuring a voltage on 100 ohm grounded resistor. Of course that GND is connected to arduino's GND. There are two levels of voltage 0 (sensor covered) and 2.2 V (sensor not covered) that are taken into account, however states between them will be also in my intrest in further work (so i need to use analog signal). Now getting to point... covering first sensor (voltage on A3 = 0V) induces A4 readout to be also 0V even though it is not covered! On the other hand covering second on induces A3 = 2.2V even though it is covered... Multimeter measurement gives me proper voltages (so they differs from one readed by arduino). I have tried double readout separated by delay and i have also changed DC supply to not to use USB 5V to power the sensors. Non of the above worked. Please help

Here is the code:

// INITIALIZATION OF SENSORS 
void setup() {
 
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  
  Serial.setTimeout(10000);
  pinMode(A3, INPUT_PULLUP); // sensor 0
  pinMode(A4, INPUT_PULLUP); // sensor 1
}

// OPTICAL END-LIMITER CODE
void braker_state_check(int which_sensor){
  int breaker_sensorValue = 0;

  if(which_sensor==1){
    breaker_sensorValue = analogRead(A3);
  }
  else{
    breaker_sensorValue = analogRead(A4);
  }

  // convert to volts
  float voltage = (float)breaker_sensorValue * (5.0 / 1023.0);

  Serial.print(voltage); // print the value in volts
  Serial.println ("V");
}

// MAIN LOOP
void main_loop(){
  while(Serial.available()) {
          if(incoming_str.equals("?R")){
            Serial.print("1");
          }
          else if ((incoming_str.substring(0,4) == "1BS?")){
            braker_state_check(1);
          }
          else if ((incoming_str.substring(0,4) == "2BS?")){
            braker_state_check(2);
          }
          else if ((incoming_str.substring(0,6) == "1&2BS?")){
            braker_state_check(1);
            braker_state_check(2);
          }
    }
}

void loop() {
  main_loop();
}

You forgot to post your code.
Please don't forget the code tags.

@jwut, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with your project. See About the Installation & Troubleshooting category.

Why with pullups?

void main_loop(){

That's new to me

void braker_state_check(int *which_sensor){
. . .
braker_state_check(1);

Not very pointer-like

In which way it doesn’t work? what errors do you get?

In this way

if(*which_sensor==1){

I will test everything tomorrow, but i think only changing pinMode can make the difference. Otherwise i will as you for further help

In case of double readout I have tested:

  if(*which_sensor==1){
    breaker_sensorValue = analogRead(A3);
    delay(10);
    breaker_sensorValue = analogRead(A3);
  }
  else{
    breaker_sensorValue = analogRead(A4);
    delay(10);
    breaker_sensorValue = analogRead(A4);
  }

I didnt get errors at all. I only had wrong voltage readout.
Changing dc source also didnt make a difference.

How you are not getting errors or at least warning when you pass int literal to pointer argument?

Please describe how the pins are connected.

S/B
image

This is how everything is connected. Such connection was taken from a documentation of TCST1103 sensors.

Its useful to post links to components you are using - like this

I'd suggest you use this example & modify it to read each input in turn with a delay between readings - so read A3 & print the voltage, delay, read A4 & print the voltage, delay.

That will tell us if the problem is with the hardware, or with your code.

/*
  ReadAnalogVoltage

  Reads an analog input on pin 0, converts it to voltage, and prints the result to the Serial Monitor.
  Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/ReadAnalogVoltage
*/

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println(voltage);
}

That is a special configuration for testing the high speed capability. For your function you should connect it as I've shown.
Also don't expect a lot of variation in the analog read, These parts are meant to be ON or OFF.

Thank you for this remark.
I have also found such scheme:
obraz
Should I also add R2? Your scheme does not have it

I thought you said you'd got rid of the pointers?

I have just corrected it :slight_smile: sorry.

The following diagram (Fig-1) will help you to understand that it is not possible (and also not needed) to connect internal-pull-up resistor (Rip) when using A3-line (A4) as an analog channel for the internal ADC.

Moreover, while executing this code: ananlogRead(A3), the A3-line is automatically configured as an input line; therefore, you also don't need to include this line : pinMode(A3, INPUT_PULLUP) in the sketch. The similar argument also applies for A4-line.

PIND3
Figure-1:

Hi,
Try this code and see if the A3 and A4 readings interact and if they are accurate.

int A3Val;
int A4Val;
int A3Pin = A3;
int A4Pin = A4;


void setup()
{
  Serial.begin(115200);
}

void loop()
{
  A3Val = analogRead(A3Pin);
  A3Val = analogRead(A3Pin);
  float A3voltage = (float)A3Val * (5.0 / 1023.0);
  A4Val = analogRead(A4Pin);
  A4Val = analogRead(A4Pin);
  float A4voltage = (float)A4Val * (5.0 / 1023.0);
  Serial.print(" A3 Value =");
  Serial.print(A3Val);
  Serial.print("\t A3 Voltage =");
  Serial.print(A3voltage);
  Serial.print("   \tA4 Value =");
  Serial.print(A4Val);
  Serial.print(" \tA4 Voltage =");
  Serial.println(A4voltage);
  delay (250);
}

Tom... :smiley: :+1: :coffee: :australia:

I think this is the solution :slight_smile: besides correcting pointer-related problem and deleting those lines gave me correct readouts. Thank you all for commitment :slight_smile:

1 Like

I have done such test previously and it didn't exhibit different behaviour that occurred during my code execution. However thanks :slight_smile: the problem is solved already I post beneath this one :slight_smile: