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();
}
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.
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.
I think this is the solution besides correcting pointer-related problem and deleting those lines gave me correct readouts. Thank you all for commitment
I have done such test previously and it didn't exhibit different behaviour that occurred during my code execution. However thanks the problem is solved already I post beneath this one