read voltage of multiple analog in pins

Hi, I am new to Arduino and I build a device which measures whether or not a nail (in total 3 nails, coupled to a red, blue or green wire) is touched by a pin(coupled to a yellow wire), and which of the three nails is touched (See the Circuit_1 attachment for drawing of the situation). The yellow wire is plugged into the 5V pin of the Arduino and the Arduino’s power comes from my computer (USB coupled). When the yellow pin touches one of the nails, a current will flow. Because all of the little circuits are also attached to an analog pin (Blue=A0, red=A2, green=A4) the voltage can be read and will be plotted in MATLAB (see MATLAB_1 attachment for plot).

It works, but my question is why it is needed to implement a resistor (1k Ohm is used) is every circuit. When I do not implement the resistor and put the ground wire directly in the row where also the analog wire and the sensor (red,blue or green) wires are plugged on to (see Circuit_2 attachement), I will get a really weird jumpy and incorrect voltage measurements when one of the nails is touched. Has this to do with floating pins?

I know that there are more simple constructions to do the measurements, but I just want to know why the one thing works and the other one not.

the MATLAB plot

First of all , while it is technically correct that when the pin touches the nail " a current will flow",
it is not correct to say that because the input impedance of an analog input is in the order of 100Mohms
and the “current” (as you say) that “flows” is in the order of 120 nA. (120E-9 Amps).
We don’t say "current flows in a analog input " because it’ just sounds stupid or ignorant to say that when
the amount of the current approaches zero.
(no offense)
It’s an analog input. The function of an analog input is NOT to measure CURRENT , but to measure VOLTAGE, so we only talk about VOLTAGE when discussing analog inputs. Everyone knows the current is
so close to zero it’s not worth mentioning, and shouldn’t be since an ADC measures voltage, not current.

We say "The voltage measured is 1023 counts*(5/1023)
MATLAB

5/1023

format long
5/1023

ans =

0.004887585532747

(that’s 4.88mV /per count)

So if you read 1023 counts
then
1023 * (5/1023) = 5V

But before we go any further, I should point out that anyone you ask is going to say:
"Why in hell are you using an analog input pin to measure a voltage you already know ?
Why not just connect the pin to DIGITAL Inputs and use digitalRead() ?
That’s the ONLY approach that makes any sense for this application.
I assume that the only reason YOU are using analog inputs is simply lack of
experience with arduinos.

To answer your question about the resistor, your analog inputs are constantly connected (pulled down)
to 0V by the resistor , which serves as a pulldown resistor.
The pulldown resistor stablizes the input current of the analog pin such that when you touch it , with the pin, you are connecting the resistor to 5V, so there are two current paths, one through the resistor at a rate of about 5V/1000 ohms = 5mA , and the second through the wire to the analog pin at about 120nA.
The resistor simply makes it more stable. If you plug a wire in instead of holding the pin so that the analog pin is connected to 5V with NO resistor from 5V to GND, and your hand is not touching it, then
it should be a stable reading of 1023 counts. If your connection from 5V to the analog pin is a good
breadboard connection, it should be a stable reading. I have done this many times to test analog inputs I thought I might have damaged. Maybe your holding it while trying to read it.

So , that being said, let’s forget about the analog input approach.
Use digital inputs. Matlab accepts boolean inputs so you can use the 1s or 0s outputted by digitalRead().
Are you using analog pins because you want to use the serial monitor capture file as an input file for
Matlab ?

How are you processing the data ?
You left that part out. You take readings and then what do you do with those ?
Are they serial monitor capture files used as input files for matlab ?
I have Matlab and I know it accepts both text and .csv files so serial monitor capture file could be
used as a matlab import file.

Without the resistors you have a short circuit from 5V to GND.

Without the resistors you have a short circuit from 5V to GND.

Actually, this schematic here is wrong :

When you touch the yellow wire to the three inputs, the three black wires short the yellow wire (and hence 5V) to GND, causing the reset.

|457x500

REMOVE THOSE THREE BLACK WIRES THAT CONNECT THE COLORED WIRES TO GND.

