First, let’s try to visualize analog signals
which reach Arduino input.
For this purpose, special devices called oscilloscopes are used,
but we are not going to connect our circuit to it. Instead, we will just
imitate the way their program works.
To accomplish this, we are going to make use of an outstanding tool called
Processing,
which you will be studying in detail next week.
For now, you can download it following the link in the Additional materials
section,
as well as download the program which I wrote to visualize analog signals.
Start Processing, open my program and take a look at the line
where the port is mentioned, to which the controller is connected.
You need to do it in a way so that its number corresponds to the
actual port number
that you can see in your system.
Also, have a good look at this number – this is the connection speed.
It should correspond to the speed mentioned in the AnalogReadSerial
sketch,
which you downloaded at the beginning of the experiment.
Don’t worry, we are going to speak about the connection speed in
detail in the next fragment.
For now, you should just pay attention to it when the program starts.
After that, you can start the visualizer by pressing the “Run” button.
We can see a window like this, which is empty at the moment,
because the potentiometer is set to 0.
Now I am going to rotate it, and now let’s look
at this line,
which corresponds to the change of the voltage at the analog input.
Since the potentiometer covers the whole range,
we have witnessed changes from 0 to 1023.
Apart from that, I have activated the display of the minimum and maximum
observed values. This might come in handy when you study the voltage
range
communicated by various analog sensors.
Looking at this picture, you might have already understood that the
input voltage
changes unstoppably, meaning that at some point it can take the value of,
let’s say, 2,8327 V.
This value can be brought to further precision, but for some reason
we only saw numbers from 0 to 023 on the screen.
The thing is that when the signal reaches the controller,
it undergoes certain changes.
It passes through an analog-to-digital converter, aka ADC.
If I try to explain the way the ADC works in simple terms,
it will be as follows: a special device known as a comparator,
can compare current voltage with some base voltage
and communicate whether it has got 0 or 1 as a result of this comparison,
and whether current voltage is higher or lower than base voltage.
In case with a 10-bit ADC, this happens 10 times,
and we get a combination of 10 ones and zeros which,
after being converted to a decimal number, display the value from
0 to 1023.
As a result, the range of 0-5 V, which we can communicate to
Arduino input, can be divided in 1024 units.
In other words, the difference in units means that the voltage
difference is approximately
0,005 V.
In fact, it is a bit lower.
Thus, we have a certain number at the input.
But how do we work with it in a program?
How does it appear there?
Let’s have a look.
Let me bring you back to the example
with which our experiment began — the one from the sketch
template called
AnalogReadSerial.
You might have already realized that
the function analogRead is responsible for reading off the analog signal.
This is another Arduino integrated function
which is designated to read off analog signals.
It receives only one thing as a parameter, which is pin number.
Let me remind you that you can refer to analog inputs
by using their board indications: А0,
А, etc. All these notations are already built in the Arduino library.
Please, take note of the fact that The analogRead function can be
called to the right of
the assignment operator, while to its left appears the name of
the variable
which is set here – the integer variable sensorValue.
Last week we already learnt how to create int-type variables.
However, here I would like to point your attention to the fact that
when we want
to read off values from a sensor, we have to use these values somewhere.
In our case, we are going to save the value into a variable.
You can also communicate it as a parameter to another function
or use it in your calculations.
Many times I have noticed how beginner Arduino amateurs
are simply trying to call the analogRead(A0) instruction when they want to read off
data from a sensor.
In reality, the controller possibly reads something,
but the value that it gets, cannot be found anywhere.
This is why, when you read off values from sensors, do try to understand
where they go.
[PAUSE] The next thing
that we will have to figure out, is how exactly the data
is communicated to the computer screen, and how we can read it from there.