(actually , scrap the whole analog input scheme. It's ridiculous)

Thanks for the responses!! Yes indeed I am totally new to Arduino. The reason why I used the Analog inputs is that I first wanted to implement a force sensitive resistor to measure the force and thus variating voltages, but I later skipped that for this construction. Indeed for just "touch or no touch" digital inputs are more suited and therefore this the design with the analog inputs is a bit crappy I know. But I just wanted to know why it works like is does to learn more about how Arduino works.

the Matlab script (adapted and adjusted a bit from another code for temperature measurements) is down here:

%User Defined Properties a = arduino('/dev/cu.usbmodem14101','Uno') % define the Arduino Communication port plotTitle = 'Arduino Data Log'; % plot title xLabel = 'Elapsed Time (s)'; % x-axis label yLabel = 'Voltage (V)'; % y-axis label legend1 = 'Voltage Sensor 1' legend2 = 'Voltage Sensor 2' legend3 = 'Voltage Sensor 3' yMax = 10; %y Maximum Value yMin = 0; %y minimum Value plotGrid = 'on'; % 'off' to turn off grid min = 0; % set y-min max = 10; % set y-max delay = .01; % make sure sample faster than resolution %Define Function Variables time = 0; Red1 = 0; Blue1 = 0; Green1 = 0; count = 0;

%Set up Plot plotGraphRed = plot(time,Red1,'-r' ) % every AnalogRead needs to be on its own Plotgraph hold on %hold on makes sure all of the channels are plotted plotGraphBlue = plot(time,Blue1,'-b') plotGraphGreen = plot(time, Green1,'-g' ) title(plotTitle,'FontSize',15); xlabel(xLabel,'FontSize',15); ylabel(yLabel,'FontSize',15); legend(legend1,legend2,legend3) axis([yMin yMax min max]); grid(plotGrid); tic while ishandle(plotGraphRed) %Loop when Plot is Active will run until plot is closed Red2 = readVoltage(a,'A2'); %Data from the arduino Blue2 = readVoltage(a,'A0'); Green2 = readVoltage(a,'A4'); count = count + 1; time(count) = toc; Red1(count) = Red2(1); Blue1(count) = Blue2(1) Green1(count) = Green2(1) %This is the magic code %Using plot will slow down the sampling time.. At times to over 20 %seconds per sample! set(plotGraphRed,'XData',time,'YData',Red1); set(plotGraphBlue,'XData',time,'YData',Blue1); set(plotGraphGreen,'XData',time,'YData',Green1); axis([0 time(count) min max]); %Update the graph pause(delay); end delete(a); disp('Plot Closed and arduino object has been deleted');

When posting code, please use the CODE TAGS toolbutton (</>) (first toolbutton from the left)

Like this:

 %User Defined Properties 
a = arduino('/dev/cu.usbmodem14101','Uno')             % define the Arduino Communication port
plotTitle = 'Arduino Data Log';  % plot title
xLabel = 'Elapsed Time (s)';     % x-axis label
yLabel = 'Voltage (V)';      % y-axis label
legend1 = 'Voltage Sensor 1'
legend2 = 'Voltage Sensor 2'
legend3 = 'Voltage Sensor 3'
yMax  = 10;                           %y Maximum Value
yMin  = 0;                       %y minimum Value
plotGrid = 'on';                 % 'off' to turn off grid
min = 0;                         % set y-min
max = 10;                        % set y-max
delay = .01;                     % make sure sample faster than resolution 
%Define Function Variables
time = 0;
Red1 = 0;
Blue1 = 0;
Green1 = 0;
count = 0;


%Set up Plot
plotGraphRed = plot(time,Red1,'-r' )  % every AnalogRead needs to be on its own Plotgraph
hold on                            %hold on makes sure all of the channels are plotted
plotGraphBlue = plot(time,Blue1,'-b')
plotGraphGreen = plot(time, Green1,'-g' )
title(plotTitle,'FontSize',15);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
legend(legend1,legend2,legend3)
axis([yMin yMax min max]);
grid(plotGrid);
tic
while ishandle(plotGraphRed) %Loop when Plot is Active will run until plot is closed
         Red2 = readVoltage(a,'A2'); %Data from the arduino
         Blue2 = readVoltage(a,'A0');
         Green2 = readVoltage(a,'A4');      
         count = count + 1;    
         time(count) = toc;    
         Red1(count) = Red2(1);         
         Blue1(count) = Blue2(1)
         Green1(count) = Green2(1)
         %This is the magic code 
         %Using plot will slow down the sampling time.. At times to over 20
         %seconds per sample!
         set(plotGraphRed,'XData',time,'YData',Red1);
         set(plotGraphBlue,'XData',time,'YData',Blue1);
         set(plotGraphGreen,'XData',time,'YData',Green1);
          axis([0 time(count) min max]);
          %Update the graph
          pause(delay);
  end
delete(a);
disp('Plot Closed and arduino object has been deleted');

Please acknowledge that you understand the two lasts posts and have corrected the issue by removing the black wires (I assume you already did that when you added the resistors) This is a courtesy to the
forum members who take the time to help you. Even though you have probably removed the three black wires please indicate that you have done so as a courtesy to the forum member who found the answer to your post question. It is also forum policy to give that person a karma click by clicking on the “[add]”
button next to their karma button.
In this case, the person who answered your OP question was JCA34F

Comment by JCA34F

Without the resistors you have a short circuit from 5V to GND.

I assume you see why touching the yellow wire to one of the colored wires when you had the three black jumpers on the board before you added the resistors would short the 5V to GND causing the arduino to reset and result in the strange serial output ?

Yes I understand the posts and and understand the concept now. The issue has been resolved (indeed already by implementing the resistors). Thanks for the help and I gave JCA24F a karma click.

Great ! Looks like you are passing the arduino output directly to matlab using the Device Manager device name. I actually had not learned that yet in my Udemy.com matlab courses but I'm going to try that. Where did you learn that ?

When I was looking into ways for doing the touch sensor and real time data log with matlab and arduino I found the following code:

https://nl.mathworks.com/matlabcentral/answers/333946-how-to-plot-the-real-time-data-from-arduino-in-matlab

and I used the following support package on mathworks:

https://nl.mathworks.com/matlabcentral/fileexchange/57170-legacy-capacitive-sensor-add-on-library-for-arduino

Basically your mistake is in thinking that if nothing is connected to a pin it will read as a low. Nothing can be further from the truth.
Read:- Inputs

Hi,

I tried the same thing and my question is here. It would be great if anyone could help to solve this. I don't get the desired output as compared to the oscilloscope. https://www.mathworks.com/matlabcentral/answers/591376-why-is-readvoltage-not-getting-me-the-correct-output?s_tid=srchtitle

https://forum.arduino.cc/index.php?topic=704536.0;topicseen

Did you not like or understand the answer you got at https://forum.arduino.cc/index.php?topic=704536.0;topicseen
Basically you can’t.

The closest you can get is to read all the analogue inputs and then display them. But this then puts the nyquist rate at half of the sample rate of all the analogue reads in one cycle of them all. This means that the examples you posted are waveforms with frequencies that are way too high and so you are seeing aliasing. You need at least 10 samples to start to see a waveform.

The yellow wire is plugged into the 5V pin of the Arduino and the Arduino's power comes from my computer (USB coupled). When the yellow pin touches one of the nails, a current will flow. Because all of the little circuits are also attached to an analog pin (Blue=A0, red=A2, green=A4) the voltage can be read and will be plotted in MATLAB (see MATLAB_1 attachment for plot)

Not much current. Arduino analog input pin is about 175uA. The current through the resistor is 5V/1000 ohms = 0.005A (5mA) but only 175uA is actually going in the analog input pin because the input impedance is 10Mohm.

Datasheet says 1uA. Input Leakage Current I/O Pin, with Vcc = 5.5V: 1uA. Iin high or Iin low. V=IR, V/I = R thus R = 5/.000001 = 5 Megohm?

Gents, look at the data of the OP. This is a thread hijack.